python - Converting list of list into 2 independent lists -
i have list of lists looks this:
lol=[['"buy":17'], ['"hold":18'], ['"sell":3']]
is there simple way convert list of lists 2 independent lists this?:
list1=["buy","hold","sell"] list2=[17,18,3]
first tried replace on list:
lol.replace('[','').replace(']','')
but outputed 'list' object has no attribute 'replace'
then thought use regular expressions on lol
@ least obtaining numbers in independent list following code:
re.findall('\d{1,2}',lol.string)
but returned outputed expected string or bytes-like object
you can split strings on ':'
, use ast.literal_eval
produce final output:
import ast l1, l2 = zip(*[map(ast.literal_eval, lst[0].split(':')) lst in lol]) print(l1, l2) # ('buy', 'hold', 'sell'), (17, 18, 3)
wiki
Comments
Post a Comment