Sắp xếp không phân biệt chữ hoa chữ thường, sắp xếp chuỗi tại chỗ, trong Python 2 hoặc 3 [được thử nghiệm trong Python 2.7.17 và 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] # >> x
['A', 'aa', 'B', 'bb', 'C', 'cc']
Chìa khóa là
x = ["aa", "A", "bb", "B", "cc", "C"]
x.sort[]
x
x.sort[key=str.lower]
x
5. Dưới đây là những lệnh đó trông như thế nào chỉ với các lệnh, để dễ dàng sao chép để bạn có thể kiểm tra chúng:x = ["aa", "A", "bb", "B", "cc", "C"]
x.sort[]
x
x.sort[key=str.lower]
x
Lưu ý rằng nếu chuỗi của bạn là chuỗi unicode, tuy nhiên [như
x = ["aa", "A", "bb", "B", "cc", "C"]
x.sort[]
x
x.sort[key=str.lower]
x
6], thì chỉ trong Python 2 [không phải trong Python 3 trong trường hợp này], lệnh x = ["aa", "A", "bb", "B", "cc", "C"]
x.sort[]
x
x.sort[key=str.lower]
x
7 trên sẽ không thành công và xuất ra lỗi sau:TypeError: descriptor 'lower' requires a 'str' object but received a 'unicode'
Nếu bạn gặp lỗi này, thì hãy nâng cấp lên Python 3, nơi chúng xử lý việc sắp xếp Unicode hoặc chuyển đổi chuỗi Unicode của bạn thành chuỗi ASCII trước, sử dụng khả năng hiểu danh sách, như thế này:
# 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]
References:
- //docs.python.org/3/library/stdtypes.html#list.sort
- Chuyển đổi chuỗi Unicode thành chuỗi trong Python [chứa các ký hiệu bổ sung]
- //www.programiz.com/python-programming/list-comprehension
Cải thiện bài viết
Lưu bài viết
Cải thiện bài viết
Lưu bài viết
Đọc
Method #1: Using casefold[]
Bàn luận
Đưa ra một danh sách các chuỗi, một nhiệm vụ là sắp xếp các chuỗi theo cách không nhạy cảm. Đưa ra dưới đây là một vài phương pháp để giải quyết nhiệm vụ.
TypeError: descriptor 'lower' requires a 'str' object but received a 'unicode'
9# 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]
0TypeError: descriptor 'lower' requires a 'str' object but received a 'unicode'
2# 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]
2TypeError: descriptor 'lower' requires a 'str' object but received a 'unicode'
2# 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]
4# 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]
5Python3
# Sort the list of strings
L = ['red', 'green', 'blue', 'orange']
L.sort[]
print[L]
# Prints ['blue', 'green', 'orange', 'red']
0# Sort the list of strings
L = ['red', 'green', 'blue', 'orange']
L.sort[]
print[L]
# Prints ['blue', 'green', 'orange', 'red']
1# Sort the list of strings
L = ['red', 'green', 'blue', 'orange']
L.sort[]
print[L]
# Prints ['blue', 'green', 'orange', 'red']
2TypeError: descriptor 'lower' requires a 'str' object but received a 'unicode'
2# Sort the list of strings
L = ['red', 'green', 'blue', 'orange']
L.sort[]
print[L]
# Prints ['blue', 'green', 'orange', 'red']
4# Sort the list of strings
L = ['red', 'green', 'blue', 'orange']
L.sort[]
print[L]
# Prints ['blue', 'green', 'orange', 'red']
5x = ["aa", "A", "bb", "B", "cc", "C"]
x.sort[]
x
x.sort[key=str.lower]
x
8x = ["aa", "A", "bb", "B", "cc", "C"]
x.sort[]
x
x.sort[key=str.lower]
x
9 TypeError: descriptor 'lower' requires a 'str' object but received a 'unicode'
0TypeError: descriptor 'lower' requires a 'str' object but received a 'unicode'
1TypeError: descriptor 'lower' requires a 'str' object but received a 'unicode'
2TypeError: descriptor 'lower' requires a 'str' object but received a 'unicode'
3__22222525222227272828# Sort the list of strings
L = ['red', 'green', 'blue', 'orange']
L.sort[]
print[L]
# Prints ['blue', 'green', 'orange', 'red']
0# Sort the list of strings
L = ['red', 'green', 'blue', 'orange']
L.sort[]
print[L]
# Prints ['blue', 'green', 'orange', 'red']
1# Sort the list of numbers
L = [42, 99, 1, 12]
L.sort[]
print[L]
# Prints [1, 12, 42, 99]
5TypeError: descriptor 'lower' requires a 'str' object but received a 'unicode'
2# Sort the list of strings
L = ['red', 'green', 'blue', 'orange']
L.sort[]
print[L]
# Prints ['blue', 'green', 'orange', 'red']
4# Sort the list of numbers
L = [42, 99, 1, 12]
L.sort[]
print[L]
# Prints [1, 12, 42, 99]
8
6# 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]
9 x = ["aa", "A", "bb", "B", "cc", "C"]
x.sort[]
x
x.sort[key=str.lower]
x
8# 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]
9 # 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]
Case-sensitive sorted list [‘Alind’,
‘GeeksForGeeks’, ‘Munich’, ‘SIngh’, ‘akshat’, ‘garg’, ‘manjeet’]
Case-insensitive sorted list [‘akshat’, ‘Alind’, ‘garg’, ‘GeeksForGeeks’, ‘manjeet’, ‘Munich’, ‘SIngh’]
# Sort the list of strings
L = ['red', 'green', 'blue', 'orange']
L.sort[]
print[L]
# Prints ['blue', 'green', 'orange', 'red']
6x = ["aa", "A", "bb", "B", "cc", "C"]
x.sort[]
x
x.sort[key=str.lower]
x
9 # 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]
8# Sort the list of strings
L = ['red', 'green', 'blue', 'orange']
L.sort[]
print[L]
# Prints ['blue', 'green', 'orange', 'red']
9x = ["aa", "A", "bb", "B", "cc", "C"]
x.sort[]
x
x.sort[key=str.lower]
x
9 # Sort the list of numbers
L = [42, 99, 1, 12]
L.sort[]
print[L]
# Prints [1, 12, 42, 99]
1 # Sort the list of numbers
L = [42, 99, 1, 12]
L.sort[]
print[L]
# Prints [1, 12, 42, 99]
2Method #2: Using lower[]
Python3
Đưa ra một danh sách các chuỗi, một nhiệm vụ là sắp xếp các chuỗi theo cách không nhạy cảm. Đưa ra dưới đây là một vài phương pháp để giải quyết nhiệm vụ.
TypeError: descriptor 'lower' requires a 'str' object but received a 'unicode'
9# 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]
0TypeError: descriptor 'lower' requires a 'str' object but received a 'unicode'
2# 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]
2TypeError: descriptor 'lower' requires a 'str' object but received a 'unicode'
2# 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]
4# 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]
5Python3
# Sort the list of strings
L = ['red', 'green', 'blue', 'orange']
L.sort[]
print[L]
# Prints ['blue', 'green', 'orange', 'red']
0# Sort the list of strings
L = ['red', 'green', 'blue', 'orange']
L.sort[]
print[L]
# Prints ['blue', 'green', 'orange', 'red']
1# Sort the list of strings
L = ['red', 'green', 'blue', 'orange']
L.sort[]
print[L]
# Prints ['blue', 'green', 'orange', 'red']
2TypeError: descriptor 'lower' requires a 'str' object but received a 'unicode'
2# Sort the list of strings
L = ['red', 'green', 'blue', 'orange']
L.sort[]
print[L]
# Prints ['blue', 'green', 'orange', 'red']
4L = ['red', 'green', 'blue', 'orange']
L.sort[key=len]
print[L]
# Prints ['red', 'blue', 'green', 'orange']
3x = ["aa", "A", "bb", "B", "cc", "C"]
x.sort[]
x
x.sort[key=str.lower]
x
8x = ["aa", "A", "bb", "B", "cc", "C"]
x.sort[]
x
x.sort[key=str.lower]
x
9 TypeError: descriptor 'lower' requires a 'str' object but received a 'unicode'
0TypeError: descriptor 'lower' requires a 'str' object but received a 'unicode'
1TypeError: descriptor 'lower' requires a 'str' object but received a 'unicode'
2TypeError: descriptor 'lower' requires a 'str' object but received a 'unicode'
3__22222525222227272828# Sort the list of strings
L = ['red', 'green', 'blue', 'orange']
L.sort[]
print[L]
# Prints ['blue', 'green', 'orange', 'red']
0# Sort the list of strings
L = ['red', 'green', 'blue', 'orange']
L.sort[]
print[L]
# Prints ['blue', 'green', 'orange', 'red']
1# Sort the list of numbers
L = [42, 99, 1, 12]
L.sort[]
print[L]
# Prints [1, 12, 42, 99]
5TypeError: descriptor 'lower' requires a 'str' object but received a 'unicode'
2# Sort the list of strings
L = ['red', 'green', 'blue', 'orange']
L.sort[]
print[L]
# Prints ['blue', 'green', 'orange', 'red']
4L = ['red', 'green', 'blue', 'orange']
L.sort[key=len]
print[L]
# Prints ['red', 'blue', 'green', 'orange']
3
6# 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]
9 x = ["aa", "A", "bb", "B", "cc", "C"]
x.sort[]
x
x.sort[key=str.lower]
x
8# 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]
9 # 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]
Case-sensitive sorted list [‘Alind’, ‘GeeksForGeeks’, ‘Munich’, ‘SIngh’, ‘akshat’, ‘garg’, ‘manjeet’]
Case-insensitive sorted list [‘akshat’, ‘Alind’, ‘garg’, ‘GeeksForGeeks’, ‘manjeet’, ‘Munich’, ‘SIngh’]
Sắp xếp các mục của danh sách Sử dụng phương thức Cách sử dụng
4 để sắp xếp các mục của danh sách.# Sort a list of tuples based on the age of students
def myFunc[e]:
return e[1] # return age
L = [['Bob', 30],
['Sam', 35],
['Max', 25]]
L.sort[key=myFunc]
print[L]
# Prints [['Max', 25], ['Bob', 30], ['Sam', 35]]
Bạn có thể tùy chọn chỉ định các tham số để sắp xếp tùy chỉnh như sắp xếp đơn hàng và sắp xếp tiêu chí.
Cú pháp
list.sort[key,reverse]sort[key,reverse]
Tham số Phương thức Danh sách Python [] tham sốTham số | Tình trạng | Sự mô tả |
Chìa khóa | Không bắt buộc | Một hàm để chỉ định giá trị tiêu chí sắp xếp.default là không có. Default value is None. |
đảo ngược | Không bắt buộc | Một hàm để chỉ định giá trị tiêu chí sắp xếp.default là không có. Default value is False. |
đảo ngượckeyword arguments.
Giải quyết nó thành các loại thực sự danh sách theo thứ tự ngược lại.default giá trị là sai.
# Sort the list of strings
L = ['red', 'green', 'blue', 'orange']
L.sort[]
print[L]
# Prints ['blue', 'green', 'orange', 'red']
# Sort the list of numbers
L = [42, 99, 1, 12]
L.sort[]
print[L]
# Prints [1, 12, 42, 99]
Xin lưu ý rằng cả hai đối số phải được chỉ định là đối số từ khóa.
L = ['red', 'blue', 1, 12, 'orange',42, 'green', 99]
L.sort[]
# Triggers TypeError: '