Given this dict:
d={0: [(78.65, 89.86),
(28.0, 23.0),
(63.43, 9.29),
(66.47, 55.47),
(68.0, 4.5),
(69.5, 59.0),
(86.26, 1.65),
(84.2, 56.2),
(88.0, 18.53),
(111.0, 40.0)], ...}
How do you create two lists, such that y takes the first element of each tuple and x takes the second, for each key in d?
In the example above (only key=0 is shown) this would be:
y=[78.65, 28.0, 63.43, 66.47, 68.0, 69.5, 86.26, 84.2, 88.0, 111.0]
x=[89.86, 23.0, 9.29, 55.47, 4.5, 59.0, 1.65, 56.2, 18.53, 40.0]
My attempt is wrong (I tried the x list only):
for j,v in enumerate(d.values()):
x=[v[i[1]] for v in d.values() for i in v]
Because:
TypeError Traceback (most recent call last)
<ipython-input-97-bdac6878fe6c> in <module>()
----> 1 x=[v[i[1]] for v in d.values() for i in v]
TypeError: list indices must be integers, not numpy.float64
What's wrong with this?
key=1? In that case, not much, because I want to extract these two lists inside a for loop, so that at each iteration they are something different. Does this answer your question?x=[i[1] for v in d.values() for i in v]should work for your snippet