Python Dict用法

 

Operation Result
len(a) the number of items in a 獲得字典中元素的個數
a[k] the item of a with key k 取得鍵K所對應的值
a[k] = v set a[k] to v 設定鍵k所對應的值成爲v
del a[k] remove a[k] from a 從字典中刪除鍵爲k的元素
a.clear() remove all items from a 清空整個字典
a.copy() a (shallow) copy of a 獲得字典副本
k in a True if a has a key k, else False 字典中存在鍵k則爲返回True,沒有則返回False
k not in a Equivalent to not k in a   字典中不存在鍵k則爲返回true,反之返回False
a.has_key(k) Equivalent to k in a, use that form in new code 等價於k in a
a.items() a copy of a's list of (keyvalue) pairs 獲得一個鍵,值的list
a.keys() a copy of a's list of keys 獲得鍵的list
a.update([b]) updates (and overwrites) key/value pairs from b從b字典中更新a字典,若是鍵相同則更新,a中不存在則追加
a.fromkeys(seq[value]) Creates a new dictionary with keys from seq and values set to value 
a.values() a copy of a's list of values
a.get(k[x]) a[k] if k in a, else x
a.setdefault(k[x]) a[k] if k in a, else x (also setting it)
a.pop(k[x]) a[k] if k in a, else x (and remove k)
a.popitem() remove and return an arbitrary (keyvalue) pair
a.iteritems() return an iterator over (keyvalue) pairs
a.iterkeys() return an iterator over the mapping's keys
a.itervalues() return an iterator over the mapping's values

 

參照來源 -- http://blog.csdn.net/wangran51/article/details/8440848app

相關文章
相關標籤/搜索