dict方法

  1 class dict(object):
  2     """
  3     dict() -> new empty dictionary
  4     dict(mapping) -> new dictionary initialized from a mapping object's
  5         (key, value) pairs
  6     dict(iterable) -> new dictionary initialized as if via:
  7         d = {}
  8         for k, v in iterable:
  9             d[k] = v
 10     dict(**kwargs) -> new dictionary initialized with the name=value pairs
 11         in the keyword argument list.  For example:  dict(one=1, two=2)
 12     """
 13     def clear(self): # real signature unknown; restored from __doc__
 14         """ D.clear() -> None.  Remove all items from D. """
 15         pass
 16 
 17     def copy(self): # real signature unknown; restored from __doc__
 18         """ D.copy() -> a shallow copy of D """
 19         pass
 20 
 21     @staticmethod # known case
 22     def fromkeys(*args, **kwargs): # real signature unknown
 23         """ Create a new dictionary with keys from iterable and values set to value. """
 24         pass
 25 
 26     def get(self, *args, **kwargs): # real signature unknown
 27         """ Return the value for key if key is in the dictionary, else default. """
 28         pass
 29 
 30     def items(self): # real signature unknown; restored from __doc__
 31         """ D.items() -> a set-like object providing a view on D's items """
 32         pass
 33 
 34     def keys(self): # real signature unknown; restored from __doc__
 35         """ D.keys() -> a set-like object providing a view on D's keys """
 36         pass
 37 
 38     def pop(self, k, d=None): # real signature unknown; restored from __doc__
 39         """
 40         D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
 41         If key is not found, d is returned if given, otherwise KeyError is raised
 42         """
 43         pass
 44 
 45     def popitem(self): # real signature unknown; restored from __doc__
 46         """
 47         D.popitem() -> (k, v), remove and return some (key, value) pair as a
 48         2-tuple; but raise KeyError if D is empty.
 49         """
 50         pass
 51 
 52     def setdefault(self, *args, **kwargs): # real signature unknown
 53         """
 54         Insert key with a value of default if key is not in the dictionary.
 55         
 56         Return the value for key if key is in the dictionary, else default.
 57         """
 58         pass
 59 
 60     def update(self, E=None, **F): # known special case of dict.update
 61         """
 62         D.update([E, ]**F) -> None.  Update D from dict/iterable E and F.
 63         If E is present and has a .keys() method, then does:  for k in E: D[k] = E[k]
 64         If E is present and lacks a .keys() method, then does:  for k, v in E: D[k] = v
 65         In either case, this is followed by: for k in F:  D[k] = F[k]
 66         """
 67         pass
 68 
 69     def values(self): # real signature unknown; restored from __doc__
 70         """ D.values() -> an object providing a view on D's values """
 71         pass
 72 
 73     def __contains__(self, *args, **kwargs): # real signature unknown
 74         """ True if the dictionary has the specified key, else False. """
 75         pass
 76 
 77     def __delitem__(self, *args, **kwargs): # real signature unknown
 78         """ Delete self[key]. """
 79         pass
 80 
 81     def __eq__(self, *args, **kwargs): # real signature unknown
 82         """ Return self==value. """
 83         pass
 84 
 85     def __getattribute__(self, *args, **kwargs): # real signature unknown
 86         """ Return getattr(self, name). """
 87         pass
 88 
 89     def __getitem__(self, y): # real signature unknown; restored from __doc__
 90         """ x.__getitem__(y) <==> x[y] """
 91         pass
 92 
 93     def __ge__(self, *args, **kwargs): # real signature unknown
 94         """ Return self>=value. """
 95         pass
 96 
 97     def __gt__(self, *args, **kwargs): # real signature unknown
 98         """ Return self>value. """
 99         pass
