可能重複:
What is a good way to test if a Key exists in Python Dictionary如何測試字典是否包含特定的密鑰?
什麼是測試如果字典有一個關鍵的乾淨的方式?
x = {'a' : 1, 'b' : 2}
if (x.contains_key('a')):
....
可能重複:
What is a good way to test if a Key exists in Python Dictionary如何測試字典是否包含特定的密鑰?
什麼是測試如果字典有一個關鍵的乾淨的方式?
x = {'a' : 1, 'b' : 2}
if (x.contains_key('a')):
....
'a' in x
和快速搜索發現有關它的一些不錯的信息:http://docs.python.org/3/tutorial/datastructures.html#dictionaries
的[教程](http://docs.python.org/tutorial/datastructures.html#dictionaries)是你的朋友。 – 2011-03-15 13:59:24
-1由於重複的問題。 – 2011-03-15 14:33:17
@Steven - 我相信重複是有幫助的,因爲它們使這個問題更容易找到。我在發佈之前做過搜索,沒有找到我要找的內容。 – ripper234 2011-03-15 15:09:17