簡體   English   中英

Python:有沒有辦法按整數值對字典排序

[英]Python: Is there any way to sort a dictionary by integer value

有什么方法可以在不知道python中的鍵的情況下在字典中查找值數字,例如3?

我知道字典沒有命令。

我只能使用dict [dict.keys()[0]]獲得任意元素

提前致謝。

因此,如果我正確理解,您正在尋找值3的所有鍵。可以通過遍歷鍵並進行比較來實現:

output = []
for key, value in dict.items():
    if value == 3:
        output.append((key, value))

print(output)

如果您只想按整數值排序,請嘗試:

sorted(dict.items(), key=lambda i:i[1])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM