Sorting a set works, sorting a dictionary fails ?

  • Thread starter Íéêüëáïò Êïýñáò
  • Start date
Í

Íéêüëáïò Êïýñáò

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?
 
Í

Íéêüëáïò Êïýñáò

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?
 
Í

Íéêüëáïò Êïýñáò

Ôç ÄåõôÝñá, 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()):
 
Í

Íéêüëáïò Êïýñáò

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.
 
U

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() ):
================

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
 
F

Fábio Santos

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" said:
[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.
KeyError: 1 means that there is no int(1) key. I think you meant to do "for
key in sorted(yourdict.keys())"
 
U

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.

Uli
 
Í

Íéêüëáïò Êïýñáò

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?
 
F

Fábio Santos

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.
 
Í

Íéêüëáïò Êïýñáò

Ôç ÄåõôÝñá, 10 Éïõíßïõ 2013 12:40:01 ì.ì. UTC+3, ï ÷ñÞóôçò Ulrich Eckhardt Ýãñáøå:
Am 10.06.2013 10:29, schrieb Íéêüëáïò Êïýñáò:


^^^ ^^^^^^






...and which you then proceed to use as key, which is obviously wrong.

How hsould have i written it then?
 
Í

Íéêüëáïò Êïýñáò

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.
 
S

Steven D'Aprano

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==
ΙανουάÏιος
ΦεβÏουάÏιος
 
U

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('''
<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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top