Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #47529 > unrolled thread
| Started by | Νικόλαος Κούρας <nikos.gr33k@gmail.com> |
|---|---|
| First post | 2013-06-10 01:04 -0700 |
| Last post | 2013-06-10 11:19 +0200 |
| Articles | 16 — 6 participants |
Back to article view | Back to comp.lang.python
Sorting a set works, sorting a dictionary fails ? Νικόλαος Κούρας <nikos.gr33k@gmail.com> - 2013-06-10 01:04 -0700
Re: Sorting a set works, sorting a dictionary fails ? Νικόλαος Κούρας <nikos.gr33k@gmail.com> - 2013-06-10 01:16 -0700
Re: Sorting a set works, sorting a dictionary fails ? Νικόλαος Κούρας <nikos.gr33k@gmail.com> - 2013-06-10 01:18 -0700
Re: Sorting a set works, sorting a dictionary fails ? Νικόλαος Κούρας <nikos.gr33k@gmail.com> - 2013-06-10 01:29 -0700
Re: Sorting a set works, sorting a dictionary fails ? Fábio Santos <fabiosantosart@gmail.com> - 2013-06-10 10:27 +0100
Re: Sorting a set works, sorting a dictionary fails ? Νικόλαος Κούρας <nikos.gr33k@gmail.com> - 2013-06-10 02:32 -0700
Re: Sorting a set works, sorting a dictionary fails ? Νικόλαος Κούρας <nikos.gr33k@gmail.com> - 2013-06-10 02:48 -0700
Re: Sorting a set works, sorting a dictionary fails ? Fábio Santos <fabiosantosart@gmail.com> - 2013-06-10 11:13 +0100
Re: Sorting a set works, sorting a dictionary fails ? Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-06-10 15:11 +0200
Re: Sorting a set works, sorting a dictionary fails ? Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-06-10 11:40 +0200
Re: Sorting a set works, sorting a dictionary fails ? Νικόλαος Κούρας <nikos.gr33k@gmail.com> - 2013-06-10 03:20 -0700
Re: Sorting a set works, sorting a dictionary fails ? Νικόλαος Κούρας <nikos.gr33k@gmail.com> - 2013-06-10 03:42 -0700
Re: Sorting a set works, sorting a dictionary fails ? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-10 11:47 +0000
Re: Sorting a set works, sorting a dictionary fails ? Denis McMahon <denismfmcmahon@gmail.com> - 2013-06-11 02:14 +0000
Re: Sorting a set works, sorting a dictionary fails ? Larry Hudson <orgnut@yahoo.com> - 2013-06-11 02:16 -0700
Re: Sorting a set works, sorting a dictionary fails ? Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-06-10 11:19 +0200
| From | Νικόλαος Κούρας <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-06-10 01:04 -0700 |
| Subject | Sorting a set works, sorting a dictionary fails ? |
| Message-ID | <3b252a10-a7e3-40f5-99dc-8afcf72cf997@googlegroups.com> |
sets and dicts are unordered.
================
Yo order the a set i use:
names = set() #the i fill it with data
for name in sorted( names ):
================
Now for the dictionary:
================
months = { 'Ιανουάριος':1, 'Φεβρουάριος':2, 'Μάρτιος':3, 'Απρίλιος':4, 'Μάϊος':5, 'Ιούνιος':6, \
'Ιούλιος':7, 'Αύγουστος':8, 'Σεπτέμβριος':9, 'Οκτώβριος':10, 'Νοέμβριος':11, 'Δεκέμβριος':12 }
for key in sorted( months.keys() ):
================
I'm having trouble ordering a dictionary though.
But how come i was able to sort the set names() and not being able to sort the dictionary keys as well with the sorted function?= i used?
[toc] | [next] | [standalone]
| From | Νικόλαος Κούρας <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-06-10 01:16 -0700 |
| Message-ID | <ce76b1a2-62d0-481d-b045-4bc791a84e37@googlegroups.com> |
| In reply to | #47529 |
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?
[toc] | [prev] | [next] | [standalone]
| From | Νικόλαος Κούρας <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-06-10 01:18 -0700 |
| Message-ID | <a609b6e4-b5a5-4d72-b5e1-c7271efe8a72@googlegroups.com> |
| In reply to | #47531 |
Τη Δευτέρα, 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 item in sorted(months.items()): > > > > why? sorry what i was tryign to say was why not as: for item in sorted(months.values()):
[toc] | [prev] | [next] | [standalone]
| From | Νικόλαος Κούρας <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-06-10 01:29 -0700 |
| Message-ID | <c2eb322d-ac51-4e6a-aa56-3b6b8267ebc4@googlegroups.com> |
| In reply to | #47533 |
Trying this:
months = { 'Ιανουάριος':1, 'Φεβρουάριος':2, 'Μάρτιος':3, 'Απρίλιος':4, 'Μάϊος':5, 'Ιούνιος':6, \
'Ιούλιος':7, 'Αύγουστος':8, 'Σεπτέμβριος':9, 'Οκτώβριος':10, 'Νοέμβριος':11, 'Δεκέμβριος':12 }
for key in sorted( months.values() ):
print('''
<option value="%s"> %s </option>
''' % (months[key], key) )
output this:
[Mon Jun 10 11:25:11 2013] [error] [client 79.103.41.173] File "/home/nikos/public_html/cgi-bin/pelatologio.py", line 310, in <module>, referer: http://superhost.gr/
[Mon Jun 10 11:25:11 2013] [error] [client 79.103.41.173] ''' % (months[key], key) ), referer: http://superhost.gr/
[Mon Jun 10 11:25:11 2013] [error] [client 79.103.41.173] KeyError: 1, referer: http://superhost.gr/
KeyError 1 ??!! All i did was to tell python to sort the dictionary values, which are just integers.
[toc] | [prev] | [next] | [standalone]
| From | Fábio Santos <fabiosantosart@gmail.com> |
|---|---|
| Date | 2013-06-10 10:27 +0100 |
| Message-ID | <mailman.2960.1370856443.3114.python-list@python.org> |
| In reply to | #47534 |
[Multipart message — attachments visible in raw view] — view raw
On 10 Jun 2013 09:34, "Νικόλαος Κούρας" <nikos.gr33k@gmail.com> wrote:
>
> Trying this:
>
> months = { 'Ιανουάριος':1, 'Φεβρουάριος':2, 'Μάρτιος':3, 'Απρίλιος':4,
'Μάϊος':5, 'Ιούνιος':6, \
> 'Ιούλιος':7, 'Αύγουστος':8, 'Σεπτέμβριος':9, 'Οκτώβριος':10,
'Νοέμβριος':11, 'Δεκέμβριος':12 }
>
> for key in sorted( months.values() ):
> print('''
> <option value="%s"> %s </option>
> ''' % (months[key], key) )
>
>
> output this:
>
> [Mon Jun 10 11:25:11 2013] [error] [client 79.103.41.173] File
"/home/nikos/public_html/cgi-bin/pelatologio.py", line 310, in <module>,
referer: http://superhost.gr/
> [Mon Jun 10 11:25:11 2013] [error] [client 79.103.41.173] ''' %
(months[key], key) ), referer: http://superhost.gr/
> [Mon Jun 10 11:25:11 2013] [error] [client 79.103.41.173] KeyError: 1,
referer: http://superhost.gr/
>
> KeyError 1 ??!! All i did was to tell python to sort the dictionary
values, which are just integers.
> --
> http://mail.python.org/mailman/listinfo/python-list
KeyError: 1 means that there is no int(1) key. I think you meant to do "for
key in sorted(yourdict.keys())"
[toc] | [prev] | [next] | [standalone]
| From | Νικόλαος Κούρας <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-06-10 02:32 -0700 |
| Message-ID | <7c0e481e-97dd-4d36-808c-88e3580d8311@googlegroups.com> |
| In reply to | #47536 |
Τη Δευτέρα, 10 Ιουνίου 2013 12:27:14 μ.μ. UTC+3, ο χρήστης Fábio Santos έγραψε:
> On 10 Jun 2013 09:34, "Νικόλαος Κούρας" <nikos...@gmail.com> wrote:
>
> >
>
> > Trying this:
>
> >
>
> > months = { 'Ιανουάριος':1, 'Φεβρουάριος':2, 'Μάρτιος':3, 'Απρίλιος':4, 'Μάϊος':5, 'Ιούνιος':6, \
>
> > 'Ιούλιος':7, 'Αύγουστος':8, 'Σεπτέμβριος':9, 'Οκτώβριος':10, 'Νοέμβριος':11, 'Δεκέμβριος':12 }
>
> >
>
> > for key in sorted( months.values() ):
>
> > print('''
>
> > <option value="%s"> %s </option>
>
> > ''' % (months[key], key) )
>
> >
>
> >
>
> > output this:
>
> >
>
> > [Mon Jun 10 11:25:11 2013] [error] [client 79.103.41.173] File "/home/nikos/public_html/cgi-bin/pelatologio.py", line 310, in <module>, referer: http://superhost.gr/
>
>
> > [Mon Jun 10 11:25:11 2013] [error] [client 79.103.41.173] ''' % (months[key], key) ), referer: http://superhost.gr/
>
> > [Mon Jun 10 11:25:11 2013] [error] [client 79.103.41.173] KeyError: 1, referer: http://superhost.gr/
>
> >
>
> > KeyError 1 ??!! All i did was to tell python to sort the dictionary values, which are just integers.
>
> > --
>
> > http://mail.python.org/mailman/listinfo/python-list
>
> KeyError: 1 means that there is no int(1) key. I think you meant to do "for key in sorted(yourdict.keys())"
both
for key in sorted( months.keys() ):
for key in sorted( months.values() ):
fail. meybe this is abug andsorted function doesnt work as expected.
[toc] | [prev] | [next] | [standalone]
| From | Νικόλαος Κούρας <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-06-10 02:48 -0700 |
| Message-ID | <56f10268-0e26-4f86-ae03-8fd740cc4544@googlegroups.com> |
| In reply to | #47537 |
After many tried this did the job:
for key in sorted(months.items(),key=lambda num : num[1]):
print('''
<option value="%s"> %s </option>
''' % (key[1], key[0]) )
but its really frustrating not being able to:
for key in sorted( months.values() ):
print('''
<option value="%s"> %s </option>
''' % (months[key], key) )
Which seemed to be an abivous way to do it.
names set() was able to order like this why not the dictionary too?
[toc] | [prev] | [next] | [standalone]
| From | Fábio Santos <fabiosantosart@gmail.com> |
|---|---|
| Date | 2013-06-10 11:13 +0100 |
| Message-ID | <mailman.2961.1370859197.3114.python-list@python.org> |
| In reply to | #47538 |
[Multipart message — attachments visible in raw view] — view raw
On 10 Jun 2013 10:53, "Νικόλαος Κούρας" <nikos.gr33k@gmail.com> wrote:
>
> After many tried this did the job:
>
> for key in sorted(months.items(),key=lambda num : num[1]):
> print('''
> <option value="%s"> %s </option>
> ''' % (key[1], key[0]) )
>
>
> but its really frustrating not being able to:
>
> for key in sorted( months.values() ):
> print('''
> <option value="%s"> %s </option>
> ''' % (months[key], key) )
>
> Which seemed to be an abivous way to do it.
> names set() was able to order like this why not the dictionary too?
Why not
for key, value in sorted(d.items()):
Tuples are totally sortable.
[toc] | [prev] | [next] | [standalone]
| From | Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> |
|---|---|
| Date | 2013-06-10 15:11 +0200 |
| Message-ID | <utvg8a-q0c.ln1@satorlaser.homedns.org> |
| In reply to | #47538 |
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('''
> <option value="%s"> %s </option>
> ''' % (key[1], key[0]) )
This code is still sending a misleading message. What you are referring
to as "key" here is in fact a (key, value) tuple. I'd use Fábio's
suggestion and use the automatic splitting:
for name, idx in sorted(months.items(), key=lambda num : num[1]):
print('month #{} is {}'.format(idx, name))
> but its really frustrating not being able to:
>
> for key in sorted( months.values() ):
> print('''
> <option value="%s"> %s </option>
> ''' % (months[key], key) )
>
> Which seemed to be an abivous way to do it.
You are composing three things:
1. months.values() - gives you a sequence with the month numbers
2. sorted() - gives you a sorted sequence
3. for-iteration - iterates over a sequence
At which point is Python doing anything non-obvious? Also, have you
considered reversing the dictionary mapping or creating a second one
with the reversed mapping? Or maybe take a look at collections.OrderedDict?
> names set() was able to order like this why not the dictionary too?
Well, why don't you use a set then, if it solves your problem? An in
which place does anything behave differently? Sorry to bring you the
news, but your expectations are not fulfilled because your assumptions
about how things should work are already flawed, I'm afraid.
Uli
[toc] | [prev] | [next] | [standalone]
| From | Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> |
|---|---|
| Date | 2013-06-10 11:40 +0200 |
| Message-ID | <igjg8a-k5b.ln1@satorlaser.homedns.org> |
| In reply to | #47534 |
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.
Uli
[toc] | [prev] | [next] | [standalone]
| From | Νικόλαος Κούρας <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-06-10 03:20 -0700 |
| Message-ID | <712f26bd-7789-4607-8e7c-b0352492a0ea@googlegroups.com> |
| In reply to | #47541 |
Τη Δευτέρα, 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 values, which are just integers. > > > > ...and which you then proceed to use as key, which is obviously wrong. How hsould have i written it then?
[toc] | [prev] | [next] | [standalone]
| From | Νικόλαος Κούρας <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-06-10 03:42 -0700 |
| Message-ID | <1b80e978-4e80-4495-88a6-74f4160bab9e@googlegroups.com> |
| In reply to | #47541 |
months = { '@@@@@@@@@@':0, 'Ιανουάριος':1, 'Φεβρουάριος':2, 'Μάρτιος':3, 'Απρίλιος':4, 'Μάϊος':5, 'Ιούνιος':6, \
'Ιούλιος':7, 'Αύγουστος':8, 'Σεπτέμβριος':9, 'Οκτώβριος':10, 'Νοέμβριος':11, 'Δεκέμβριος':12 }
for key in sorted( months.values() ):
print('''
<option value="%s"> %s </option>
''' % (months[key], key) )
==============
please tell me Uli why this dont work as expected to.
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-06-10 11:47 +0000 |
| Message-ID | <51b5bcd8$0$29997$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #47544 |
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}
print("==Values==")
for x in sorted(months.values()):
print(x)
print("==Keys==")
for x in sorted(months.keys()):
print(x)
prints:
==Values==
1
2
==Keys==
Ιανουάριος
Φεβρουάριος
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2013-06-11 02:14 +0000 |
| Message-ID | <kp616n$nrp$2@dont-email.me> |
| In reply to | #47544 |
On Mon, 10 Jun 2013 03:42:38 -0700, Νικόλαος Κούρας wrote:
> for key in sorted( months.values() ):
> print('''
> <option value="%s"> %s </option>
> ''' % (months[key], key) )
> ==============
>
> please tell me Uli why this dont work as expected to.
Because inside the for loop, your value 'key' is an item from the sorted
list of the values part of months. When you use months[key], you're
trying to look up in months based on the value part, not the key part.
Calling it key is confusing you.
Try this:
things = { "a":1, "b":3, "c":2 }
for thing in sorted( things.values() ):
print thing
>> Prints 1, 2, 3
for thing in sorted( things.keys() ):
print thing
>> Prints a, b, c
Now although things["b"] is a valid reference because "b" is a key in a
key - value pair, things[3] is not a valid reference, because 3 is not a
key in a key - value pair.
--
Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Larry Hudson <orgnut@yahoo.com> |
|---|---|
| Date | 2013-06-11 02:16 -0700 |
| Message-ID | <ZOidnfUWhupEdyvMnZ2dnUVZ_uadnZ2d@giganews.com> |
| In reply to | #47534 |
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.values() ):
> print('''
> <option value="%s"> %s </option>
> ''' % (months[key], key) )
>
>
> output this:
>
> [Mon Jun 10 11:25:11 2013] [error] [client 79.103.41.173] File "/home/nikos/public_html/cgi-bin/pelatologio.py", line 310, in <module>, referer: http://superhost.gr/
> [Mon Jun 10 11:25:11 2013] [error] [client 79.103.41.173] ''' % (months[key], key) ), referer: http://superhost.gr/
> [Mon Jun 10 11:25:11 2013] [error] [client 79.103.41.173] KeyError: 1, referer: http://superhost.gr/
>
> KeyError 1 ??!! All i did was to tell python to sort the dictionary values, which are just integers.
>
Come on now, wake up!! You do know how to use Python dictionaries don't you?
You can find the dictionary _values_ by using the _key_, but you CAN NOT get the key from the
value. (For one thing, keys must be unique but values don't need to be.) You are trying to use
the values as keys, but the keys here are all strings, there are NO keys that are integers. Of
course this is a dictionary key error.
I think someone already told you this previously, but I'll repeat it, perhaps more verbosely,
but at least in different words....
The items() function gives you a list of key/value pairs as tuples. (Not exactly, Python 3 give
you an iterator not an actual list, but anyway...)
You want to sort this list based on the second element of these tuples (the values). A simple
call to the sorted() function will sort based on the first element (the keys). But you can
alter this function by using the key parameter, and here you can do it with a short lambda
function. (And don't confuse the sorted() key parameter with dictionary keys -- two entirely
different things that just happen to use the same word.) See if you can follow this short example:
>>> d = {'Jan':1, 'Feb':2, 'Mar':3, 'Apr':4, 'May':5} # A short example dictionary
>>> list(d.items()) # Display unsorted list
[('Jan', 1), ('Apr', 4), ('Mar', 3), ('Feb', 2), ('May', 5)]
>>> sorted(d.items()) # Display list sorted by keys (month names)
[('Apr', 4), ('Feb', 2), ('Jan', 1), ('Mar', 3), ('May', 5)] # Not what you want
>>> keys = sorted(d.items(), key=lambda n: n[1]) # Sort by values (the 2nd element of items)
>>> for month in keys: # Print this sorted list
print('Month {} is {}'.format(month[1], month[0]))
Month 1 is Jan
Month 2 is Feb
Month 3 is Mar
Month 4 is Apr
Month 5 is May
[toc] | [prev] | [next] | [standalone]
| From | Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> |
|---|---|
| Date | 2013-06-10 11:19 +0200 |
| Message-ID | <saig8a-b3b.ln1@satorlaser.homedns.org> |
| In reply to | #47529 |
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() ):
> ================
>
> I'm having trouble ordering a dictionary though.
I can't find a problem here. I tried simple dictionaries containing
numbers as keys using Python 3.3, and sorting the keys works without any
problem there. What exactly is the "trouble" you are having? Be a bit
more precise and describe what you saw and, just in case, also what you
expected to see.
BTW: You have a line continuation there using a backslash. This isn't
necessary, since the pair of {} automatically tell Python the target range.
Good luck!
Uli
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web