【问题标题】:case-insensitive list sorting, without lowercasing the result?不区分大小写的列表排序,不小写结果?
【发布时间】:2019-02-06 19:54:19
【问题描述】:

我有一个这样的字符串列表:

['Aden', 'abel']

我想对项目进行排序,不区分大小写。 所以我想得到:

['abel', 'Aden']

但我对sorted()list.sort() 的看法正好相反,因为大写出现在小写之前。

如何忽略此案?我见过涉及小写所有列表项的解决方案,但我不想更改列表项的大小写。

【问题讨论】:

标签: python string list sorting case-insensitive


【解决方案1】:

不区分大小写的排序,在 Python 2 OR 3 中就地对字符串进行排序(在 Python 2.7.17 和 Python 3.6.9 中测试):

>>> x = ["aa", "A", "bb", "B", "cc", "C"]
>>> x.sort()
>>> x
['A', 'B', 'C', 'aa', 'bb', 'cc']
>>> x.sort(key=str.lower)           # <===== there it is!
>>> x
['A', 'aa', 'B', 'bb', 'C', 'cc']

密钥是key=str.lower。以下是这些命令的外观,仅包含命令,便于复制粘贴,以便您进行测试:

x = ["aa", "A", "bb", "B", "cc", "C"]
x.sort()
x
x.sort(key=str.lower)
x

请注意,如果您的字符串是 unicode 字符串(例如 u'some string'),那么仅在 Python 2 中(在这种情况下不在 Python 3 中)上述 x.sort(key=str.lower) 命令将失败并输出以下错误:

TypeError: descriptor 'lower' requires a 'str' object but received a 'unicode'

如果您收到此错误,请升级到 Python 3 以处理 unicode 排序,或者先使用列表解析将您的 unicode 字符串转换为 ASCII 字符串,如下所示:

# for Python2, ensure all elements are ASCII (NOT unicode) strings first
x = [str(element) for element in x]  
# for Python2, this sort will only work on ASCII (NOT unicode) strings
x.sort(key=str.lower)

参考资料:

  1. https://docs.python.org/3/library/stdtypes.html#list.sort
  2. Convert a Unicode string to a string in Python (containing extra symbols)
  3. https://www.programiz.com/python-programming/list-comprehension

