迭代列表时出错

时间:2021-03-24 22:30:33
[{'January': 'Sun'}, {'January': 'Sun'}, {'January': 'Sun'}, {'January': 'Sun'}]

Thats my dict.

多数民众赞成。

My code to make a list of values of dict_month

我的代码列出了dict_month的值

dict_month = []    
jan_month= []
for x in file_st:
    a=calendar.month_name[int(x['Start Time'][5:7])] #month name
    b=parser.parse(x['Start Time']).strftime("%a")  # day name
    dict_month.append({a:b})     # [{}]

for x in dict_month:
    jan_month.append(x['January'])

The error i get is

我得到的错误是

    KeyError                                  Traceback (most recent call 
last)
<ipython-input-42-879788f99587> in <module>()
 23 
 24 for x in dict_month:
---> 25     jan_month.append(x['January'])
     26 

 KeyError: 'January'

Where is the code wrong? how do i correct it?

代码在哪里错了?我该如何纠正呢?

output [sun,sun,sun]

edit its a key error this should help.

编辑一个关键错误,这应该有所帮助。

3 个解决方案

#1


2  

Hope it may help you:

希望它可以帮助你:

dict_month = [{'January': 'Sun'}, {'January': 'Sun'}, {'January': 'Sun'}, {'January': 'Sun'}]  

jan_month =  [x['January'] for x in dict_month]

>>>['Sun', 'Sun', 'Sun', 'Sun']

#2


1  

dict_month is a list of dicts and values can be append in a new list which is jan_month.

dict_month是一个dicts列表,值可以附加在jan_month的新列表中。

>>> dict_month = [{'January': 'Sun'}, {'January': 'Sun'}, {'January': 'Sun'}, {'January': 'Sun'}]
>>> jan_month = []
>>> for x in dict_month:
...     jan_month.append(x['January'])
...
>>> jan_month
['Sun', 'Sun', 'Sun', 'Sun']

#3


0  

[{'January': 'Sun'}, {'January': 'Sun'}, {'January': 'Sun'}, {'January': 'Sun'}]

Is not a dict

不是一个字典

a dict is something that is enclosed in {}

dict是{}中包含的内容

 {'January': 'Sun', 'January': 'Sun','January': 'Sun','January': 'Sun'}

Should be your dict

应该是你的dict

Anything enclosed in a [] is a list

[]中包含的任何内容都是一个列表

So technically

[{'January': 'Sun'}, {'January': 'Sun'}, {'January': 'Sun'}, {'January': 'Sun'}]

is a list, and each item of the list is a dict with 1 key,value pair

是一个列表,列表中的每个项目都是一个带有1个键值对的字典

x = [{'January': 'Sun'}, {'January': 'Sun'}, {'January': 'Sun'}, {'January': 'Sun'}]

xyz = []

for y in x:
     xyz.append(y['January'])

print(xyz)

#1


2  

Hope it may help you:

希望它可以帮助你:

dict_month = [{'January': 'Sun'}, {'January': 'Sun'}, {'January': 'Sun'}, {'January': 'Sun'}]  

jan_month =  [x['January'] for x in dict_month]

>>>['Sun', 'Sun', 'Sun', 'Sun']

#2


1  

dict_month is a list of dicts and values can be append in a new list which is jan_month.

dict_month是一个dicts列表,值可以附加在jan_month的新列表中。

>>> dict_month = [{'January': 'Sun'}, {'January': 'Sun'}, {'January': 'Sun'}, {'January': 'Sun'}]
>>> jan_month = []
>>> for x in dict_month:
...     jan_month.append(x['January'])
...
>>> jan_month
['Sun', 'Sun', 'Sun', 'Sun']

#3


0  

[{'January': 'Sun'}, {'January': 'Sun'}, {'January': 'Sun'}, {'January': 'Sun'}]

Is not a dict

不是一个字典

a dict is something that is enclosed in {}

dict是{}中包含的内容

 {'January': 'Sun', 'January': 'Sun','January': 'Sun','January': 'Sun'}

Should be your dict

应该是你的dict

Anything enclosed in a [] is a list

[]中包含的任何内容都是一个列表

So technically

[{'January': 'Sun'}, {'January': 'Sun'}, {'January': 'Sun'}, {'January': 'Sun'}]

is a list, and each item of the list is a dict with 1 key,value pair

是一个列表,列表中的每个项目都是一个带有1个键值对的字典

x = [{'January': 'Sun'}, {'January': 'Sun'}, {'January': 'Sun'}, {'January': 'Sun'}]

xyz = []

for y in x:
     xyz.append(y['January'])

print(xyz)