Re: Zipping a dictionary whose values are lists
On 13 April 2012 17:35, Kiuhnm kiuhnm03.4t.yahoo...@mail.python.org wrote: On 4/13/2012 17:58, Alexander Blinne wrote: zip(*[x[1] for x in sorted(d.items(), key=lambda y: y[0])]) Or zip(*[d[k] for k in sorted(d.keys())]) .keys() is superfluous here: zip(*(d[k] for k in sorted(d))) -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list
RE: Zipping a dictionary whose values are lists
The below code should work: zip(*d.values()) when you do *d.values() its going to return tuple of elements, which then further be can be zipped to achieve your desired result. Regards, Shambhu Rajak Python Lover -Original Message- From: tkp...@gmail.com [mailto:tkp...@gmail.com] Sent: 12/04/2012 9:58 PM To: python-list@python.org Subject: Zipping a dictionary whose values are lists I using Python 3.2 and have a dictionary d = {0:[1,2], 1:[1,2,3], 2:[1,2,3,4]} whose values are lists I would like to zip into a list of tuples. If I explicitly write: list(zip([1,2], [1,2,3], [1,2,3,4]) [(1, 1, 1), (2, 2, 2)] I get exactly what I want. On the other hand, I have tried list(zip(d)) [(0,), (1,), (2,)] list(zip(d.values())) [([1, 2],), ([1, 2, 3],), ([1, 2, 3, 4],)] list(zip(d[i] for i in d)) [([1, 2],), ([1, 2, 3],), ([1, 2, 3, 4],)] list(zip(*d)) Traceback (most recent call last): File pyshell#48, line 1, in module list(zip(*d)) TypeError: zip argument #1 must support iteration and nothing quite works. What am I doing wrong? Sincerely Thomas Philips -- http://mail.python.org/mailman/listinfo/python-list
Re: Zipping a dictionary whose values are lists
Am 12.04.2012 18:38, schrieb Kiuhnm: Almost. Since d.values() = [[1,2], [1,2,3], [1,2,3,4]], you need to use list(zip(*d.values())) which is equivalent to list(zip([1,2], [1,2,3], [1,2,3,4])) Kiuhnm While this accidently works in this case, let me remind you that d.values() does not return the elements of the d in any specific order. (It is a non-random but implementation-specific order, see http://docs.python.org/library/stdtypes.html#dict.items.) Thus if you need the correct order (as suggested by the dict keys) an explicit sorting step is required, for example zip(*[x[1] for x in sorted(d.items(), key=lambda y: y[0])]) Greetings -- http://mail.python.org/mailman/listinfo/python-list
Re: Zipping a dictionary whose values are lists
Alexander Blinne wrote: zip(*[x[1] for x in sorted(d.items(), key=lambda y: y[0])]) Why not zip(*[x[1] for x in sorted(d.items())])? -- http://mail.python.org/mailman/listinfo/python-list
Re: Zipping a dictionary whose values are lists
On 4/13/2012 17:58, Alexander Blinne wrote: Am 12.04.2012 18:38, schrieb Kiuhnm: Almost. Since d.values() = [[1,2], [1,2,3], [1,2,3,4]], you need to use list(zip(*d.values())) which is equivalent to list(zip([1,2], [1,2,3], [1,2,3,4])) Kiuhnm While this accidently works in this case, let me remind you that d.values() does not return the elements of the d in any specific order. The OP said nothing about ordering. The fact that the keys are ordered might be accidental :) (It is a non-random but implementation-specific order, see http://docs.python.org/library/stdtypes.html#dict.items.) Thus if you need the correct order (as suggested by the dict keys) an explicit sorting step is required, for example zip(*[x[1] for x in sorted(d.items(), key=lambda y: y[0])]) Or zip(*[d[k] for k in sorted(d.keys())]) Kiuhnm -- http://mail.python.org/mailman/listinfo/python-list
Re: Zipping a dictionary whose values are lists
zip(*d.values()) On 12 April 2012 20:28, tkp...@gmail.com wrote: I using Python 3.2 and have a dictionary d = {0:[1,2], 1:[1,2,3], 2:[1,2,3,4]} whose values are lists I would like to zip into a list of tuples. If I explicitly write: list(zip([1,2], [1,2,3], [1,2,3,4]) [(1, 1, 1), (2, 2, 2)] I get exactly what I want. On the other hand, I have tried list(zip(d)) [(0,), (1,), (2,)] list(zip(d.values())) [([1, 2],), ([1, 2, 3],), ([1, 2, 3, 4],)] list(zip(d[i] for i in d)) [([1, 2],), ([1, 2, 3],), ([1, 2, 3, 4],)] list(zip(*d)) Traceback (most recent call last): File pyshell#48, line 1, in module list(zip(*d)) TypeError: zip argument #1 must support iteration and nothing quite works. What am I doing wrong? Sincerely Thomas Philips -- http://mail.python.org/mailman/listinfo/python-list -- С уважением, Аносов Павел -- http://mail.python.org/mailman/listinfo/python-list
Re: Zipping a dictionary whose values are lists
On 4/12/2012 18:28, tkp...@gmail.com wrote: I using Python 3.2 and have a dictionary d = {0:[1,2], 1:[1,2,3], 2:[1,2,3,4]} whose values are lists I would like to zip into a list of tuples. If I explicitly write: list(zip([1,2], [1,2,3], [1,2,3,4]) [(1, 1, 1), (2, 2, 2)] I get exactly what I want. On the other hand, I have tried list(zip(d)) [(0,), (1,), (2,)] list(zip(d.values())) [([1, 2],), ([1, 2, 3],), ([1, 2, 3, 4],)] Almost. Since d.values() = [[1,2], [1,2,3], [1,2,3,4]], you need to use list(zip(*d.values())) which is equivalent to list(zip([1,2], [1,2,3], [1,2,3,4])) Kiuhnm -- http://mail.python.org/mailman/listinfo/python-list
Re: Zipping a dictionary whose values are lists
tkp...@gmail.com wrote: I using Python 3.2 and have a dictionary d = {0:[1,2], 1:[1,2,3], 2:[1,2,3,4]} whose values are lists I would like to zip into a list of tuples. If I explicitly write: list(zip([1,2], [1,2,3], [1,2,3,4]) [(1, 1, 1), (2, 2, 2)] I get exactly what I want. On the other hand, I have tried list(zip(d)) [(0,), (1,), (2,)] list(zip(d.values())) [([1, 2],), ([1, 2, 3],), ([1, 2, 3, 4],)] list(zip(d[i] for i in d)) [([1, 2],), ([1, 2, 3],), ([1, 2, 3, 4],)] list(zip(*d)) Traceback (most recent call last): File pyshell#48, line 1, in module list(zip(*d)) TypeError: zip argument #1 must support iteration and nothing quite works. What am I doing wrong? You have all the building blocks ;) d = {0:[1,2], 1:[1,2,3], 2:[1,2,3,4]} list(zip(*d.values())) [(1, 1, 1), (2, 2, 2)] The order of the values is undefined, so you may want to sort the lists by key first: list(zip(*[v for k, v in sorted(d.items())])) [(1, 1, 1), (2, 2, 2)] Well, I guess it doesn't really matter for that example... -- http://mail.python.org/mailman/listinfo/python-list
Re: Zipping a dictionary whose values are lists
On Thu, 12 Apr 2012 09:28:03 -0700 (PDT) tkp...@gmail.com wrote: I using Python 3.2 and have a dictionary d = {0:[1,2], 1:[1,2,3], 2:[1,2,3,4]} whose values are lists I would like to zip into a list of tuples. If I explicitly write: list(zip([1,2], [1,2,3], [1,2,3,4]) [(1, 1, 1), (2, 2, 2)] I get exactly what I want. On the other hand, I have tried list(zip(d)) [(0,), (1,), (2,)] list(zip(d.values())) [([1, 2],), ([1, 2, 3],), ([1, 2, 3, 4],)] list(zip(d[i] for i in d)) [([1, 2],), ([1, 2, 3],), ([1, 2, 3, 4],)] list(zip(*d)) Traceback (most recent call last): File pyshell#48, line 1, in module list(zip(*d)) TypeError: zip argument #1 must support iteration and nothing quite works. What am I doing wrong? Try this: list(zip(*d.values())) d.values() is a list, but zip wants the individual values as separate arguments. HTH, Dan -- http://mail.python.org/mailman/listinfo/python-list