如何将{'CS2261': '140', 'CS3264': '55'}
转换为{'CS2261': 140, 'CS3264': 55}
?
def everything(file):
a = {}
with open(file, 'r') as f:
for i in f:
Module,Group,Quota = i.split(',')
if Module not in a:
a[Module] = int(Quota)
return a
答案 0 :(得分:1)
您可以像这样
重建词典理解词典d = {'CS2261': '140', 'CS3264': '55'}
print {k: int(d[k]) for k in d}
# {'CS3264': 55, 'CS2261': 140}
答案 1 :(得分:1)
{k:int(v) for k,v in d.items()}