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 (key, value) 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 (key, value) pair |
a.iteritems() |
return an iterator over (key, value) 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/8440848
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。