我有一个这样的字符串列表:
['Aden', 'abel']
我想对项目进行排序,不区分大小写。所以我想得到:
['abel', 'Aden']
但我得到相反的sorted()
or list.sort()
,因为大写出现在小写之前。
我怎么能忽略这个案子?我见过涉及小写所有列表项的解决方案,但我不想更改列表项的大小写。
我有一个这样的字符串列表:
['Aden', 'abel']
我想对项目进行排序,不区分大小写。所以我想得到:
['abel', 'Aden']
但我得到相反的sorted()
or list.sort()
,因为大写出现在小写之前。
我怎么能忽略这个案子?我见过涉及小写所有列表项的解决方案,但我不想更改列表项的大小写。
在 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
方法。
In Python 2 it works for a mix of normal and unicode strings, since values of the two types can be compared with each other. Python 3 doesn't work like that, though: you can't compare a byte string and a unicode string, so in Python 3 you should do the sane thing and only sort lists of one type of string.
>>> lst = ['Aden', u'abe1']
>>> sorted(lst)
['Aden', u'abe1']
>>> sorted(lst, key=lambda s: s.lower())
[u'abe1', 'Aden']
>>> x = ['Aden', 'abel']
>>> sorted(x, key=str.lower) # Or unicode.lower if all items are unicode
['abel', 'Aden']
在 Python 3str
中是 unicode,但在 Python 2 中,您可以使用这种更通用的方法,它适用于str
和unicode
:
>>> sorted(x, key=lambda s: s.lower())
['abel', 'Aden']
You can also try this to sort the list in-place:
>>> x = ['Aden', 'abel']
>>> x.sort(key=lambda y: y.lower())
>>> x
['abel', 'Aden']
This works in Python 3 and does not involves lowercasing the result (!).
values.sort(key=str.lower)
In python3 you can use
list1.sort(key=lambda x: x.lower()) #Case In-sensitive
list1.sort() #Case Sensitive
I did it this way for 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]
Then you just can call this function:
sortCaseIns(yourListToSort)
Case-insensitive sort, sorting the string in place, in Python 2 OR 3 (tested in Python 2.7.17 and 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']
The key is key=str.lower
. Here's what those commands look like with just the commands, for easy copy-pasting so you can test them:
x = ["aa", "A", "bb", "B", "cc", "C"]
x.sort()
x
x.sort(key=str.lower)
x
Note that if your strings are unicode strings, however (like u'some string'
), then in Python 2 only (NOT in Python 3 in this case) the above x.sort(key=str.lower)
command will fail and output the following error:
TypeError: descriptor 'lower' requires a 'str' object but received a 'unicode'
If you get this error, then either upgrade to Python 3 where they handle unicode sorting, or convert your unicode strings to ASCII strings first, using a list comprehension, like this:
# 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)
Try this
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)
Output
['abel', 'Aden']