在遍历多个 for 循环时创建字典?
Creating a dictionary while iterating through multiple for loops?
我正在将总统演讲的日期和每个演讲各自的 filename
存储在字典中。 speeches
对象如下所示:
[<a href="/president/obama/speeches/speech-4427">Acceptance Speech at the Democratic National Convention (August 28, 2008)</a>,
<a href="/president/obama/speeches/speech-4424">Remarks on Election Night (November 4, 2008)</a>,...]
而 end_link
看起来像:
['/president/obama/speeches/speech-4427', '/president/obama/speeches/speech-4424',...]
这是我的代码:
date_dict = {}
for speech in speeches:
text = speech.get_text(strip=True)
date = text[text.index("(") + 1:text.rindex(")")]
end_link = [tag.get('href') for tag in speeches if tag.get('href') is not None]
for x in end_link:
splitlink = x.split("/")
president = splitlink[2]
speech_num = splitlink[4]
filename = "{0}_{1}".format(president,speech_num)
if 2 == 2:
f = date_dict['{0} {1}'.format(filename,date)]
我得到了正确的日期输出(例如 August 15, 1999
)并且 filename
没问题。现在,我只是想加入两者并收到以下错误:
date_dict['{0} {1}'.format(filename,date)]
KeyError: 'obama_speech-4427 August 28, 2008'
我真的不知道从这里到哪里去。
您没有将该键的值设置为任何值,因此 Python 认为您正在尝试读取该键。 date_dict 字典为空。
你需要设置一个值,所以像这样:
date_dict[date] = filename
字典有键和值。要分配给字典,您可以这样做:
date_dict['key'] = value
连接部分没有问题。 '{0} {1}'.format(filename,date)
很好,尽管您可能需要下划线而不是 space。如果要在网站上发布,也可能是破折号。
Related question about KeyError
编辑
根据我们的讨论,我认为您需要这样做:
date_dict = {}
for speech in speeches:
text = speech.get_text(strip=True)
date = text[text.index("(") + 1:text.rindex(")")]
end_link = [tag.get('href') for tag in speeches if tag.get('href') is not None]
for x in end_link:
splitlink = x.split("/")
president = splitlink[2]
speech_num = splitlink[4]
filename = "{0}_{1}".format(president,speech_num)
if 2 == 2:
date_dict[filename] = date
# Prints name date for a given file(just an example)
print("File", filname, "recorded on", date_dict[filename])
我正在将总统演讲的日期和每个演讲各自的 filename
存储在字典中。 speeches
对象如下所示:
[<a href="/president/obama/speeches/speech-4427">Acceptance Speech at the Democratic National Convention (August 28, 2008)</a>,
<a href="/president/obama/speeches/speech-4424">Remarks on Election Night (November 4, 2008)</a>,...]
而 end_link
看起来像:
['/president/obama/speeches/speech-4427', '/president/obama/speeches/speech-4424',...]
这是我的代码:
date_dict = {}
for speech in speeches:
text = speech.get_text(strip=True)
date = text[text.index("(") + 1:text.rindex(")")]
end_link = [tag.get('href') for tag in speeches if tag.get('href') is not None]
for x in end_link:
splitlink = x.split("/")
president = splitlink[2]
speech_num = splitlink[4]
filename = "{0}_{1}".format(president,speech_num)
if 2 == 2:
f = date_dict['{0} {1}'.format(filename,date)]
我得到了正确的日期输出(例如 August 15, 1999
)并且 filename
没问题。现在,我只是想加入两者并收到以下错误:
date_dict['{0} {1}'.format(filename,date)]
KeyError: 'obama_speech-4427 August 28, 2008'
我真的不知道从这里到哪里去。
您没有将该键的值设置为任何值,因此 Python 认为您正在尝试读取该键。 date_dict 字典为空。
你需要设置一个值,所以像这样:
date_dict[date] = filename
字典有键和值。要分配给字典,您可以这样做:
date_dict['key'] = value
连接部分没有问题。 '{0} {1}'.format(filename,date)
很好,尽管您可能需要下划线而不是 space。如果要在网站上发布,也可能是破折号。
Related question about KeyError
编辑
根据我们的讨论,我认为您需要这样做:
date_dict = {}
for speech in speeches:
text = speech.get_text(strip=True)
date = text[text.index("(") + 1:text.rindex(")")]
end_link = [tag.get('href') for tag in speeches if tag.get('href') is not None]
for x in end_link:
splitlink = x.split("/")
president = splitlink[2]
speech_num = splitlink[4]
filename = "{0}_{1}".format(president,speech_num)
if 2 == 2:
date_dict[filename] = date
# Prints name date for a given file(just an example)
print("File", filname, "recorded on", date_dict[filename])