我有三個數字值(重量,計數,貢獻)的各種字符串(單詞),我想組織成一個多維數組,然後排序。要做到這一點,我的字典,其中的數值是在列表中,字符串是關鍵中所做的列表:用於在Python中對多維數組進行排序的適當數據結構?
print_dictionary[word] = [weight,count,contribution]
我如何排序,首先按升序排列,然後按降序排列,通過'貢獻'(列表中的第三個值),並顯示排序列表的前10項。我怎樣才能做到這一點?
例如,對於以下print_dictionary:
print_dictionary[sam] = [2,7,1]
print_dictionary[sun] = [4,1,3]
print_dictionary[dog] = [1,3,2]
我希望他們能夠以升序排序的貢獻:
Word: Weight: Count: Contribution:
sam 2 7 1
dog 1 3 2
sun 4 1 3
我看不出itemegetter如何使用這個:
sorted(print_dictionary, key=itemgetter(2))
列表_are_在python中排序。我認爲你的意思是「字典無序」。 –
謝謝,我編輯了這個帖子來澄清。 – Zach
此外,你不能像這樣的字典上使用itemgetter。你必須調用'print_dictionary.items()'。 –