100 
101     def __init__(self, seq=None, **kwargs): # known special case of dict.__init__
102         """
103         dict() -> new empty dictionary
104         dict(mapping) -> new dictionary initialized from a mapping object's
105             (key, value) pairs
106         dict(iterable) -> new dictionary initialized as if via:
107             d = {}
108             for k, v in iterable:
109                 d[k] = v
110         dict(**kwargs) -> new dictionary initialized with the name=value pairs
111             in the keyword argument list.  For example:  dict(one=1, two=2)
112         # (copied from class doc)
113         """
114         pass
115 
116     def __iter__(self, *args, **kwargs): # real signature unknown
117         """ Implement iter(self). """
118         pass
119 
120     def __len__(self, *args, **kwargs): # real signature unknown
121         """ Return len(self). """
122         pass
123 
124     def __le__(self, *args, **kwargs): # real signature unknown
125         """ Return self<=value. """
126         pass
127 
128     def __lt__(self, *args, **kwargs): # real signature unknown
129         """ Return self<value. """
130         pass
131 
132     @staticmethod # known case of __new__
133     def __new__(*args, **kwargs): # real signature unknown
134         """ Create and return a new object.  See help(type) for accurate signature. """
135         pass
136 
137     def __ne__(self, *args, **kwargs): # real signature unknown
138         """ Return self!=value. """
139         pass
140 
141     def __repr__(self, *args, **kwargs): # real signature unknown
142         """ Return repr(self). """
143         pass
144 
145     def __setitem__(self, *args, **kwargs): # real signature unknown
146         """ Set self[key] to value. """
147         pass
148 
149     def __sizeof__(self): # real signature unknown; restored from __doc__
150         """ D.__sizeof__() -> size of D in memory, in bytes """
151         pass
152 
153     __hash__ = None
dict源码

1.clear()

  清空字典

2.copy()

  深拷贝父对象,浅拷贝子对象,

dict1 = {'a': 1,'b': 2,'c': [1,2]}
dict2 = dict1     # {'a': 1,'b': 2,'c': [1,2]}
dict3 = dict1.copy()    #{'a': 1,'b': 2,'c': [1,2]}
dict1['c'][1] = 3
dict2    # {'a': 1,'b': 2,'c': [1,3]}   浅拷贝
dict3    # {'a': 1,'b': 2,'c': [1,2]}   深拷贝
View Code

3.fromkeys()

  静态方法,该方法可返回一个新字典,fromkeys会接收两个参数,第一个参数为从外部传入的可迭代对象,会将循环取出元素作为字典的key值,另外一个参数是字典的value值,若这个值存在则所有的key值所对应的value值均为None,反之则为默认的值。

4.get()

  返回字典中键为传参的值,若不存在这个键,则返回默认值,一般为None。

5.items()

  返回一个可迭代的dict_items,其中为所有的键值对,每一对都以元组形式保存。

6.keys()

  返回一个可迭代的dict_keys,其中为字典中的所有键

7.pop()

  参数为想要删除的键名,返回该键所对应的的值,当字典为空时,调用此方法,会产生异常。

8.popitem() 

  随机返回并删除字典中的最后一对键和值。当字典为空时,调用此方法,会产生异常。

9.setdefault()

  如果字典中包含有给定键,则返回该键对应的值,否则返回为该键设置的值

10.update()

  更新字典,把传入字典添加到本字典中。

11.values()

  返回一个可迭代的dict_values,其中为字典中的所有值

12.dict中的内置函数

__contains__                   确认字典中是否包含这个键
__delitem__                    删除  del dict[key] 
__eq__                            判断是否相同
__getattribute__                获取字典的属性
__getitem__                    返回值
__ge__                            判断值是否大于
__iter__                            可迭代
__len__                            返回字典长度  len(dict)
__le__                              判断值是否小于等于
__lt__                                判断值是否小于
__new__                         新建并返回一个字典对象    dict()
__ne__                            判断值是否不同
__repr__                           直接输出时的内容
__setitem__                    设置对应键的值  dict[key] = value
__sizeof__                        查看字典所占内存

 

posted @ 2020-04-14 18:49  爱学习的红领巾  阅读(199)  评论(0编辑  收藏  举报