Re: Sorting a set works, sorting a dictionary fails ?

2013-06-11 Thread Larry Hudson
On 06/10/2013 01:29 AM, Νικόλαος Κούρας wrote: Trying this: months = { 'Ιανουάριος':1, 'Φεβρουάριος':2, 'Μάρτιος':3, 'Απρίλιος':4, 'Μάϊος':5, 'Ιούνιος':6, \ 'Ιούλιος':7, 'Αύγουστος':8, 'Σεπτέμβριος':9, 'Οκτώβριος':10, 'Νοέμβριος':11, 'Δεκέμβριος':12 } for key in sorted( months.valu

Re: Sorting a set works, sorting a dictionary fails ?

2013-06-10 Thread Denis McMahon
On Mon, 10 Jun 2013 03:42:38 -0700, Νικόλαος Κούρας wrote: > for key in sorted( months.values() ): > print(''' >%s > ''' % (months[key], key) ) > == > > please tell me Uli why this dont work as expected to. Because inside the for loop, your value 'key' i

Re: Sorting a set works, sorting a dictionary fails ?

2013-06-10 Thread Ulrich Eckhardt
Am 10.06.2013 11:48, schrieb Νικόλαος Κούρας: After many tried this did the job: for key in sorted(months.items(),key=lambda num : num[1]): print(''' %s ''' % (key[1], key[0]) ) This code is still sending a misleading message. What you are referring to as "ke

Re: Sorting a set works, sorting a dictionary fails ?

2013-06-10 Thread Steven D'Aprano
On Mon, 10 Jun 2013 03:42:38 -0700, Νικόλαος Κούρας wrote: > for key in sorted( months.values() ): > please tell me Uli why this dont work as expected to. Because values are not keys. You are looking at the values, and trying to use them as keys. months = {'Φεβρουάριος':2, 'Ιανουάριος':1} prin

Re: Sorting a set works, sorting a dictionary fails ?

2013-06-10 Thread Νικόλαος Κούρας
Τη Δευτέρα, 10 Ιουνίου 2013 12:40:01 μ.μ. UTC+3, ο χρήστης Ulrich Eckhardt έγραψε: > Am 10.06.2013 10:29, schrieb Νικόλαος Κούρας: > > > for key in sorted( months.values() ): > >^^^ ^^ > > > > > KeyError 1 ??!! All i did was to tell python to sort the dictionary

Re: Sorting a set works, sorting a dictionary fails ?

2013-06-10 Thread Ulrich Eckhardt
Am 10.06.2013 10:04, schrieb Νικόλαος Κούρας: months = { 'Ιανουάριος':1, 'Φεβρουάριος':2, 'Μάρτιος':3, 'Απρίλιος':4, 'Μάϊος':5, 'Ιούνιος':6, \ 'Ιούλιος':7, 'Αύγουστος':8, 'Σεπτέμβριος':9, 'Οκτώβριος':10, 'Νοέμβριος':11, 'Δεκέμβριος':12 } for key in sorted( months.keys() ): =

Re: Sorting a set works, sorting a dictionary fails ?

2013-06-10 Thread Ulrich Eckhardt
Am 10.06.2013 10:29, schrieb Νικόλαος Κούρας: for key in sorted( months.values() ): ^^^ ^^ KeyError 1 ??!! All i did was to tell python to sort the dictionary values, which are just integers. ...and which you then proceed to use as key, which is obviously wrong.

Re: Sorting a set works, sorting a dictionary fails ?

2013-06-10 Thread Fábio Santos
On 10 Jun 2013 10:53, "Νικόλαος Κούρας" wrote: > > After many tried this did the job: > > for key in sorted(months.items(),key=lambda num : num[1]): > print(''' > %s > ''' % (key[1], key[0]) ) > > > but its really frustrating not being able to: > > for key in sort

Re: Sorting a set works, sorting a dictionary fails ?

2013-06-10 Thread Νικόλαος Κούρας
After many tried this did the job: for key in sorted(months.items(),key=lambda num : num[1]): print(''' %s ''' % (key[1], key[0]) ) but its really frustrating not being able to: for key in sorted( months.values() ): print(''' %s

Re: Sorting a set works, sorting a dictionary fails ?

2013-06-10 Thread Fábio Santos
On 10 Jun 2013 09:34, "Νικόλαος Κούρας" wrote: > > Trying this: > > months = { 'Ιανουάριος':1, 'Φεβρουάριος':2, 'Μάρτιος':3, 'Απρίλιος':4, 'Μάϊος':5, 'Ιούνιος':6, \ >'Ιούλιος':7, 'Αύγουστος':8, 'Σεπτέμβριος':9, 'Οκτώβριος':10, 'Νοέμβριος':11, 'Δεκέμβριος':12 } > > for key in sorted( mo

Re: Sorting a set works, sorting a dictionary fails ?

2013-06-10 Thread Νικόλαος Κούρας
Trying this: months = { 'Ιανουάριος':1, 'Φεβρουάριος':2, 'Μάρτιος':3, 'Απρίλιος':4, 'Μάϊος':5, 'Ιούνιος':6, \ 'Ιούλιος':7, 'Αύγουστος':8, 'Σεπτέμβριος':9, 'Οκτώβριος':10, 'Νοέμβριος':11, 'Δεκέμβριος':12 } for key in sorted( months.values() ): print(''' %s

Re: Sorting a set works, sorting a dictionary fails ?

2013-06-10 Thread Νικόλαος Κούρας
Τη Δευτέρα, 10 Ιουνίου 2013 11:16:37 π.μ. UTC+3, ο χρήστης Νικόλαος Κούρας έγραψε: > What if i wanted to sort it out if alphabetically and not by the values? > > > > Thsi worked: > > > > for item in sorted(months.items(),key=lambda num : num[1]): > > > > but this failed: > > > > for it

Re: Sorting a set works, sorting a dictionary fails ?

2013-06-10 Thread Νικόλαος Κούρας
What if i wanted to sort it out if alphabetically and not by the values? Thsi worked: for item in sorted(months.items(),key=lambda num : num[1]): but this failed: for item in sorted(months.items()): why? -- http://mail.python.org/mailman/listinfo/python-list

Re: Sorting a dictionary

2009-05-15 Thread J. Cliff Dyer
On Fri, 2009-05-15 at 09:57 -0700, Tobiah wrote: > On Tue, 12 May 2009 14:17:54 +0200, Jaime Fernandez del Rio wrote: > > > This one I think I know... Try with: > > > > for k in sorted(word_count) : > > print k,"=",word_count[k] > > > > You need to do the sorting before iterating over the ke

Re: Sorting a dictionary

2009-05-15 Thread Tobiah
On Tue, 12 May 2009 14:17:54 +0200, Jaime Fernandez del Rio wrote: > This one I think I know... Try with: > > for k in sorted(word_count) : > print k,"=",word_count[k] > > You need to do the sorting before iterating over the keys... Isn't that what's happening here? I read this as the 'sor

Re: Sorting a dictionary

2009-05-12 Thread Andre Engels
On Tue, May 12, 2009 at 1:54 PM, Ronn Ross wrote: > I'm attempting to sort for the results of a dictionary. I would like to > short by the 'key' in ascending order. I have already made several attempts > using: sorted() and .sort(). > Here is my loop: >     for key,value in word_count.items(): >  

Re: Sorting a dictionary

2009-05-12 Thread Jaime Fernandez del Rio
This one I think I know... Try with: for k in sorted(word_count) : print k,"=",word_count[k] You need to do the sorting before iterating over the keys... Jaime On Tue, May 12, 2009 at 1:54 PM, Ronn Ross wrote: > I'm attempting to sort for the results of a dictionary. I would like to > shor

Sorting a dictionary

2009-05-12 Thread Ronn Ross
I'm attempting to sort for the results of a dictionary. I would like to short by the 'key' in ascending order. I have already made several attempts using: sorted() and .sort(). Here is my loop: for key,value in word_count.items(): print sorted(key), "=", value Thanks -- http://mail.py

Re: sorting a dictionary?

2005-03-23 Thread Bengt Richter
On Wed, 23 Mar 2005 06:27:08 GMT, Dennis Lee Bieber <[EMAIL PROTECTED]> wrote: >On Tue, 22 Mar 2005 14:13:00 +1200, "Tony Meyer" <[EMAIL PROTECTED]> >declaimed the following in comp.lang.python: > >> >> There are no doubt faster and cleverer ways to do this (and ways that don't >> use a lambda),

RE: sorting a dictionary?

2005-03-22 Thread Tony Meyer
> mydict = {'the':358, 'they':29, 'went':7, 'said':65} > > Is there an easy to sort this dictionary and get a > list like the following (in decreasing order)? > > the 358 > said 65 > they 29 > went 7 The dictionary's keys and values are lists, which can be sorted, so you can just use that.

RE: sorting a dictionary?

2005-03-22 Thread Tony Meyer
> Thank you scott, but 'sorted' itself is not a python > library function. So I assume by > "sorted(mydict.items()", you meant the object of > mydict.items() which has been sorted, right? It's a builtin as of Python 2.4: Python 2.4.1c1 (#63, Mar 10 2005, 10:36:41) [MSC v.1310 32 bit (Intel)] on

Re: sorting a dictionary?

2005-03-21 Thread Scott David Daniels
Anthony Liu wrote: --- Scott David Daniels <[EMAIL PROTECTED]> wrote: def sortkey((word, frequency)): return -frequency, word for word_freq in sorted(mydict.items(), key=sortkey): print '%-6s %s' % word_freq Thank you scott, but 'sorted' itself is not a python library functi

Re: sorting a dictionary?

2005-03-21 Thread George Sakkis
"Anthony Liu" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > > --- Scott David Daniels <[EMAIL PROTECTED]> wrote: > > Anthony Liu wrote: > > > mydict = {'the':358, 'they':29, 'went':7, > > 'said':65} > > > Is there an easy to sort this dictionary and get a > > > list like the followi

Re: sorting a dictionary?

2005-03-21 Thread Anthony Liu
--- Scott David Daniels <[EMAIL PROTECTED]> wrote: > Anthony Liu wrote: > > mydict = {'the':358, 'they':29, 'went':7, > 'said':65} > > Is there an easy to sort this dictionary and get a > > list like the following (in decreasing order)? > Yes. > > def sortkey((word, frequency)): > r

Re: sorting a dictionary?

2005-03-21 Thread Scott David Daniels
Anthony Liu wrote: mydict = {'the':358, 'they':29, 'went':7, 'said':65} Is there an easy to sort this dictionary and get a list like the following (in decreasing order)? Yes. def sortkey((word, frequency)): return -frequency, word for word_freq in sorted(mydict.items(), key=sortkey)

Re: sorting a dictionary?

2005-03-21 Thread M.E.Farmer
This comes up so often on this list that there are many examples and recipes. You are looking for a dictionary sort by value. search strategy: Python sorting dictionary 44,000+ hits on google. First entry: http://aspn.activestate.com/ASPN/Python/Cookbook/Recipe/52306 hth, M.E.Farmer -- http://

sorting a dictionary?

2005-03-21 Thread Anthony Liu
I have a dictionary which contains some words and their frequency in a short novel. It looks like this: mydict = {'the':358, 'they':29, 'went':7, 'said':65} Is there an easy to sort this dictionary and get a list like the following (in decreasing order)? the 358 said 65 they 29 went 7 Tha