python - 排序方法的Python字母顺序显示错误的顺序
问题描述
我刚刚学习 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
我很困惑,无法在学习过程中继续前进,你的见解会很有帮助。
解决方案
你可以试试:
my_str="Welcome to Python"
some_list = my_str.split()
some_list.sort(key=str.casefold)
print("The sorted words are:")
for word in some_list:
print(word)
输出将是:
Python
to
Welcome
推荐阅读
- python - 如果最小值满足特定条件,则在函数中编写查找第二最小值的脚本
- html - 我不能在 CSS 中使用内联 svg 作为自定义光标
- php - PHP 多重查询
- python - 如何将 ARRAY_CONSTRUCT 作为 ARRAY?
- ruby-on-rails - 数组中空白元素的Rails顺序
- python - Django 过滤器:“排序依据”功能
- javascript - 限制 API 调用队列并返回结果的速率
- php - Laravel 中的批量用户通知
- python - 如何在python中忽略异常并继续处理整个块(多行)?
- javascript - Nuxt.js + Auth(jwt 刷新令牌)