我有一本带字符串键和int值的字典
for word in coocc[query]:
resp[word]=coocc[query][word]
{"share": 1, "pizza": 3, "eating": 1,...}
我需要按值排序并返回一个json字符串。你知道吗
以下工作:
sortedList=sorted(resp.items(), key=operator.itemgetter(1), reverse=True)
sortedDict=collections.OrderedDict(sortedList)
return json.dumps(sortedDict)
'{"cheese": 4, "pizza": 3, "table": 3,..}
但我觉得效率不高
Python 3解决方案:
输出:
编辑:
输出:
你可以找到更多的细节 Here
相关问题 更多 >
编程相关推荐