0

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.

2
  • Have you tried it doing yourself? Commented Jul 1, 2015 at 5:29
  • @NamanSogani. Thanks for you advice.I have thought for some time but did not come up with a good method, Commented Jul 1, 2015 at 5:33

4 Answers 4

2

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]]
Sign up to request clarification or add additional context in comments.

Comments

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]

Comments

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

3 Comments

Cast to int is missing.
@Delgan yes will change it
@i'L'i changed that to
1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.