亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 Python 中對字典進行排序

在 Python 中對字典進行排序

DIEA 2021-12-29 20:21:14
我使用 Python 3,6,我需要按值對多維字典進行排序。預期的結果是輸出將按Num_Subdir值排序,并顯示相同的字典,但具有此順序。我通過函數獲取字典,并將此函數的輸出重定向到 var 'Dirs',因此 dirs 是字典。我嘗試使用sorted函數,但一直收到此錯誤:orden = sorted(['dirs'], key = lambda x: x['Num_Subdir'])錯誤:TypeError: string indices must be integers編輯我簡化了我的字典,只得到我將來會使用的:{'1234': {'/root/git/dir1/1234': 10}, 'cece': {'/root/git/dir1/cece': 0}, 'dir11': {'/root/git/dir1/dir11': 7}, 'dir13': {'/root/git/dir1/dir13': 7}, 'dir14': {'/root/git/dir1/dir14': 11}, 'dir15': {'/root/git/dir1/dir15': 12}, 'dir16': {'/root/git/dir1/dir16': 0}, 'dir17': {'/root/git/dir1/dir17': 0}, 'dir18': {'/root/git/dir1/dir18': 0}, 'jaja': {'/root/git/dir1/jaja': 0}, 'ola1': {'/root/git/dir1/ola1': 0}}我現在如何按目錄的值排序?編輯2:我得到了它: sorted(dicta.items(), key=lambda x: [int(x) for x in x[1].values()], reverse = True)
查看完整描述

3 回答

?
人到中年有點甜

TA貢獻1895條經驗 獲得超7個贊

如果將您的 dict 更簡化為:


dirs = {

 '/root/git/dir1/1234': 10,

 '/root/git/dir1/cece': 0,

 '/root/git/dir1/dir11': 7,

 '/root/git/dir1/dir13': 7,

 '/root/git/dir1/dir14': 11,

 '/root/git/dir1/dir15': 12,

 '/root/git/dir1/dir16': 0,

 '/root/git/dir1/dir17': 0,

 '/root/git/dir1/dir18': 0,

 '/root/git/dir1/jaja': 0,

 '/root/git/dir1/ola1': 0}

然后您可以按數字排序打印它:


print('{' + ',\n'.join(

    '\'{}\': {}'.format(x, y) for x, y in sorted(

    dirs.items(), key=lambda x: x[1])) + '}')

輸出:


{'/root/git/dir1/ola1': 0,

'/root/git/dir1/cece': 0,

'/root/git/dir1/dir18': 0,

'/root/git/dir1/dir17': 0,

'/root/git/dir1/jaja': 0,

'/root/git/dir1/dir16': 0,

'/root/git/dir1/dir11': 7,

'/root/git/dir1/dir13': 7,

'/root/git/dir1/1234': 10,

'/root/git/dir1/dir14': 11,

'/root/git/dir1/dir15': 12}


查看完整回答
反對 回復 2021-12-29
?
www說

TA貢獻1775條經驗 獲得超8個贊

我認為問題在于您使用的數據結構!


默認情況下,python 中的字典沒有稱為排序的功能。因此,您需要考慮替代方案。


解決這個問題的方法之一是使用元組列表。


為什么是元組列表?


元組列表可以在必要時輕松轉換為 dict。例如,


>>> list_of_tuples = [('a', 'b'), ('c', 'd')]

>>> a_dict = dict(list_of_tuples)

>>> a_dict

{'a': 'b', 'c': 'd'}

現在,由于您的數據是 dict 格式,您需要將其轉換為可以使用 dict.items() 方法完成的元組列表。就是這樣


>>> a_dict.items()

dict_items([('a', 'b'), ('c', 'd')])

>>> list(a_dict.items())

[('a', 'b'), ('c', 'd')]

結果與 完全相同list_of_tuples。請記住,dict 類型對象的鍵應該始終是不可變的數據類型。


現在,回到你的問題,


我嘗試使用排序函數,但一直收到此錯誤: orden = sorted(['dirs'], key = lambda x: x['Num_Subdir']) 錯誤: TypeError: string indices must be integers


您在 x 中獲得的值是一個字符串,而不是一個 dict 類型的對象。因此,TypeError.


獲取編輯結果:


data_dict = {k: {v['Path']: v['Num_Subdir']} for k, v in d['Subdir'].items()}

更改為建議的數據結構


data = [(k, (v['Path'], v['Num_Subdir'])) for k, v in d['Subdir'].items()]

按路徑排序


data.sort(key=lambda item: item[1][0], reverse=False)

按 Num_Subdir 排序


data.sort(key=lambda item: item[1][1], reverse=False)

按子目錄名稱排序


data.sort(key=lambda item: item[0], reverse=False)  # reverse=True for descending order


查看完整回答
反對 回復 2021-12-29
?
慕桂英546537

TA貢獻1848條經驗 獲得超10個贊

希望這可以幫助...


試試 OrderedDict,這是非常有用的一個,


>>>from collections import OrderedDict

>>>final_response1 = OrderedDict(sorted(dirs['Subdir']['1234']['Subdir'].items(),key=lambda t: t[0]))

#In the above these were the reference that you have stored **"dirs['Subdir']['1234']['Subdir'].items()"**

現在您將獲得已排序的項目。


查看完整回答
反對 回復 2021-12-29
  • 3 回答
  • 0 關注
  • 170 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號