2015-07-01 60 views
0

這裏是數據我有:提取元件,以形成一個二維數組在Python

[('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)] 

我想提取,說元件like'600855 + 600860' 以形成在此的2D陣列方式:

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

怎麼辦? 謝謝。

+0

你有沒有嘗試過做自己? –

+0

@NamanSogani。感謝您的建議。我想了一段時間,但沒有拿出一個好方法, – epx

回答

2

你可以試試這個,

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

投給'int'丟失。 – Delgan

+0

@Delgan是的會改變它 – The6thSense

+1

@i'L'i改變它 – The6thSense

1

如果您正在使用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) 
相關問題