小编Kla*_*aas的帖子

Python:按值对字典进行排序,同时按字母顺序维护以防止出现关系

因此,如果项目值相同,我想按值对字典进行排序并按字母顺序维护以防止出现联系。但是,python字典没有按字母顺序保存数据,如何解决?先感谢您!

这是我的代码:

from collections import Counter
test = "betty bought a bit of butter but the butter was bitter"
Counter(test.split()).most_common(3)
Run Code Online (Sandbox Code Playgroud)

输出:

[('butter', 2), ('a', 1), ('bitter', 1)]
Run Code Online (Sandbox Code Playgroud)

所需的输出应该是:

[('butter', 2), ('a', 1), ('betty', 1)]
Run Code Online (Sandbox Code Playgroud)

因为“苦味”应该按字母顺序排在“贝蒂”之后。

python sorting dictionary

1
推荐指数
1
解决办法
620
查看次数

标签 统计

dictionary ×1

python ×1

sorting ×1