Python3: Using sorted(key=...)

J

Johannes Bauer

Hello list,

I'm having trouble with a incredibly simple sort of a list containing
ints and tuples:

def myorder(x):
if type(x) == int:
return x
else:
return x[0]

odata = sorted([ (a, b) for (a, b) in data["description"].items() ],
key=myorder)

still says:

Traceback (most recent call last):
File "./genproto.py", line 81, in <module>
odata = sorted([ (a, b) for (a, b) in data["description"].items() ],
key=myorder)
TypeError: unorderable types: tuple() < int()

Why is that? Am I missing something very obvious?

Kind regards,
Johannes
 
M

MRAB

Johannes said:
Hello list,

I'm having trouble with a incredibly simple sort of a list containing
ints and tuples:

def myorder(x):
if type(x) == int:
return x
else:
return x[0]

odata = sorted([ (a, b) for (a, b) in data["description"].items() ],
key=myorder)
You're sorting a list of tuples (key/value pairs), so 'myorder' is
always given a tuple.
still says:

Traceback (most recent call last):
File "./genproto.py", line 81, in <module>
odata = sorted([ (a, b) for (a, b) in data["description"].items() ],
key=myorder)
TypeError: unorderable types: tuple() < int()

Why is that? Am I missing something very obvious?
Are some keys 'int' and others 'tuple'? In Python 3.x you can't compare
them except for equality:


Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.False


Python 3.1 (r31:73574, Jun 26 2009, 20:21:35) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: tuple() < int()
 
J

Johannes Bauer

MRAB said:
Johannes said:
Hello list,

I'm having trouble with a incredibly simple sort of a list containing
ints and tuples:

def myorder(x):
if type(x) == int:
return x
else:
return x[0]

odata = sorted([ (a, b) for (a, b) in data["description"].items() ],
key=myorder)
You're sorting a list of tuples (key/value pairs), so 'myorder' is
always given a tuple.

Oh good lord! You're right... I meant

def myorder(x):
if type(x[0]) == int:
return x[0]
else:
return x[0][0]

Thanks for your help,
Kind regards,
Johannes
 
P

Paul Rubin

Johannes Bauer said:
def myorder(x):
if type(x[0]) == int:
return x[0]
else:
return x[0][0]

I used to write code like that pretty regularly, but over time I found
that it's better to stay consistent and use the same container format
(in this case, tuples) for everything, rather than having special
cases for the singleton and non-singleton case.
 

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

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top