【讨论】:

    【解决方案2】:

    您也可以尝试这样对列表进行就地排序:

    >>> x = ['Aden', 'abel']
    >>> x.sort(key=lambda y: y.lower())
    >>> x
    ['abel', 'Aden']
    

    【讨论】:

      【解决方案3】:

      在 Python 3.3+ 中,有专门为无大小写匹配设计的 str.casefold 方法:

      sorted_list = sorted(unsorted_list, key=str.casefold)
      

      在 Python 2 中使用 lower():

      sorted_list = sorted(unsorted_list, key=lambda s: s.lower())
      

      它适用于普通字符串和 unicode 字符串,因为它们都有 lower 方法。

      在 Python 2 中,它适用于普通字符串和 unicode 字符串的混合,因为这两种类型的值可以相互比较。但是 Python 3 不是这样工作的:你不能比较字节字符串和 unicode 字符串,所以在 Python 3 中你应该做理智的事情,只对一种字符串类型的列表进行排序。

      >>> lst = ['Aden', u'abe1']
      >>> sorted(lst)
      ['Aden', u'abe1']
      >>> sorted(lst, key=lambda s: s.lower())
      [u'abe1', 'Aden']
      

      【讨论】:

      • 可以避免 (Python 3) 使用通用 str.lower 函数作为 sorted(lst, key=str.lower)(Python 2) 的 lambda 函数往返使用lower method of the string module 作为sorted(lst, key=string.lower)。也可以在 Python 2 中将 str.lower 用于字符串,但随后必须将 unicode.lower 用于 unicode 对象,而 string.lower 接受两者(正如您所说,这可能不是真正的“理智”操作模式)。
      • 这不适用于像 ['Z', 'B', 'a', 'b', 'A'] 这样排序为 ['a', 'A', 'B'、'b'、'Z']。大写的“B”出现在小写的“b”之前,因为 Python 的 sort() 和 sorted() 在字符串匹配时保留原始顺序。在这种情况下,当使用 casefold 时,大写字母“B”被认为与小写字母“b”匹配。如果您转换大小写以进行比较,总是会发生这种情况:sorted(spam, key=str.lower) 或 sorted(spam, key=str.upper) 或 sorted(spam, key=str.casefold)。
      • 试试这个解决方案:stackoverflow.com/a/1098160/10668287。它将 ['Aden', 'aden'] 正确排序为 ['aden', 'Aden']。
      【解决方案4】:

      这在 Python 3 中有效,并且不涉及小写结果 (!)。

      values.sort(key=str.lower)
      
      【解决方案5】:

      在python3中你可以使用

      list1.sort(key=lambda x: x.lower()) #Case In-sensitive             
      list1.sort() #Case Sensitive
      

      【讨论】:

        【解决方案6】:
        >>> x = ['Aden', 'abel']
        >>> sorted(x, key=str.lower) # Or unicode.lower if all items are unicode
        ['abel', 'Aden']
        

        在 Python 3 中,str 是 unicode,但在 Python 2 中,您可以使用这种更通用的方法,它适用于 strunicode

        >>> sorted(x, key=lambda s: s.lower())
        ['abel', 'Aden']
        

        【讨论】:

        • 谢谢。我知道我之前应该提到过这一点,但我听说在 unicode 字符串 (Py2) 上使用此方法存在问题。你知道吗?
        • 它们都是unicode。谢谢!还有一个问题,如何在这样的列表中做到这一点:[['Aden'], ['abel']]
        • 每个列表是否只有一项?如果是这样,只需将其修改为:sorted(x,key=lambda i:i[0].lower())
        • 嗯,它可能还有一些其他的东西,但不应该用于排序。
        • 没关系,看来我错了,排序确实适用于字符串和 unicode 的混合,我对之前的问题感到困惑,其中元组也包含在排序中。
        【解决方案7】:

        我是在 Python 3.3 中这样做的:

         def sortCaseIns(lst):
            lst2 = [[x for x in range(0, 2)] for y in range(0, len(lst))]
            for i in range(0, len(lst)):
                lst2[i][0] = lst[i].lower()
                lst2[i][1] = lst[i]
            lst2.sort()
            for i in range(0, len(lst)):
                lst[i] = lst2[i][1]
        

        那么你就可以调用这个函数了:

        sortCaseIns(yourListToSort)
        

        【讨论】:

          【解决方案8】:

          试试这个

          def cSort(inlist, minisort=True):
              sortlist = []
              newlist = []
              sortdict = {}
              for entry in inlist:
                  try:
                      lentry = entry.lower()
                  except AttributeError:
                      sortlist.append(lentry)
                  else:
                      try:
                          sortdict[lentry].append(entry)
                      except KeyError:
                          sortdict[lentry] = [entry]
                          sortlist.append(lentry)
          
              sortlist.sort()
              for entry in sortlist:
                  try:
                      thislist = sortdict[entry]
                      if minisort: thislist.sort()
                      newlist = newlist + thislist
                  except KeyError:
                      newlist.append(entry)
              return newlist
          

          lst = ['Aden', 'abel']
          print cSort(lst)
          

          输出

          ['abel', 'Aden']

          【讨论】:

          • 当单行就足够时,此解决方案过于矫枉过正且不可读。这在 Python 以外的语言中可能更容易接受。
          猜你喜欢
          • 1970-01-01
          • 2013-04-17
          • 1970-01-01
          • 2019-01-31
          • 2020-09-13
          • 2012-06-22
          • 1970-01-01
          相关资源
          最近更新 更多