Re: Dictionary sorting

2011-11-04 Thread Ben Finney
Hrvoje Niksic writes: > Ben Finney writes: > > > Tim Chase writes: > >> Does this "never trust it" hold even for two consecutive iterations > >> over an unchanged dict? I didn't see anything in the docs[1] to make > >> such a claim, > > > > Exactly. > > This is false. The docs say: > > If

Re: Dictionary sorting

2011-11-04 Thread Hrvoje Niksic
Ben Finney writes: > Tim Chase writes: > >> On 11/03/11 16:36, Terry Reedy wrote: >> > CPython iterates (and prints) dict items in their arbitrary internal >> > hash table order, which depends on the number and entry order of the >> > items. It is a bug to depend on that arbitrary order in any w

Re: Dictionary sorting

2011-11-03 Thread Ben Finney
Tim Chase writes: > On 11/03/11 16:36, Terry Reedy wrote: > > CPython iterates (and prints) dict items in their arbitrary internal > > hash table order, which depends on the number and entry order of the > > items. It is a bug to depend on that arbitrary order in any way. > > Does this "never tru

Re: Dictionary sorting

2011-11-03 Thread Chris Angelico
On Fri, Nov 4, 2011 at 10:01 AM, Tim Chase wrote: > list1 = list(d.iterkeys()) >  list2 = list(d.iterkeys()) >  assert list1 == list2 > There is such a guarantee in Python 2. From http://docs.python.org/library/stdtypes.html: "If items(), keys(), values(), iteritems(), iterkeys(), and itervalues

Re: Dictionary sorting

2011-11-03 Thread Tim Chase
On 11/03/11 16:36, Terry Reedy wrote: > Is there a way to not sort them and leave the order as is? CPython iterates (and prints) dict items in their arbitrary internal hash table order, which depends on the number and entry order of the items. It is a bug to depend on that arbitrary order in

Re: Dictionary sorting

2011-11-03 Thread Terry Reedy
On 11/3/2011 2:46 PM, Scott Ware wrote: Python newbie here. So, when creating dictionaries, I am noticing that each time I print it out, that its not in the same order as when I typed it in. They seem to be getting sorted somehow. No, the entries are not being sorted at all. > Is there a way t

Re: Dictionary sorting

2011-11-03 Thread insomnia
Moreover, for on-the-fly ordering you can consider to use sorted() on yourdict.keys(), like: for k in sorted(yourdict.keys()): print k, yourdict[k] I think it has no side effects, except that the orderering can be slow on huge data sets, and that you need to call it every time after updatin

Re: Dictionary sorting

