Re: Inverse of dict(zip(x,y))

2009-03-06 Thread Tino Wildenhain
Andre Engels wrote: On Thu, Mar 5, 2009 at 7:46 PM, Andre Engels 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 data

Re: Inverse of dict(zip(x,y))

2009-03-06 Thread Tino Wildenhain
Hi, psykeedelik wrote: On Mar 5, 6:01 pm, Tino Wildenhain wrote: Piet van Oostrum wrote: Andre Engels (AE) wrote: AE> On Wed, Mar 4, 2009 at 11:02 AM, lone_eagle 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 l

Re: Inverse of dict(zip(x,y))

2009-03-06 Thread Tino Wildenhain
Andre Engels wrote: On Thu, Mar 5, 2009 at 6:01 PM, Tino Wildenhain 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 somethi

Re: Inverse of dict(zip(x,y))

2009-03-05 Thread Carl Banks
On Mar 5, 9:01 am, Tino Wildenhain wrote: > Piet van Oostrum wrote: > >> Andre Engels (AE) wrote: > > >> AE> On Wed, Mar 4, 2009 at 11:02 AM, lone_eagle 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

Re: Inverse of dict(zip(x,y))

2009-03-05 Thread Andre Engels
On Thu, Mar 5, 2009 at 7:46 PM, Andre Engels 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/mail

Re: Inverse of dict(zip(x,y))

2009-03-05 Thread Andre Engels
On Thu, Mar 5, 2009 at 7:07 PM, psykeedelik 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

Re: Inverse of dict(zip(x,y))

2009-03-05 Thread psykeedelik
On Mar 5, 6:01 pm, Tino Wildenhain wrote: > Piet van Oostrum wrote: > >> Andre Engels (AE) wrote: > > >> AE> On Wed, Mar 4, 2009 at 11:02 AM, lone_eagle 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

Re: Inverse of dict(zip(x,y))

2009-03-05 Thread Andre Engels
On Thu, Mar 5, 2009 at 6:01 PM, Tino Wildenhain 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 don

Re: Inverse of dict(zip(x,y))

2009-03-05 Thread Tino Wildenhain
Piet van Oostrum wrote: Andre Engels (AE) wrote: AE> On Wed, Mar 4, 2009 at 11:02 AM, lone_eagle 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)),

Re: Inverse of dict(zip(x,y))

2009-03-04 Thread Paul Rubin
Steven D'Aprano 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

Re: Inverse of dict(zip(x,y))

2009-03-04 Thread Steven D'Aprano
On Wed, 04 Mar 2009 08:00:14 -0800, Paul McGuire wrote: > On Mar 4, 5:33 am, 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 ot

Re: Inverse of dict(zip(x,y))

2009-03-04 Thread Benjamin Peterson
Andre Engels 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 Mapp

Re: Inverse of dict(zip(x,y))

2009-03-04 Thread Paul McGuire
On Mar 4, 5:33 am, 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. Also, it

Re: Inverse of dict(zip(x,y))

2009-03-04 Thread Paul McGuire
On Mar 4, 5:33 am, 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. I think t

Re: Inverse of dict(zip(x,y))

2009-03-04 Thread Lie Ryan
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)) * in argument unpacking is not an operat

Re: Inverse of dict(zip(x,y))

2009-03-04 Thread Lorenzo
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())

Re: Inverse of dict(zip(x,y))

2009-03-04 Thread Piet van Oostrum
> Andre Engels (AE) wrote: >AE> On Wed, Mar 4, 2009 at 11:02 AM, lone_eagle 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)), bu

RE: Inverse of dict(zip(x,y))

2009-03-04 Thread Andreas Tawn
>>>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()

Re: Inverse of dict(zip(x,y))

2009-03-04 Thread Tino Wildenhain
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 hav

Re: Inverse of dict(zip(x,y))

2009-03-04 Thread Lie Ryan
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))

2009-03-04 Thread Hrvoje Niksic
psykeedelik 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 on

Re: Inverse of dict(zip(x,y))

2009-03-04 Thread Tino Wildenhain
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, ...}, wh

RE: Inverse of dict(zip(x,y))

2009-03-04 Thread Peter Otten
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 t

Re: Inverse of dict(zip(x,y))

2009-03-04 Thread psykeedelik
On Mar 4, 11:06 am, Paul Rubin wrote: > lone_eagle 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, ..

Re: Inverse of dict(zip(x,y))

2009-03-04 Thread Andre Engels
On Wed, Mar 4, 2009 at 11:02 AM, lone_eagle 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,

Re: Inverse of dict(zip(x,y))

2009-03-04 Thread Paul Rubin
lone_eagle 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

RE: Inverse of dict(zip(x,y))

2009-03-04 Thread Andreas Tawn
>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, ..