gpt4 book ai didi

python - 将字符串列表转换为字典

转载 作者:太空狗 更新时间:2023-10-29 20:39:38 26 4
gpt4 key购买 nike

我有一个 list

['Tests run: 1', ' Failures: 0', ' Errors: 0']

我想把它转换成字典

{'Tests run': 1, 'Failures': 0, 'Errors': 0}

我该怎么做?

最佳答案

使用:

a = ['Tests run: 1', ' Failures: 0', ' Errors: 0']

d = {}
for b in a:
i = b.split(': ')
d[i[0]] = i[1]

print d

返回:

{' Failures': '0', 'Tests run': '1', ' Errors': '0'}

如果你想要整数,改变赋值:

d[i[0]] = int(i[1])

这将给出:

{' Failures': 0, 'Tests run': 1, ' Errors': 0}

关于python - 将字符串列表转换为字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22980977/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com