epx
epx

Reputation: 591

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.

Upvotes: 0

Views: 53

Answers (4)

Addarsh Chandrasekar
Addarsh Chandrasekar

Reputation: 578

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)

Upvotes: 1

The6thSense
The6thSense

Reputation: 8335

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]]

Upvotes: 1

TigerhawkT3
TigerhawkT3

Reputation: 49318

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]

Upvotes: 1

Avinash Raj
Avinash Raj

Reputation: 174706

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]]

Upvotes: 2

Related Questions