【问题标题】:Why can't I sort this list?为什么我不能对这个列表进行排序?
【发布时间】:2009-11-29 13:19:59
【问题描述】:
statlist = [('abc',5,1), ('bzs',66,1), ... ]
sorted(statlist, key=lambda x: int(x[1]))

我想按整数从大到小排序。在本例中为 5 和 66。但它似乎不起作用。

【问题讨论】:

  • “似乎不起作用”是什么意思?请提供您实际遇到的错误回溯或具体问题。
  • 另外,周日下午有三个n00b性质的问题,我猜这是家庭作业。请清楚地标记作业。
  • 我忘记将新列表设置为旧列表。我认为它的工作原理类似于“排序”功能。
  • @alex: (0) 你的两个句子不兼容......你不能忘记你不知道的东西 (1) 对照 (a) 文档 (b) 简单测试测试你的想法使用交互式解释器...通常可以节省提问时间并且速度更快 (2) 考虑重新访问 http://stackoverflow.com/questions/1808567/what-is-the-default-content-type-charset 并找出您真正的问题是什么。
  • @alex:进一步的想法在答案中(无法在 cmets 中格式化代码)。

标签: python list tuples


【解决方案1】:

sorted 函数返回一个 new 列表,因此您需要像这样分配函数的结果:

new_list = sorted(statlist, key=lambda x: int(x[1])) 

【讨论】:

  • 这不符合 OP 的要求,引用“从最大到最小的整数”。
  • 与此无关,但谁能告诉我 OP 是什么意思?我也在其他帖子中看到过。谢谢
  • 原始海报。由于 SO 像 wiki 一样工作,我们以这种方式指代第一个用户,这将他写的内容与已更正的内容区分开来。
【解决方案2】:

使用.sort 方法进行就地排序:

statlist = [('abc',5,1), ('bzs',66,1), ... ]
statlist.sort(key=lambda x: int(x[1]))

如果您确实想使用sorted,则重新分配变量:

statlist = [('abc',5,1), ('bzs',66,1), ... ]
statlist = sorted(statlist, key=lambda x: int(x[1]))

对于降序排序,使用reverse:

statlist = [('abc',5,1), ('bzs',66,1), ... ]
statlist = sorted(statlist, key=lambda x: int(x[1]), reverse=True)

那么,你最好使用itemgetter 而不是lambda

import operator
statlist = [('abc',5,1), ('bzs',66,1), ... ]
statlist = sorted(statlist, key=operator.itemgetter(1), reverse=True)

【讨论】:

    【解决方案3】:

    您可以传递、键和反转到 .sort 函数

    >>> x.sort(key=lambda x:x[1],reverse=True)
    >>> x
    [('bzs', 66, 1), ('abc', 5, 1)]
    >>>
    

    【讨论】:

      【解决方案4】:

      用于就地排序使用

      statlist.sort(key=lambda x: x[1])
      

      用于创建其他列表,使用排序数据

      otherlist = sorted( statlist, key=lambda x: x[1] )
      

      【讨论】:

        【解决方案5】:
        from operator import itemgetter
        statlist = [('abc',5,1), ('bzs',66,1), ... ]
        
        # statlist.sort modifiest the statlist, sorted returns a new one
        # reverse puts the largest items to the front
        statlist.sort(key=itemgetter(1), reverse=True)
        

        【讨论】:

          【解决方案6】:

          在回应 alex 的评论时,他认为 sorted() 的工作方式“类似于 sort 函数”:

          如果它“像排序功能”一样工作,那么它不太可能被放入库中。

          无论如何,没有排序函数 ...你指的是列表对象的排序方法

          使用交互式解释器的简单演示:

          >>> alist = [3, 2, 1]; x = alist.sort(); print x; print alist
          None
          [1, 2, 3]
          >>> alist = [3, 2, 1]; x = sorted(alist); print x; print alist
          [1, 2, 3]
          [3, 2, 1]
          

          这里有一个提示:寻找模式和相似之处,但始终验证您的直观推断。您可能希望将这些想法应用于reversereversed

          【讨论】:

            【解决方案7】:
            >>> s = [('xyz', 8, 1), ('abc',5,1), ('bzs',66,1) ]
            >>> s = sorted(s, key=lambda x: int(x[1]))
            >>> s.reverse()
            >>> print s
            [('bzs', 66, 1), ('xyz', 8, 1), ('abc', 5, 1)]
            

            【讨论】:

              【解决方案8】:

              嘿,当我将某些东西保存到数组时,我不会担心顺序,然后最后我使用sorted(),例如像这样statlist = sorted(statlist),如果你想要它从大到小statlist = sorted(statlist, reverse = True)这是从大到小的简单方法!

              我使用过的示例代码(只是摘录)

                  while i <= math.sqrt(intnum):
                      if (intnum % i) == 0:
                          numbers.insert(0,i)
                          numbers.insert(0,int(intnum/i))
                          print(i,":", int(intnum/i))
                      i += 1
                  numbers = sorted(numbers, reverse = True)
              

              【讨论】:

                猜你喜欢
                • 2014-08-13
                • 1970-01-01
                • 2015-12-27
                • 1970-01-01
                • 2022-11-22
                • 2013-08-02
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多