Python 忽略大写/小写的字符串排序列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13954841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:00:17  来源:igfitidea点击:

Sort list of strings ignoring upper/lower case

pythonsortinglexicographiclexicographic-ordering

提问by Darknight

I have a list which contains strings representing animal names. I need to sort the list. If I use sorted(list), it will give the list output with uppercase strings first and then lowercase.

我有一个列表,其中包含表示动物名称的字符串。我需要对列表进行排序。如果我使用sorted(list),它将首先给出带有大写字符串的列表输出,然后是小写的。

But I need the below output.

但我需要以下输出。

Input:

输入:

var = ['ant','bat','cat','Bat','Lion','Goat','Cat','Ant']

Output:

输出:

['ant', 'Ant', 'bat', 'Bat', 'cat', 'Cat', 'Goat', 'Lion']

采纳答案by Martijn Pieters

The sort()method and the sorted()function take a key argument:

sort()方法和sorted()函数采用一个关键参数:

var.sort(key=lambda v: v.upper())

The function named in keyis called for each value and the return value is used when sorting, without affecting the actual values:

key为每个值调用名为 in 的函数,排序时使用返回值,不影响实际值:

>>> var=['ant','bat','cat','Bat','Lion','Goat','Cat','Ant']
>>> sorted(var, key=lambda v: v.upper())
['ant', 'Ant', 'bat', 'Bat', 'cat', 'Cat', 'Goat', 'Lion']

To sort Antbefore ant, you'd have to include a little more info in the key, so that otherwise equal values are sorted in a given order:

要排序Antbefore ant,您必须在键中包含更多信息,以便其他相等的值按给定顺序排序:

>>> sorted(var, key=lambda v: (v.upper(), v[0].islower()))
['Ant', 'ant', 'Bat', 'bat', 'Cat', 'cat', 'Goat', 'Lion']

The more complex key generates ('ANT', False)for Ant, and ('ANT', True)for ant; Trueis sorted after Falseand so uppercased words are sorted before their lowercase equivalent.

更复杂的键产生('ANT', False)Ant,和('ANT', True)用于ant; True排在后面False,因此大写单词排在小写字母之前。

See the Python sorting HOWTOfor more information.

有关更多信息,请参阅Python 排序 HOWTO

回答by Aaron Hall

New answer for Python 3, I'd like to add two points:

Python 3 的新答案,我想补充两点:

  1. Use str.casefoldfor case-insensitive comparisons.
  2. Use the method directly instead of inside of a lambda.
  1. 使用str.casefold不区分大小写的比较。
  2. 直接使用该方法而不是在 lambda 内部使用。

That is:

那是:

var = ['ant','bat','cat','Bat','Lion','Goat','Cat','Ant']

var.sort(key=str.casefold)

(which sorts in-place) and now:

(就地排序)现在:

>>> var
['ant', 'Ant', 'bat', 'Bat', 'cat', 'Cat', 'Goat', 'Lion']

Or, to return a new list, use sorted

或者,要返回一个新列表,请使用 sorted

>>> var = ['ant','bat','cat','Bat','Lion','Goat','Cat','Ant']
>>> sorted(var, key=str.casefold)
['ant', 'Ant', 'bat', 'Bat', 'cat', 'Cat', 'Goat', 'Lion']

Why is this different from str.loweror str.upper? According to the documentation:

为什么这与str.lower或不同str.upper根据文档:

Casefolding is similar to lowercasing but more aggressive because it is intended to remove all case distinctions in a string. For example, the German lowercase letter '?'is equivalent to "ss". Since it is already lowercase, str.lower()would do nothing to '?'; casefold()converts it to "ss".

Casefolding 类似于小写,但更具侵略性,因为它旨在消除字符串中的所有大小写区别。例如,德语小写字母'?'相当于"ss". 由于它已经是小写字母,str.lower()因此不会对'?'; casefold()将其转换为"ss".

回答by Christian Tismer

I need to add yet another answer, since both the accepted answer and the newer versions lack one important thing:

我需要添加另一个答案,因为接受的答案和较新的版本都缺少一件重要的事情

The here proposed case-insensitive sorting is not stablein the ordering of "equal" keys!

这里建议的不区分大小写的排序在“相等”键的排序中不稳定

That means: When you have a mixture of mixed case strings that you want to sort, you get a correctly sorted list, but it is undefined whether "AbC" comes before "aBc" or after. This may even vary between runs of the same program.

这意味着:当您想要排序混合大小写字符串时,您会得到一个正确排序的列表,但不确定“AbC”是在“aBc”之前还是之后。这甚至可能因同一程序的运行而异。

In order to always have the same output with a stable default ordering of strings, I use the following function:

为了始终具有相同的输出和稳定的默认字符串排序,我使用以下函数:

sorted(var, key=lambda v: (v.casefold(), v))

This way, the original key is always appended as a fallback ordering when the casefold version does not supply a difference to sort on.

这样,当 casefold 版本不提供差异进行排序时,原始键始终作为后备排序附加。