欢迎来到Louis的博客

人生三从境界:昨夜西风凋碧树,独上高楼,望尽天涯路。 衣带渐宽终不悔,为伊消得人憔悴。 众里寻他千百度,蓦然回首,那人却在灯火阑珊处。
扩大
缩小

python 字典dict方法

dict方法

字典(dict)
  特性
    1.由键值对组成的序列。
    2.字典是无序的(非线性序列)。 字典的定义(初始化):
    d = dict()
    d1 = {'k1':'v1','k2','v2'} 生成字典方法: d1
= {'k1':v1,'k2':'v2'}
  
  字典方法:
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)
    #字典初始化,可接收name=value形式的参数,生成字典
------------------------------------------------------------------------------
example:
>>> d1 = dict(k1 = 'v1', age = 27, name = 'handsome man')

>>> print(d1)
  
{'k1': 'v1', 'age': 27, 'name': 'handsome man'}
------------------------------------------------------------------------------
    """
def clear(self): # real signature unknown; restored from __doc__
""" D.clear() -> None. Remove all items from D. """
pass
  #删除列表中的所有元素。
-------------------------------------------------------------------------------
example:

>>> print(d1)
{'k1': 'v1', 'age': 27, 'name': 'handsome man'}
>>> d1.clear()
>>> print(d1)
{}

-------------------------------------------------------------------------------


-------------------------------------------------------------------------------
-------------------------------------------------------------------------------

def copy(self): # real signature unknown; restored from __doc__
""" D.copy() -> a shallow copy of D """
pass
  #复制,浅复制,只复制字段第一层的元素。

-------------------------------------------------------------------------------

>>> d1 = dict(k1 = 'v1', age = 27, name = 'handsome man')
>>> d2 = d1.copy()
>>> print(d2)
{'k1': 'v1', 'age': 27, 'name': 'handsome man'}

>>> d3 = {'k1':[1,2,3,4,5,6]}

>>> d4 = d3.copy()
>>> d3['k1'].insert(0,100)
>>> print(d3,d4)
{'k1': [100, 1, 2, 3, 4, 5, 6]} {'k1': [100, 1, 2, 3, 4, 5, 6]}
>>>

-------------------------------------------------------------------------------
  

@staticmethod # known case
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): # real signature unknown; restored from __doc__
""" D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None. """
pass
  #获取键k对应的value,如果k在字典中返回value,否则返回d,d默认值为none
-------------------------------------------------------------------------------

>>> d1
{'k1': 'v1', 'age': 27, 'name': 'handsome man'}
>>> d1.get('k1')
'v1'
>>> d1.get('k2')
>>> print(d1.get('k2'))
None

>>> d1.get('k2','nothiskey')
'nothiskey'

-------------------------------------------------------------------------------

def items(self): # real signature unknown; restored from __doc__
""" D.items() -> a set-like object providing a view on D's items """
  #返回一个键值对组成的,dict_items对象(可迭代)
pass
------------------------------------------------------------------------------

>>> d1.items()
dict_items([('k1', 'v1'), ('age', 27), ('name', 'handsome man')])
>>> type(d1.items())
<class 'dict_items'>
>>> for i,j in d1.items():
... print(i,j)
...
k1 v1
age 27
name handsome man
>>>

------------------------------------------------------------------------------


def keys(self): # real signature unknown; restored from __doc__
""" D.keys() -> a set-like object providing a view on D's keys """
pass
  #返回一个key组成的,dict_keys对象(可迭代)
------------------------------------------------------------------------------

>>> d1.keys()
dict_keys(['k1', 'age', 'name'])
>>> type(d1.keys())
<class 'dict_keys'>
>>> for i in d1.keys():
... print(i)
...
k1
age
name

------------------------------------------------------------------------------
    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
  #移除一个键值对,并反馈该键所对应的值。
------------------------------------------------------------------------------

>>> d1
{'k1': 'v1', 'age': 27, 'name': 'handsome man'}
>>> d1.pop('k1')
'v1'

------------------------------------------------------------------------------
    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
  #随机移除一个键值对,并返回该键值对组成的元组
------------------------------------------------------------------------------

>>> d1
{'k1': 'v1', 'age': 27, 'name': 'handsome man'}
>>> d1.popitem()
('name', 'handsome man')
>>> d1
{'k1': 'v1', 'age': 27}
>>>

------------------------------------------------------------------------------
    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): # 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
  #用一个字典来更新自己。
------------------------------------------------------------------------------

>>> d2 = {'name':'handsome man','k2':'v2'}
>>> d1 = dict(k1 = 'v1', age = 27, name = 'handsome man')
>>> d2.update(d1)
>>> d2
{'name': 'handsome man', 'k2': 'v2', 'k1': 'v1', 'age': 27}

------------------------------------------------------------------------------

  
def values(self): # real signature unknown; restored from __doc__
""" D.values() -> an object providing a view on D's values """
pass
  #返回value组成的dict_values对象(可迭代)
-------------------------------------------------------------------------------

>>> d1
{'k1': 'v1', 'age': 27, 'name': 'handsome man'}
>>> d1.values()
dict_values(['v1', 27, 'handsome man'])
>>> for i in d1.values():
... print(i)
...
v1
27
handsome man

-----------------------------------------------------------------------------------

 

posted on 2017-12-20 15:56  Louiszj  阅读(3549)  评论(0编辑  收藏  举报

导航