dict实现哪些方法:
1)
#fromkeys方法
'''
@staticmethod # known case
def fromkeys(*args, **kwargs): # real signature unknown
""" Create a new dictionary with keys from iterable and values set to value. """
pass
'''
'''
fromkeys将可迭代对象转化成dict
'''
list2 = ['tom','jarry','mark']
new_dict = dict.fromkeys(list2,'cartion')
print(new_dict)
---------------------------------------------
{'tom': 'cartion', 'jarry': 'cartion', 'mark': 'cartion'}
#get方法 可以根据key值获取value值,并可以设置默认值
'''
def get(self, *args, **kwargs): # real signature unknown
""" Return the value for key if key is in the dictionary, else default. """
pass
'''
list2 = ['tom','jarry','mark']
new_dict = dict.fromkeys(list2,'cartion')
print(new_dict)
com = new_dict.get('tom2','not find')
print(com)
------------------------------------------------------
{'tom': 'cartion', 'jarry': 'cartion', 'mark': 'cartion'}
not find
#items 获取dict元素
'''
def items(self): # real signature unknown; restored from __doc__
""" D.items() -> a set-like object providing a view on D's items """
pass
'''
list2 = ['tom','jarry','mark']
new_dict = dict.fromkeys(list2,'cartion')
print(new_dict)
for item in new_dict.items():
print(item)
-----------------------------------------------------------------
{'tom': 'cartion', 'jarry': 'cartion', 'mark': 'cartion'}
('tom', 'cartion')
('jarry', 'cartion')
('mark', 'cartion')
#setdefault
'''
def setdefault(self, *args, **kwargs): # real signature unknown
"""
Insert key with a value of default if key is not in the dictionary.
Return the value for key if key is in the dictionary, else default.
"""
pass
'''
list2 = ['tom','jarry','mark']
new_dict = dict.fromkeys(list2,'cartion')
print(new_dict)
new_dict.setdefault('autom','name')
print(new_dict)
-------------------------------------------------------------
{'tom': 'cartion', 'jarry': 'cartion', 'mark': 'cartion'}
{'tom': 'cartion', 'jarry': 'cartion', 'mark': 'cartion', 'autom': 'name'}
#update
'''
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
'''
list1 = {'tom': 'cartion', 'jarry': 'cartion', 'mark': 'cartion'}
list1.update({'key':'value'})
print(list1)
--------------------------------------------------------------------------
{'tom': 'cartion', 'jarry': 'cartion', 'mark': 'cartion', 'key': 'value'}
list1 = {'tom': 'cartion', 'jarry': 'cartion', 'mark': 'cartion'}
list1.update(key1='value1',key2='value2')
print(list1)
-----------------------------------------------------------------------
{'tom': 'cartion', 'jarry': 'cartion', 'mark': 'cartion', 'key1': 'value1', 'key2': 'value2'}
list1 = {'tom': 'cartion', 'jarry': 'cartion', 'mark': 'cartion'}
list1.update([('key','value'),('key1','value'),('key2','value')])
print(list1)
----------------------------------------------------------
{'tom': 'cartion', 'jarry': 'cartion', 'mark': 'cartion', 'key': 'value', 'key1': 'value', 'key2': 'value'}
查看dict类,看下其实现了哪些方法
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): # real signature unknown
""" Create a new dictionary with keys from iterable and values set to value. """
pass
def get(self, *args, **kwargs): # real signature unknown
""" Return the value for key if key is in the dictionary, else default. """
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, default is returned if given, otherwise KeyError is raised
"""
pass
def popitem(self, *args, **kwargs): # real signature unknown
"""
Remove and return a (key, value) pair as a 2-tuple.
Pairs are returned in LIFO (last-in, first-out) order.
Raises KeyError if the dict is empty.
"""
pass
def setdefault(self, *args, **kwargs): # real signature unknown
"""
Insert key with a value of default if key is not in the dictionary.
Return the value for key if key is in the dictionary, else default.
"""
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
def __class_getitem__(self, *args, **kwargs): # real signature unknown
""" See PEP 585 """
pass
def __contains__(self, *args, **kwargs): # real signature unknown
""" True if the dictionary has the specified key, else False. """
pass
def __delitem__(self, *args, **kwargs): # real signature unknown
""" Delete self[key]. """
pass
def __eq__(self, *args, **kwargs): # real signature unknown
""" Return self==value. """
pass
def __getattribute__(self, *args, **kwargs): # real signature unknown
""" Return getattr(self, name). """
pass
def __getitem__(self, y): # real signature unknown; restored from __doc__
""" x.__getitem__(y) <==> x[y] """
pass
def __ge__(self, *args, **kwargs): # real signature unknown
""" Return self>=value. """
pass
def __gt__(self, *args, **kwargs): # real signature unknown
""" Return self>value. """
pass
def __init__(self, seq=None, **kwargs): # known special case of dict.__init__
"""
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)
# (copied from class doc)
"""
pass
def __ior__(self, *args, **kwargs): # real signature unknown
""" Return self|=value. """
pass
def __iter__(self, *args, **kwargs): # real signature unknown
""" Implement iter(self). """
pass
def __len__(self, *args, **kwargs): # real signature unknown
""" Return len(self). """
pass
def __le__(self, *args, **kwargs): # real signature unknown
""" Return self<=value. """
pass
def __lt__(self, *args, **kwargs): # real signature unknown
""" Return self<value. """
pass
@staticmethod # known case of __new__
def __new__(*args, **kwargs): # real signature unknown
""" Create and return a new object. See help(type) for accurate signature. """
pass
def __ne__(self, *args, **kwargs): # real signature unknown
""" Return self!=value. """
pass
def __or__(self, *args, **kwargs): # real signature unknown
""" Return self|value. """
pass
def __repr__(self, *args, **kwargs): # real signature unknown
""" Return repr(self). """
pass
def __reversed__(self, *args, **kwargs): # real signature unknown
""" Return a reverse iterator over the dict keys. """
pass
def __ror__(self, *args, **kwargs): # real signature unknown
""" Return value|self. """
pass
def __setitem__(self, *args, **kwargs): # real signature unknown
""" Set self[key] to value. """
pass
def __sizeof__(self): # real signature unknown; restored from __doc__
""" D.__sizeof__() -> size of D in memory, in bytes """
pass
__hash__ = None