相关疑难解决方法(0)

按第二项(整数值)对元组列表进行排序

我有一个类似于下面的元组列表:

[('abc', 121),('abc', 231),('abc', 148), ('abc',221)]
Run Code Online (Sandbox Code Playgroud)

我想按元组内的整数值按升序对此列表进行排序.可能吗?

python tuples list

370
推荐指数
8
解决办法
34万
查看次数

Python元组基于最后一个元素进行排序

这是我的问题,我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
Run Code Online (Sandbox Code Playgroud)

运行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
Run Code Online (Sandbox Code Playgroud)

python terminal dictionary tuples list

5
推荐指数
1
解决办法
7513
查看次数

标签 统计

list ×2

python ×2

tuples ×2

dictionary ×1

terminal ×1