我不明白字典「count」是如何由「List」填充和引用的。使用列表填充字典if語句
具體來說,爲什麼列表('列表')中的項目與「if item in count」語句一起被添加到字典('count')?
'count'字典是空的,並且沒有'append'功能。
這裏是Python功能:
def countDuplicates(List):
count = {}
for item in List:
if item in count:
count[item] += 1
else:
count[item] = 1
return count
print(countDuplicates([1, 2, 4, 3, 2, 2, 5]))
輸出:{1: 1, 2: 3, 3: 1, 4: 1, 5: 1}
這段代碼有什麼問題嗎?這是你的代碼嗎? –
歡迎來到SO。請花時間閱讀[問]及其中包含的鏈接。您可能還需要投入一些時間來完成[教程](https://docs.python.org/3/tutorial/index.html)。 – wwii
我認爲這個問題很明顯。第二段詢問'in'的作用,第三段詢問如何在沒有'append'的情況下將內容添加到'dict'中。 – luther