Re: Inverse of dict(zip(x,y))
Andre Engels wrote: On Thu, Mar 5, 2009 at 6:01 PM, Tino Wildenhain t...@wildenhain.de wrote: Still I'd like to see an application where this really matters (that keys() and values() match in order) I think there are many such applications, but also that in each of those cases it's a mis-programming of something that would be done better and more pythonic using items()... Thats what I'm thinking, so I'd like to see a few if there are so many... T. smime.p7s Description: S/MIME Cryptographic Signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Inverse of dict(zip(x,y))
Hi, psykeedelik wrote: On Mar 5, 6:01 pm, Tino Wildenhain t...@wildenhain.de wrote: Piet van Oostrum wrote: Andre Engels andreeng...@gmail.com (AE) wrote: AE On Wed, Mar 4, 2009 at 11:02 AM, lone_eagle icym...@gmail.com wrote: Can someone suggest a easy method to do the inverse of dict(zip(x,y)) to get two lists x and y? So, if x and y are two lists, it is easier to make a dictionary using d = dict(zip(x,y)), but if I have d of the form, d = {x1:y1, x2:y2, ...}, what is there any trick to get lists x = [x1, x2, ...] and y = [y1, y2, ...] AE x = d.keys() AE y = [d[e] for d in x] AE y = d.values() might also work, but I am not sure whether d.keys() and AE d.values() are guaranteed to use the same order. Yes, they are if the dictionary is not changed in the meantime (not even inserting and removing the same thing). See the library documentation, section dict. Still I'd like to see an application where this really matters (that keys() and values() match in order) Tino smime.p7s 4KViewDownload First, thanks to all the guys who posted replies to my query!! And then, just as an example to what Tino raised ... I usually get properties that I compute, in a dictionary like property = [key1: val1, key2:val2, ...] and then I usually want to plot them in pylab, which AFAIK requires x and y as lists for the plot argument. Then I need to get the lists [key1, key2, ...] and [val1, val2, ...]. And now I wonder if there a more efficient way doing what I described above!! ;) I didn't check but I don't believe that. Usually x/y plots base on tuples for (x,y) and that's exactly what items() would deliver. Regards Tino smime.p7s Description: S/MIME Cryptographic Signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Inverse of dict(zip(x,y))
Andre Engels wrote: On Thu, Mar 5, 2009 at 7:46 PM, Andre Engels andreeng...@gmail.com wrote: If the dict = {key1: val1, key2: val2, ...}, you can do: for key in dict: plot(key,dictionary[key]) Of course I meant: for key in dict: plot(key,dict[key]) Which would be the verbose form of: for x,y in datadict.items(): plot(x,y) T. smime.p7s Description: S/MIME Cryptographic Signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Inverse of dict(zip(x,y))
Piet van Oostrum wrote: Andre Engels andreeng...@gmail.com (AE) wrote: AE On Wed, Mar 4, 2009 at 11:02 AM, lone_eagle icym...@gmail.com wrote: Can someone suggest a easy method to do the inverse of dict(zip(x,y)) to get two lists x and y? So, if x and y are two lists, it is easier to make a dictionary using d = dict(zip(x,y)), but if I have d of the form, d = {x1:y1, x2:y2, ...}, what is there any trick to get lists x = [x1, x2, ...] and y = [y1, y2, ...] AE x = d.keys() AE y = [d[e] for d in x] AE y = d.values() might also work, but I am not sure whether d.keys() and AE d.values() are guaranteed to use the same order. Yes, they are if the dictionary is not changed in the meantime (not even inserting and removing the same thing). See the library documentation, section dict. Still I'd like to see an application where this really matters (that keys() and values() match in order) Tino smime.p7s Description: S/MIME Cryptographic Signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Inverse of dict(zip(x,y))
On Thu, Mar 5, 2009 at 6:01 PM, Tino Wildenhain t...@wildenhain.de wrote: Still I'd like to see an application where this really matters (that keys() and values() match in order) I think there are many such applications, but also that in each of those cases it's a mis-programming of something that would be done better and more pythonic using items()... -- André Engels, andreeng...@gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Inverse of dict(zip(x,y))
On Mar 5, 6:01 pm, Tino Wildenhain t...@wildenhain.de wrote: Piet van Oostrum wrote: Andre Engels andreeng...@gmail.com (AE) wrote: AE On Wed, Mar 4, 2009 at 11:02 AM, lone_eagle icym...@gmail.com wrote: Can someone suggest a easy method to do the inverse of dict(zip(x,y)) to get two lists x and y? So, if x and y are two lists, it is easier to make a dictionary using d = dict(zip(x,y)), but if I have d of the form, d = {x1:y1, x2:y2, ...}, what is there any trick to get lists x = [x1, x2, ...] and y = [y1, y2, ...] AE x = d.keys() AE y = [d[e] for d in x] AE y = d.values() might also work, but I am not sure whether d.keys() and AE d.values() are guaranteed to use the same order. Yes, they are if the dictionary is not changed in the meantime (not even inserting and removing the same thing). See the library documentation, section dict. Still I'd like to see an application where this really matters (that keys() and values() match in order) Tino smime.p7s 4KViewDownload First, thanks to all the guys who posted replies to my query!! And then, just as an example to what Tino raised ... I usually get properties that I compute, in a dictionary like property = [key1: val1, key2:val2, ...] and then I usually want to plot them in pylab, which AFAIK requires x and y as lists for the plot argument. Then I need to get the lists [key1, key2, ...] and [val1, val2, ...]. And now I wonder if there a more efficient way doing what I described above!! ;) Cheers, Chaitanya -- http://mail.python.org/mailman/listinfo/python-list
Re: Inverse of dict(zip(x,y))
On Thu, Mar 5, 2009 at 7:07 PM, psykeedelik icym...@gmail.com wrote: I usually get properties that I compute, in a dictionary like property = [key1: val1, key2:val2, ...] and then I usually want to plot them in pylab, which AFAIK requires x and y as lists for the plot argument. Then I need to get the lists [key1, key2, ...] and [val1, val2, ...]. And now I wonder if there a more efficient way doing what I described above!! ;) If the dict = {key1: val1, key2: val2, ...}, you can do: for key in dict: plot(key,dictionary[key]) -- André Engels, andreeng...@gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Inverse of dict(zip(x,y))
On Thu, Mar 5, 2009 at 7:46 PM, Andre Engels andreeng...@gmail.com wrote: If the dict = {key1: val1, key2: val2, ...}, you can do: for key in dict: plot(key,dictionary[key]) Of course I meant: for key in dict: plot(key,dict[key]) -- André Engels, andreeng...@gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Inverse of dict(zip(x,y))
On Mar 5, 9:01 am, Tino Wildenhain t...@wildenhain.de wrote: Piet van Oostrum wrote: Andre Engels andreeng...@gmail.com (AE) wrote: AE On Wed, Mar 4, 2009 at 11:02 AM, lone_eagle icym...@gmail.com wrote: Can someone suggest a easy method to do the inverse of dict(zip(x,y)) to get two lists x and y? So, if x and y are two lists, it is easier to make a dictionary using d = dict(zip(x,y)), but if I have d of the form, d = {x1:y1, x2:y2, ...}, what is there any trick to get lists x = [x1, x2, ...] and y = [y1, y2, ...] AE x = d.keys() AE y = [d[e] for d in x] AE y = d.values() might also work, but I am not sure whether d.keys() and AE d.values() are guaranteed to use the same order. Yes, they are if the dictionary is not changed in the meantime (not even inserting and removing the same thing). See the library documentation, section dict. Still I'd like to see an application where this really matters (that keys() and values() match in order) Just as an example, if you are using a third-party library function that demands side-by-side inputs in respective lists, you could make use of it. That's not a good interface, IMHO, but if you have to use such a library, and you want to supply key-value pairs, then you can use keys and values seperately. populate_database(d.keys(),d.values()) Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
Inverse of dict(zip(x,y))
Hi all, This might be trivial ... Can someone suggest a easy method to do the inverse of dict(zip(x,y)) to get two lists x and y? So, if x and y are two lists, it is easier to make a dictionary using d = dict(zip(x,y)), but if I have d of the form, d = {x1:y1, x2:y2, ...}, what is there any trick to get lists x = [x1, x2, ...] and y = [y1, y2, ...] Cheers, Chaitanya. -- http://mail.python.org/mailman/listinfo/python-list
RE: Inverse of dict(zip(x,y))
Can someone suggest a easy method to do the inverse of dict(zip(x,y)) to get two lists x and y? So, if x and y are two lists, it is easier to make a dictionary using d = dict(zip(x,y)), but if I have d of the form, d = {x1:y1, x2:y2, ...}, what is there any trick to get lists x = [x1, x2, ...] and y = [y1, y2, ...] Cheers, Chaitanya. x = d.keys() y = d.values() Cheers, Drea -- http://mail.python.org/mailman/listinfo/python-list
Re: Inverse of dict(zip(x,y))
lone_eagle icym...@gmail.com writes: So, if x and y are two lists, it is easier to make a dictionary using d = dict(zip(x,y)), but if I have d of the form, d = {x1:y1, x2:y2, ...}, what is there any trick to get lists x = [x1, x2, ...] and y = [y1, y2, ...] This may be a bit of a mind bender, but: x, y = zip(*d.items()) The trick is that if xys is a list of pairs, then zip(*xys) splits out the pairs, e.g.: zip(*((1,2),(3,4),(5,6))) [(1, 3, 5), (2, 4, 6)] I found that in the python docs somewhere. The mind wobbles. -- http://mail.python.org/mailman/listinfo/python-list
Re: Inverse of dict(zip(x,y))
On Wed, Mar 4, 2009 at 11:02 AM, lone_eagle icym...@gmail.com wrote: Can someone suggest a easy method to do the inverse of dict(zip(x,y)) to get two lists x and y? So, if x and y are two lists, it is easier to make a dictionary using d = dict(zip(x,y)), but if I have d of the form, d = {x1:y1, x2:y2, ...}, what is there any trick to get lists x = [x1, x2, ...] and y = [y1, y2, ...] x = d.keys() y = [d[e] for d in x] y = d.values() might also work, but I am not sure whether d.keys() and d.values() are guaranteed to use the same order. -- André Engels, andreeng...@gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Inverse of dict(zip(x,y))
On Mar 4, 11:06 am, Paul Rubin http://phr...@nospam.invalid wrote: lone_eagle icym...@gmail.com writes: So, if x and y are two lists, it is easier to make a dictionary using d = dict(zip(x,y)), but if I have d of the form, d = {x1:y1, x2:y2, ...}, what is there any trick to get lists x = [x1, x2, ...] and y = [y1, y2, ...] This may be a bit of a mind bender, but: x, y = zip(*d.items()) The trick is that if xys is a list of pairs, then zip(*xys) splits out the pairs, e.g.: zip(*((1,2),(3,4),(5,6))) [(1, 3, 5), (2, 4, 6)] I found that in the python docs somewhere. The mind wobbles. That was cool!! I just checked the python documentation, but the below note worries me a bit!! Keys and values are listed in an arbitrary order which is non- random, varies across Python implementations, and depends on the dictionary’s history of insertions and deletions. I hope it does not mean that the key-value mapping is not guaranteed, but only that the order of the [key: value] pairs would change. Which one is right? Cheers, Chaitanya -- http://mail.python.org/mailman/listinfo/python-list
RE: Inverse of dict(zip(x,y))
Andreas Tawn wrote: Can someone suggest a easy method to do the inverse of dict(zip(x,y)) to get two lists x and y? So, if x and y are two lists, it is easier to make a dictionary using d = dict(zip(x,y)), but if I have d of the form, d = {x1:y1, x2:y2, ...}, what is there any trick to get lists x = [x1, x2, ...] and y = [y1, y2, ...] Cheers, Chaitanya. x = d.keys() y = d.values() But be aware that you lose order and of course duplicate keys: d = dict(zip(abca, xyzt)) d.keys() ['a', 'c', 'b'] d.values() ['t', 'z', 'y'] See also the note for the dict.items() method at http://docs.python.org/library/stdtypes.html#mapping-types-dict Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: Inverse of dict(zip(x,y))
lone_eagle wrote: Hi all, This might be trivial ... Can someone suggest a easy method to do the inverse of dict(zip(x,y)) to get two lists x and y? So, if x and y are two lists, it is easier to make a dictionary using d = dict(zip(x,y)), but if I have d of the form, d = {x1:y1, x2:y2, ...}, what is there any trick to get lists x = [x1, x2, ...] and y = [y1, y2, ...] x,y=zip(d.items()) Cheers Tino smime.p7s Description: S/MIME Cryptographic Signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Inverse of dict(zip(x,y))
psykeedelik icym...@gmail.com writes: Keys and values are listed in an arbitrary order which is non- random, varies across Python implementations, and depends on the dictionary’s history of insertions and deletions. I hope it does not mean that the key-value mapping is not guaranteed, but only that the order of the [key: value] pairs would change. Which one is right? The latter, of course. The documentation is trying to warn you not to rely on the order of returned pairs, not imply that the items() method is unusable. -- http://mail.python.org/mailman/listinfo/python-list
Re: Inverse of dict(zip(x,y))
Andre Engels wrote: y = d.values() might also work, but I am not sure whether d.keys() and d.values() are guaranteed to use the same order. If they were called immediately after each other I think they should, but better not rely on it. -- http://mail.python.org/mailman/listinfo/python-list
Re: Inverse of dict(zip(x,y))
Lie Ryan wrote: Andre Engels wrote: y = d.values() might also work, but I am not sure whether d.keys() and d.values() are guaranteed to use the same order. If they were called immediately after each other I think they should, but better not rely on it. otoh, I could not think of any use having the two resulting sequences relate to each other. So in fact it doesn't matter if order is preserved or not. Regards Tino smime.p7s Description: S/MIME Cryptographic Signature -- http://mail.python.org/mailman/listinfo/python-list
RE: Inverse of dict(zip(x,y))
So, if x and y are two lists, it is easier to make a dictionary using d = dict(zip(x,y)), but if I have d of the form, d = {x1:y1, x2:y2, ...}, what is there any trick to get lists x = [x1, x2, ...] and y = [y1, y2, ...] Cheers, Chaitanya. x = d.keys() y = d.values() But be aware that you lose order and of course duplicate keys: True, but that's a result of creating the dictionary, not extracting the keys and values later. d = dict(zip(abca, xyzt)) d {'a': 't', 'c': 'z', 'b': 'y'} -- http://mail.python.org/mailman/listinfo/python-list
Re: Inverse of dict(zip(x,y))
Andre Engels andreeng...@gmail.com (AE) wrote: AE On Wed, Mar 4, 2009 at 11:02 AM, lone_eagle icym...@gmail.com wrote: Can someone suggest a easy method to do the inverse of dict(zip(x,y)) to get two lists x and y? So, if x and y are two lists, it is easier to make a dictionary using d = dict(zip(x,y)), but if I have d of the form, d = {x1:y1, x2:y2, ...}, what is there any trick to get lists x = [x1, x2, ...] and y = [y1, y2, ...] AE x = d.keys() AE y = [d[e] for d in x] AE y = d.values() might also work, but I am not sure whether d.keys() and AE d.values() are guaranteed to use the same order. Yes, they are if the dictionary is not changed in the meantime (not even inserting and removing the same thing). See the library documentation, section dict. -- Piet van Oostrum p...@cs.uu.nl URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: p...@vanoostrum.org -- http://mail.python.org/mailman/listinfo/python-list
Re: Inverse of dict(zip(x,y))
Having a look at python documentation I found: zip() in conjunction with the * operator can be used to unzip a list: x = [1, 2, 3] y = [4, 5, 6] zipped = zip(x, y) zipped [(1, 4), (2, 5), (3, 6)] x2, y2 = zip(*zipped) x == x2, y == y2 True So, x2, y2 = zip(*d.items()) should fix your problem -- http://mail.python.org/mailman/listinfo/python-list
Re: Inverse of dict(zip(x,y))
Lorenzo wrote: zip() in conjunction with the * operator can be used to unzip a list: That's because zip is the inverse operation of zip. I remember someone saying that zip's typical name is transpose (like in matrix transpose). a == zip(*zip(*a)) nitpick * in argument unpacking is not an operator /nitpick -- http://mail.python.org/mailman/listinfo/python-list
Re: Inverse of dict(zip(x,y))
On Mar 4, 5:33 am, Lie Ryan lie.1...@gmail.com wrote: Andre Engels wrote: y = d.values() might also work, but I am not sure whether d.keys() and d.values() are guaranteed to use the same order. If they were called immediately after each other I think they should, but better not rely on it. I think this is why a solution based on d.items() is preferable - it returns each key-value pair together. -- Paul -- http://mail.python.org/mailman/listinfo/python-list
Re: Inverse of dict(zip(x,y))
On Mar 4, 5:33 am, Lie Ryan lie.1...@gmail.com wrote: Andre Engels wrote: y = d.values() might also work, but I am not sure whether d.keys() and d.values() are guaranteed to use the same order. If they were called immediately after each other I think they should, but better not rely on it. Also, it offends my efficiency/performance sensibilities to use two separate calls to iterate over the dict twice, when there is a perfectly good equivalent that is just as readable and iterates only once. -- http://mail.python.org/mailman/listinfo/python-list
Re: Inverse of dict(zip(x,y))
Andre Engels andreengels at gmail.com writes: y = d.values() might also work, but I am not sure whether d.keys() and d.values() are guaranteed to use the same order. They are for the builtin dictionary type, but that requirement does not extend to any other mapping type. (It's not a requirement of the Mapping interface.) -- http://mail.python.org/mailman/listinfo/python-list
Re: Inverse of dict(zip(x,y))
On Wed, 04 Mar 2009 08:00:14 -0800, Paul McGuire wrote: On Mar 4, 5:33 am, Lie Ryan lie.1...@gmail.com wrote: Andre Engels wrote: y = d.values() might also work, but I am not sure whether d.keys() and d.values() are guaranteed to use the same order. If they were called immediately after each other I think they should, but better not rely on it. Also, it offends my efficiency/performance sensibilities to use two separate calls to iterate over the dict twice, when there is a perfectly good equivalent that is just as readable and iterates only once. Sure, but if you want two lists, as the OP asked for, then you have to iterate over it twice either way: # method 1: keys = dict.keys() values = dict.values() # method 2: keys, values = zip(*dict.items()) First you iterate over the dict to get the items, then you iterate over the items to split into two lists. Anyone want to take bets on which is faster? from timeit import Timer d = {'a':1, 'b':2, 'c':3, 'z':26} Timer('d.keys();d.values()', 'from __main__ import d').repeat() [1.1103789806365967, 0.90148496627807617, 0.9004051685333252] Timer('zip(*d.items())', 'from __main__ import d').repeat() [2.1786351203918457, 2.0767219066619873, 2.076124906539917] For a small dict, the zip solution is about twice as slow. What about for a bigger dict? D = dict((n, n*2+1) for n in xrange(-20, 100)) Timer('D.keys();D.values()', \ ... 'from __main__ import D').repeat(number=100) [9.2809889316558838, 9.150738000869751, 9.2292399406433105] Timer('zip(*D.items())', \ ... 'from __main__ import D').repeat(number=100) [63.850389957427979, 55.749162912368774, 61.448837041854858] Well, I think this is clear evidence that the zip solution is a pessimation, not an optimization. That's what I love about Python -- my intuition about what code is faster is so often wrong! [only half sarcastic...] -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Inverse of dict(zip(x,y))
Steven D'Aprano ste...@remove.this.cybersource.com.au writes: Sure, but if you want two lists, as the OP asked for, then you have to iterate over it twice either way: # method 1: keys = dict.keys() values = dict.values() # method 2: keys, values = zip(*dict.items()) First you iterate over the dict to get the items, then you iterate over the items to split into two lists. Anyone want to take bets on which is faster? The first way involves iterating over the dict items twice. The second way iterates over the dict items just once, copying them to another place; it then iterates over the copy. -- http://mail.python.org/mailman/listinfo/python-list