简体   繁体   English

从列表中提取元素以在Python中形成二维数组

[英]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'以这种方式形成2d数组:

[[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+, 如果您使用的是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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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