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

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