简体   繁体   中英

extract element from list to form a 2-d array in Python

Here is the data I have:

[('600855+600860', 0.17285466741397107), ('600841+600984', 0.22412665503906901), ('600860+600984', 0.32286764496195208), ('600815+600841', 0.33635550553792876), ('600841+600860', 0.39050910738491346), ('600815+600860', 0.40568508088748162), ('600841+600855', 0.41509110502628777), ('600855+600984', 0.44548966249191208), ('600815+600855', 0.46775374453232454), ('600815+600984', 0.59956672168742298)]

I want to extract, say element like'600855+600860' to form a 2d array in this way:

[[600855,600860], [600841,600984]......]

How to do it? Thanks.

You may try this,

>>> l = [('600855+600860', 0.17285466741397107), ('600841+600984', 0.22412665503906901), ('600860+600984', 0.32286764496195208), ('600815+600841', 0.33635550553792876), ('600841+600860', 0.39050910738491346), ('600815+600860', 0.40568508088748162), ('600841+600855', 0.41509110502628777), ('600855+600984', 0.44548966249191208), ('600815+600855', 0.46775374453232454), ('600815+600984', 0.59956672168742298)]
>>> [list(map(int,i[0].split('+'))) for i in l]
[[600855, 600860], [600841, 600984], [600860, 600984], [600815, 600841], [600841, 600860], [600815, 600860], [600841, 600855], [600855, 600984], [600815, 600855], [600815, 600984]]
l = [('600855+600860', 0.17285466741397107),
     ('600841+600984', 0.22412665503906901),
     ('600860+600984', 0.32286764496195208),
     ('600815+600841', 0.33635550553792876),
     ('600841+600860', 0.39050910738491346),
     ('600815+600860', 0.40568508088748162),
     ('600841+600855', 0.41509110502628777),
     ('600855+600984', 0.44548966249191208),
     ('600815+600855', 0.46775374453232454),
     ('600815+600984', 0.59956672168742298)]

l2 = [list(map(int, item[0].split('+'))) for item in l]
a=[('600855+600860', 0.17285466741397107), ('600841+600984', 0.22412665503906901), ('600860+600984', 0.32286764496195208), ('600815+600841', 0.33635550553792876), ('600841+600860', 0.39050910738491346), ('600815+600860', 0.40568508088748162), ('600841+600855', 0.41509110502628777), ('600855+600984', 0.44548966249191208), ('600815+600855', 0.46775374453232454), ('600815+600984', 0.59956672168742298)]
[map(int,f[0].split("+")) for f in a  ]
[[600855, 600860], [600841, 600984], [600860, 600984], [600815, 600841], [600841, 600860], [600815, 600860], [600841, 600855], [600855, 600984], [600815, 600855], [600815, 600984]]

If you are using Python 3+,

data = [('600855+600860', 0.17285466741397107), ('600841+600984', 0.22412665503906901), ('600860+600984', 0.32286764496195208), ('600815+600841', 0.33635550553792876), ('600841+600860', 0.39050910738491346), ('600815+600860', 0.40568508088748162), ('600841+600855', 0.41509110502628777), ('600855+600984', 0.44548966249191208), ('600815+600855', 0.46775374453232454), ('600815+600984', 0.59956672168742298)]

numList = []

for (key,value) in data:
    numList.append(list(map(int, key.split("+"))))

print(numList)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM