gpt4 book ai didi

python - 如何在python中将List数据类型转换为Dictionary数据

转载 作者:行者123 更新时间:2023-11-30 22:38:49 24 4
gpt4 key购买 nike

我希望将我的列表转换为字典例如

list1=["Indrajit","Mayur","Swapnali","Akankasha"]

进入字典具有自动生成的键,例如

dict1 = {"1" : "indrajit","2" : "Mayur", "3" : "Swapnali", "4" : "Akankasha"}

最佳答案

您可以使用从 1 开始的 enumerate 并将索引转换为字符串,在字典理解中:

list1=["Indrajit","Mayur","Swapnali","Akankasha"]

dict1 = {str(k):v for k,v in enumerate(list1,1)}

print(dict1)

结果:

{'1': 'Indrajit', '4': 'Akankasha', '2': 'Mayur', '3': 'Swapnali'}

关于python - 如何在python中将List数据类型转换为Dictionary数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43410851/

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