python转换为字典中的整数

时间:2014-04-10 15:55:30

标签: python dictionary int

如何将{'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

2 个答案:

答案 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()}