python——字典

字典(dict):
1、基本结构: info = {
"k1:v1", 键值对,
"k2:v2"
}
2、字典的v可以是任何值
3、字典的k可以是字符串、数字,可以是元祖,不可以是列表、字典,
4、字典不可以作为字典的k
5、字典不是有序的
2、取值 v = info(k1)
print(k1)
可以索引
可以删除
支持for循环



class dict(object):(字典)

dict() -> new empty dictionary
dict(mapping) -> new dictionary initialized from a mapping object's
(key, value) pairs
dict(iterable) -> new dictionary initialized as if via:
d = {}
for k, v in iterable:
d[k] = v
dict(**kwargs) -> new dictionary initialized with the name=value pairs
in the keyword argument list. For example: dict(one=1, two=2)
"""
def clear(self): # real signature unknown; restored from __doc__(清空)
""" D.clear() -> None. Remove all items from D. """
pass

def copy(self): # real signature unknown; restored from __doc__(浅拷贝)
""" D.copy() -> a shallow copy of D """
pass

@staticmethod # known case(静态方法)
def fromkeys(*args, **kwargs(第一个参数作为k,第二个参数作为value)): # real signature unknown(根据序列,创建字典,并制定第一个值)
""" Returns a new dict with keys from iterable and values equal to value. """
pass

def get(self, k, d=None): # real signature unknown; restored from __doc__(genjukey获取值,key不存在时,可以指定默认值)
""" D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None. """
pass

def items(self): # real signature unknown; restored from __doc__(可以进行循环)
""" D.items() -> a set-like object providing a view on D's items """
pass

def keys(self): # real signature unknown; restored from __doc__
""" D.keys() -> a set-like object providing a view on D's keys """
pass

def pop(self, k, d=None): # real signature unknown; restored from __doc__(即可以删除又可以得到我删除的那个值)
"""
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): # real signature unknown; restored from __doc__(随机删除一个值)
"""
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__(设置值,已经存在就不设置获取当前k对应的值,如果不存在获取当前的值)
""" 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): # known special case of dict.update(更新,已经存在的进行更新,不存在的更新进去)
"""
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]
"""
pass

def values(self): # real signature unknown; restored from __doc__(循环键值对的值)
""" D.values() -> an object providing a view on D's values """
pass

猜你喜欢

转载自www.cnblogs.com/ydp616938947/p/8955917.html
今日推荐