我很难理解if语句条件的求值顺序:
假设我有一本这样的字典,它将单词映射到一系列网页:index = { WORD, [url1,url2,url3] }
插入此索引时有两种情况:
1)索引中不存在关键字(WORD),需要创建一个列表并将WORD设置为
输入地图
2)索引中已经存在关键字(WORD),我只需要将当前的url追加到字典中已经存在的列表中
我期望的工作:
^{pr2}$
但这只允许每个单词有一个url。在
什么起作用了:def update_index(word, url):
if word in index: # <- isnt having two consecutive if statements
# the same as an AND???
if not(url in index[word]):
index[word].append(url) # list already exists append to it
else:
index[word] = [url] # new list with url as a single element
如果你能帮我解决这个问题,我将不胜感激。在