我有一个这样的清单:
["comp1", "comp2", "comp1", "mycomp", "mycomp"]
我希望将其转换为这样的字典:
{“comp1” : 2, “comp2” : 1, “mycomp” : 2
}
我该怎么做?
答案 0 :(得分:1)
这可能是最简单的方法:
lst = ["comp1", "comp2", "comp1", "mycomp", "mycomp"]
d = {}
for i in lst:
d[i] = d.get(i, 0) +1
print (d)
输出:
{'comp2': 1, 'comp1': 2, 'mycomp': 2}