我想將列表轉換為字典,其中鍵是列表中指定的整數,值是列表中數字的頻率。例如,列表 = [10,10,10,20,20,40,50]那么字典就會是這樣的,字典 = { '10': 3, '20': 2, '40': 1, '50': 1}。這種轉換的方法是什么?
1 回答

holdtom
TA貢獻1805條經驗 獲得超10個贊
nlist = [10,10,10,20,20,40,50]
ndict = {}
for item in set(nlist):
ndict[item] = nlist.count(item)
創建 ndict:
{40: 1, 10: 3, 20: 2, 50: 1}
添加回答
舉報
0/150
提交
取消