字典 (dict
)
儲存字串到其他物件的映射。請參閱字典。
您也可以使用 foreach
陳述式 迭代字典。
(自 0.48.0 起): 字典可以相加 (例如 d1 = d2 + d3
和 d1 += d2
)。第二個字典的值會覆蓋第一個字典的值。(自 0.62.0 起): 字典的順序保證為插入順序。
字典方法
dict.get()
如果字典中存在以第一個參數給定的鍵,則返回該鍵的值,否則返回作為第二個參數給定的可選回退值。如果只給定一個參數且未找到該鍵,則會導致嚴重錯誤
簽名
# returns the value for the key given as first
any get(
str key, # The key to query
any [fallback], # Fallback value that is returned if the key is not in the dict
.
)
參數
此函式不支援參數扁平化。
方法 dict.get()
接受以下位置參數
名稱 | 類型 | 描述 | 標籤 |
---|---|---|---|
key |
str |
要查詢的鍵。 |
|
fallback |
any |
如果鍵不在 |
[可選] |
dict.has_key()
如果字典包含以參數給定的鍵,則返回 true
,否則返回 false
。
簽名
# Returns `true` if the dictionary contains the key given as argument, `false` otherwise
bool has_key(
str key, # The key to query
)
參數
方法 dict.has_key()
接受以下位置參數
名稱 | 類型 | 描述 | 標籤 |
---|---|---|---|
key |
str |
要查詢的鍵。 |
|
dict.keys()
返回字典中鍵的陣列。
簽名
list[str] keys()
搜尋結果為