Python3 排序函数未按预期运行,得到不同的输出
Python3 Sorted function not behaving as expected, getting different output
为什么会出现这种奇怪的行为:
a = ['This','is','some','banana']
"_".join(sorted(a)).
输出-
This_is_banana_some
它应该给出输出 -
is_banana_some_this
我是不是漏掉了什么?
您需要指定排序键 - 小写str
。
"_".join(sorted(a, key=str.lower))
这行得通。默认情况下 python 将大写放在首位。
为什么会出现这种奇怪的行为:
a = ['This','is','some','banana']
"_".join(sorted(a)).
输出-
This_is_banana_some
它应该给出输出 -
is_banana_some_this
我是不是漏掉了什么?
您需要指定排序键 - 小写str
。
"_".join(sorted(a, key=str.lower))
这行得通。默认情况下 python 将大写放在首位。