【发布时间】:2013-01-27 13:37:18
【问题描述】:
这是我的问题,我有tuple1=[(1, 3), (3, 2), (2, 1)] 我想根据每个元组的最后一位数字对元组进行排序,因此结果将如下所示
output=[(2, 1), (3, 2), (1, 3)]
下面是我的代码
i=0
for x in tuples:
c.append(x[len(x)-1])
last=sorted(c)
for y in last.iteritems():
if(y in x[len(x)-1]):
print x
#b.insert(i,x)
i=i+1
运行 iam 后收到错误消息
Traceback (most recent call last):
File "x.py", line 47, in <module>
sort_last([(1, 3), (3, 2), (2, 1)])
File "x.py", line 35, in sort_last
if(y in x[len(x)-1]):
TypeError: argument of type 'int' is not iterable
【问题讨论】:
-
我的错,这个问题与排序无关,但解决方案是相同的:将
key函数指定为sort或sorted。
标签: python list dictionary terminal tuples