我有一个数组
months=['January','February','March','April','May','June','July','August','September','October','November','December']
我想把它做成字典
months = {1:'January', 2:'February',....}
我尝试了
enumerate()
,但没有任何运气。如何为列表分配序列号以制作字典?
提前致谢!
最佳答案
如果您想坚持枚举,可以这样做:
months =['January','February','March','April','May','June','July','August','September','October','November','December']
d_months = {}
for i, month in enumerate(months):
d_months[i+1] = month
print(d_months)
输出:
{1: 'January', 2: 'February', 3: 'March', 4: 'April', 5: 'May', 6: 'June', 7: 'July', 8: 'August', 9: 'September', 10: 'October', 11: 'November', 12: 'December'}
由于枚举从0开始,因此您需要使用
[i+1]