【发布时间】:2020-08-13 02:16:51
【问题描述】:
我刚刚学习 python,遇到了一个按字母顺序排序单词的代码。
我的代码是:
my_str="Welcome to Python"
words = my_str.split()
words.sort()
print("The sorted words are:")
for word in words:
print(word)
我的结果是:
排序后的单词是:
Python
Welcome
to
我的意思是它是按字母顺序排序的,那么结果应该是
Python
to
Welcome
我很困惑,无法在学习过程中继续前进,你的见解会很有帮助。
【问题讨论】:
-
ASCII 中大写字母排在小写字母之前
-
不区分大小写排序,可以使用
str.casefold。类似some_list.sort(key=str.casefold)。
标签: python python-3.x