意见箱
恒创运营部门将仔细参阅您的意见和建议,必要时将通过预留邮箱与您保持联络。感谢您的支持!
意见/建议
提交建议

python将2个列表合并为一个字典 &&字符串转列表

来源:恒创科技 编辑:恒创科技编辑部
2023-12-31 16:58:59
# key 和 value 个数一致
x = [1,2,3,4,5]
y = ['a','b','c','d','e']
res = dict(zip(y,x))
print(res)
print("===================")
# key 和 value 个数不一致
## key多
x = [1,2]
y = ['a','b','c']
res = dict(zip(y,x))
print(res)
print("===================")
## key少
x = [1,2,3,4]
y = ['a','b','c']
res = dict(zip(y,x))
print(res)
print("===================")
# 将字符串转换为列表
temp_str = "['肯德基','麦当劳','华莱士']"
temp_list = ast.literal_eval(temp_str)
print(temp_list)
print(type(temp_list))

输出结果:

{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}{'a': 1, 'b': 2}{'a': 1, 'b': 2, 'c': 3}

['肯德基', '麦当劳', '华莱士']
<class 'list'>


原文链接:https://blog.csdn.net/qq_1724...


python将2个列表合并为一个字典 &amp;&amp;字符串转列表

上一篇: 15款Python编辑器,你都使用过哪一款 下一篇: 项目目录规范(项目资料整理规范)