2011-11-03 Thread Chris Kaynor
Note that there are a number of recipes available for free online, and if you are using a newer version of Python (2.7 or higher), the collections module includes an OrderedDict class (http://docs.python.org/library/collections.html#collections.OrderedDict - this also include a library for Python 2

Re: Dictionary sorting

2011-11-03 Thread Scott Ware
Great! Thanks, John for the quick response! -- http://mail.python.org/mailman/listinfo/python-list

Re: Dictionary sorting

2011-11-03 Thread John Gordon
In <16245908.783.1320346014867.JavaMail.geo-discussion-forums@yqhd1> Scott Ware writes: > Python newbie here. So, when creating dictionaries, I am noticing that > each time I print it out, that its not in the same order as when I typed > it in. They seem to be getting sorted somehow. Is there a

Dictionary sorting

2011-11-03 Thread Scott Ware
Python newbie here. So, when creating dictionaries, I am noticing that each time I print it out, that its not in the same order as when I typed it in. They seem to be getting sorted somehow. Is there a way to not sort them and leave the order as is? Thanks! -- http://mail.python.org/mailman/li

Re: Nested Dictionary Sorting

2006-11-06 Thread Peter Otten
Sam Loxton wrote: > I am fairly new to the python language and am trying to sort a nested > Dictionary of a Dictionary which I wish to sort by value. The dictionary > does not have to be restructured as I only need it sorted in this way > for printing purposes. > > The following is an example of

Nested Dictionary Sorting

2006-11-06 Thread Sam Loxton
Hi, I am fairly new to the python language and am trying to sort a nested Dictionary of a Dictionary which I wish to sort by value. The dictionary does not have to be restructured as I only need it sorted in this way for printing purposes. The following is an example of my Dictionary printed w

Re: Dictionary sorting problem

2005-09-17 Thread Bengt Richter
On 17 Sep 2005 11:01:41 GMT, Duncan Booth <[EMAIL PROTECTED]> wrote: >Bengt Richter wrote: > >> or tell sorted what to do ;-) >> >> >>> original= { >> ... 'hello':135, >> ... 'goodbye':30, >> ... 'lucy':4, >> ... 'sky':55, >> ... 'diamonds':239843, >> ... 'yesterday':4 } >> >>> list(sorted(

Re: Dictionary sorting problem

2005-09-17 Thread Duncan Booth
Bengt Richter wrote: > or tell sorted what to do ;-) > > >>> original= { > ... 'hello':135, > ... 'goodbye':30, > ... 'lucy':4, > ... 'sky':55, > ... 'diamonds':239843, > ... 'yesterday':4 } > >>> list(sorted(original.iteritems(), None, lambda t:t[1], True)) > [('diamonds', 239843), ('hell

Re: Dictionary sorting problem

2005-09-16 Thread Bengt Richter
On Fri, 16 Sep 2005 21:42:40 +0200, Irmen de Jong <[EMAIL PROTECTED]> wrote: >JerryB wrote: >> Hi, >> I have a dictionary for counting ocurrences of strings in a document. >> The dictionary looks like this: >> >> 'hello':135 >> 'goodbye':30 >> 'lucy':4 >> 'sky':55 >> 'diamonds':239843 >> 'yesterd

Re: Dictionary sorting problem

2005-09-16 Thread Jason Mobarak
You can't sort dictionaries (as implemented by hash tables), they are unordered data types, so by definition there's no way to force an order on them. http://en.wikipedia.org/wiki/Hash_tables -- http://mail.python.org/mailman/listinfo/python-list

Re: Dictionary sorting problem

2005-09-16 Thread Irmen de Jong
JerryB wrote: > Hi, > I have a dictionary for counting ocurrences of strings in a document. > The dictionary looks like this: > > 'hello':135 > 'goodbye':30 > 'lucy':4 > 'sky':55 > 'diamonds':239843 > 'yesterday':4 > > I want to print the dictionary so I see most common words first: > > 'diamond

Dictionary sorting problem

2005-09-16 Thread JerryB
Hi, I have a dictionary for counting ocurrences of strings in a document. The dictionary looks like this: 'hello':135 'goodbye':30 'lucy':4 'sky':55 'diamonds':239843 'yesterday':4 I want to print the dictionary so I see most common words first: 'diamonds':239843 'hello':135 'sky':55 'goodbye':3

Re: dictionary: sorting the values preserving the order

2005-04-01 Thread Terry Reedy
"Rakesh" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > This gets a list sorted by the keys. That is all you *can* get (with the list keys being the dict values). > How would I get a revised dictionary sorted by its values. You can't. A dictionary is not sorted. The print orde

Re: dictionary: sorting the values preserving the order

2005-04-01 Thread Luis M. Gonzalez
Another alternative: d1 = {'a':4,'b':5,'c':1,'d':2,'e':3­} il=[(v,k) for k,v in d1.items()] il.sort() -- http://mail.python.org/mailman/listinfo/python-list

Re: dictionary: sorting the values preserving the order

2005-04-01 Thread Ron_Adam
On 31 Mar 2005 22:40:53 -0800, "Rakesh" <[EMAIL PROTECTED]> wrote: >Hi, > For a particular problem of mine, I want to sort pairs >by its value. > >Eg: > >Input: > >A, 4 >B, 5 >C, 1 >D, 2 >E, 3 > >I would like the output to be: > >C >D >E >A >B > >i.e. I would like to get the keys in the sorted o

Re: dictionary: sorting the values preserving the order

2005-04-01 Thread Satchidanand Haridas
Rakesh wrote: Hi, For a particular problem of mine, I want to sort pairs by its value. Eg: Input: A, 4 B, 5 C, 1 D, 2 E, 3 I would like the output to be: C D E A B the following code does that: >>> d1 = {'a':4,'b':5,'c':1,'d':2,'e':3} >>> i1 = [ (d1[i], i) for i in d1.keys() ] >>> i1.sort() >>

Re: dictionary: sorting the values preserving the order

2005-03-31 Thread Vikram
hi, assuming your key-value relationship is one-to-one then as a simple first pass you can simply initialize d1={} and for i in d.keys(): d1[d[i]] = i and pass d1 to your sortedDictValue3 function, no? thanks, Vikram On 31 Mar 2005, Rakesh wrote: > Hi, > For a partic

dictionary: sorting the values preserving the order

2005-03-31 Thread Rakesh
Hi, For a particular problem of mine, I want to sort pairs by its value. Eg: Input: A, 4 B, 5 C, 1 D, 2 E, 3 I would like the output to be: C D E A B i.e. I would like to get the keys in the sorted order of values. I did google around a little bit. One solution to a similar problem sugges