Python之字典方法
def clear(self): # 清除所有内容
""" D.clear() -> None. Remove all items from D. """
pass
def copy(self): # 浅复制
""" D.copy() -> a shallow copy of D """
def fromkeys(*args, **kwargs): # real signature unknown
""" Returns a new dict with keys from iterable and values equal to value. """
pass
def get(self, k, d=None): # 根据字典的key获取值,如果key不存在,返回None。
""" D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None. """
pass
def items(self): # 获取字典所有的键值对
""" D.items() -> a set-like object providing a view on D's items """
pass
example:
dic = {
1: 'abc',
2: 'def',
3: 'hjk'
}
for k,v in dic.items():
print(k,v)
1 abc #返回结果
2 def
3 hjk
def keys(self): # 获取字典所有的key
""" D.keys() -> a set-like object providing a view on D's keys """
pass
def pop(self, k, d=None): # 获取并在字典中移除,可指定移除。
"""
D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
If key is not found, d is returned if given, otherwise KeyError is raised
"""
pass
def popitem(self): # 获取并在字典中移除,只能从尾部移除。
"""
D.popitem() -> (k, v), remove and return some (key, value) pair as a
2-tuple; but raise KeyError if D is empty.
"""
pass
def setdefault(self, k, d=None): # real signature unknown; restored from __doc__
""" D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D """
pass
def update(self, E=None, **F): # 批量更新
"""
D.update([E, ]**F) -> None. Update D from dict/iterable E and F.
If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]
If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v
In either case, this is followed by: for k in F: D[k] = F[k]
"""
example:
dic = {
1: 'abc',
2: 'def',
3: 'hjk'
}
test = {
4: 'lmn',
5: 'opq'
}
dic.update(test)
print(dic)
{1: 'abc', 2: 'def', 3: 'hjk', 4: 'lmn', 5: 'opq'} #返回结果
def values(self): # 获取字典所有的值
""" D.values() -> an object providing a view on D's values """
pass
posted on 2018-02-05 15:10 大道至简,规则无上。 阅读(126) 评论(0) 编辑 收藏 举报