gpt4 book ai didi

python - 按数值排序字典

转载 作者:太空宇宙 更新时间:2023-11-03 12:56:22 25 4
gpt4 key购买 nike

我有一个音乐流派的dict:

tag_weight = {'industrial': '533621', 'indie': '1971962', 'metal': '1213678', 'heavy metal': '652471', 'japanese': '428102', 'pop': '1873806', 'new wave': '399507', 'black metal': '772132', 'rap': '513024', 'ambient': '1030414', 'alternative': '2059313', 'hard rock': '820796', 'electronic': '2288563', 'blues': '531045', 'folk': '882178', 'classic rock': '1123712', 'alternative rock': '1123488', '90s': '447671', 'indie rock': '850515', 'death metal': '671118', 'electronica': '614494', 'female vocalists': '1557702', 'Soundtrack': '529406', 'dance': '769039', 'funk': '399843', 'psychedelic': '458710', '80s': '751871', 'piano': '409931', 'chillout': '636088', 'post-rock': '426516', 'punk rock': '518515', 'jazz': '1117114', 'seen live': '2097509', 'instrumental': '817816', 'singer-songwriter': '810185', 'metalcore': '444383', 'hardcore': '656111', 'Hip-Hop': '814630', 'hip hop': '394989', 'Classical': '539190', 'punk': '848955', 'soul': '641095', 'british': '667559', 'thrash metal': '465163', 'Progressive metal': '407220', 'rock': '3879179', 'acoustic': '460841', 'german': '409030', 'Progressive rock': '693480', 'experimental': '1010190'}

我想按受欢迎程度标记它们,即按值排序,从最受欢迎到不受欢迎。

因为 dicts 本质上是无序的,我必须为此使用 tuples,我一直在尝试使用它:

sorted_dict = sorted(tag_weight.items(), key=operator.itemgetter(0), reverse=True)

但它似乎不起作用,因为它返回:

[('thrash metal', '465163'), ('soul', '641095'), ('singer-songwriter', '810185'), ('seen live', '2097511'), ('rock', '3879179'), ('rap', '513024'), ('punk rock', '518515'), ('punk', '848955'), ('psychedelic', '458710'), ('post-rock', '426516'), ('pop', '1873806'), ('piano', '409931'), ('new wave', '399507'), ('metalcore', '444383'), ('metal', '1213678'), ('jazz', '1117114'), ('japanese', '428102'), ('instrumental', '817816'), ('industrial', '533621'), ('indie rock', '850515'), ('indie', '1971962'), ('hip hop', '394989'), ('heavy metal', '652471'), ('hardcore', '656111'), ('hard rock', '820796'), ('german', '409030'), ('funk', '399843'), ('folk', '882178'), ('female vocalists', '1557702'), ('experimental', '1010190'), ('electronica', '614494'), ('electronic', '2288563'), ('death metal', '671118'), ('dance', '769039'), ('classic rock', '1123712'), ('chillout', '636088'), ('british', '667559'), ('blues', '531045'), ('black metal', '772132'), ('ambient', '1030414'), ('alternative rock', '1123488'), ('alternative', '2059313'), ('acoustic', '460841'), ('Soundtrack', '529406'), ('Progressive rock', '693480'), ('Progressive metal', '407220'), ('Hip-Hop', '814630'), ('Classical', '539190'), ('90s', '447671'), ('80s', '751871')]

我想 ('rock', '3879179') 应该在列表的顶部。

我做错了什么?

最佳答案

使用为此目的构建的 collections.Counter:

import collections

# Convert values to int
tag_weight = {k: int(v) for k, v in tag_weight.items()}
count = collections.Counter(tag_weight)

# Print the top 10
print count.most_common(10)

# Print all, from most popular to least
print count.most_common()

前 10 名的输出:

[('rock', 3879179), ('electronic', 2288563), ('seen live', 2097509), ('alternative', 2059313), ('indie', 1971962), ('pop', 1873806), ('female vocalists', 1557702), ('metal', 1213678), ('classic rock', 1123712), ('alternative rock', 1123488)]

关于python - 按数值排序字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40208429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com