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
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
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
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
Τη Δευτέρα, 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
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() ):
=
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.
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
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
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
Trying this:
months = { 'Ιανουάριος':1, 'Φεβρουάριος':2, 'Μάρτιος':3, 'Απρίλιος':4,
'Μάϊος':5, 'Ιούνιος':6, \
'Ιούλιος':7, 'Αύγουστος':8, 'Σεπτέμβριος':9, 'Οκτώβριος':10,
'Νοέμβριος':11, 'Δεκέμβριος':12 }
for key in sorted( months.values() ):
print('''
%s
Τη Δευτέρα, 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
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
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
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
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():
>
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
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
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),
> 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.
> 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
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
"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
--- 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
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)
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://
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
27 matches
Mail list logo