简体   繁体   中英

Python: how to make two lists from a dictionary

I have a dictionary.

{1 : [1.2, 2.3, 4.9, 2.0],  2 : [4.1, 5.1, 6.3],  3 : [4.9, 6.8, 9.5, 1.1, 7.1]}

I want to pass each key:value pair to an instance of matplotlib.pyplot as two lists: x values and y values.

Each key is an x value associated with each item in its value.

So I want two lists for each key:

[1,1,1,1] [1.2,2.3,4.9,2.0]

[2,2,2] [4.1,5.1,6.3]

[3,3,3,3,3] [4.9,6.8,9.5,1.1,7.1]

Is there an elegant way to do this?

Or perhaps there is a way to pass a dict to matplotlib.pyplot ?

for k, v in dictionary.iteritems():
    x = [k] * len(v)
    y = v
    pyplot.plot(x, y)
d = {1 : [1.2, 2.3, 4.9, 2.0], 2 : [4.1, 5.1, 6.3], 3 : [4.9, 6.8, 9.5, 1.1, 7.1]}

res = [([x]*len(y), y) for x, y in d.iteritems()]

res will be a list of tuples, where the first element in the tuple is your list of x-values and second element in the tuple is your list f y-values

Maybe something like:

d = {1 : [1.2, 2.3, 4.9, 2.0], 2 : [4.1, 5.1, 6.3], 3 : [4.9, 6.8, 9.5, 1.1, 7.1]}
result = []
for key, values in d.items():
    result.append(([key]*len(values), values))

Use this list comprehension:

[([k]*len(v), v) for k, v in D.iteritems()]

Here's an example of it being used:

>>> from pprint import pprint
>>> D = {1: [1.2, 2.3, 4.9, 2.0], 2: [4.1, 5.1, 6.3], 3: [4.9, 6.8, 9.5, 1.1, 7.1]}
>>> LL = [([k]*len(v), v) for k, v in D.iteritems()]
>>> pprint(LL)
[([1, 1, 1, 1], [1.2, 2.2999999999999998, 4.9000000000000004, 2.0]),
 ([2, 2, 2], [4.0999999999999996, 5.0999999999999996, 6.2999999999999998]),
 ([3, 3, 3, 3, 3],
  [4.9000000000000004,
   6.7999999999999998,
   9.5,
   1.1000000000000001,
   7.0999999999999996])]

As a list comprehension:

r = [([k]*len(v), v) for k,v in d.items()]

If your dictionary is very large, you'd want to use a generator expression:

from itertools import repeat
r = ((repeat(k, len(v)), v) for k,v in d.iteritems())

...though note that using repeat means that the first item in each tuple the generator returns is itself a generator. That's unnecessary if the dictionary's values don't themselves have many items.

>>> d = {1 : [1.2, 2.3, 4.9, 2.0], 2 : [4.1, 5.1, 6.3], 3 : [4.9, 6.8, 9.5, 1.1, 7.1]}
>>> result = [ ([k] * len(d[k]), d[k]) for k in d.keys() ]
>>> print result
[([1, 1, 1, 1], [1.2, 2.2999999999999998, 4.9000000000000004, 2.0]), ([2, 2, 2],
[4.0999999999999996, 5.0999999999999996, 6.2999999999999998]), ([3, 3, 3, 3, 3],
[4.9000000000000004, 6.7999999999999998, 9.5, 1.1000000000000001, 7.0999999999999996])]

I guess that a wizard will put something nicer, but I would do something like:

map(lambda x: ([x]*len(a[x]),a[x]),a)

for a tuple, or

map(lambda x: [[x]*len(a[x]),a[x]],a)

for a list.

btw: a is the dictionary, of course!

I assume that you work with the 2.x series... Regards

the map function in python will allow this

x = [1,2,4]
y = [1,24,2]
c = zip(x,y)
print c
d = map(None,x,y)
print d

check it out. This will give you

[(1, 1), (2, 24), (4, 2)]

In the case of zip(), if one of the lists are smaller then the others, values will be truncated:

x = [1,2,4]
a = [1,2,3,4,5]
c = zip(x,a)
print c
d = map(None,x,a)
print d

[(1, 1), (2, 2), (4, 3)]
[(1, 1), (2, 2), (4, 3), (None, 4), (None, 5)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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