python3.x 基础一:dict字典

时间:2022-04-18 22:11:05

字典,{key,value}

help(dict)

  • 定义一个字典:

 

>>> dict1
{
'salary': '100', 'age': '18', 'name': 'yzw'}
>>> dict2=dict1
>>> id(dict2)
139948810542280
>>> id(dict1)
139948810542280
>>> dict2=dict1.copy()
>>> id(dict1),id(dict2)
(
139948810542280, 139948887698696)

 

  •  |  clear(...)  清空一个字典
     |      D.clear() -> None.  Remove all items from D.
    >>> dict2.clear()
    >>> dict2
    {}
  •  |  copy(...)  普通赋值指向相同的内存地址
     |      D.copy() -> a shallow copy of D
    >>> dict2=dict1.copy()
    >>> id(dict1),id(dict2)
    (
    139948810542280, 139948887698696)
  •  |  fromkeys(iterable, value=None, /) from builtins.type  创建一个以序列为key,值相同的字典
     |      Returns a new dict with keys from iterable and values equal to value.

    >>> seq1=('length','width','square')
    >>> dict3=dict.fromkeys(seq1)
    >>> dict3
    {
    'length': None, 'width': None, 'square': None}
    >>> dict4=dict.fromkeys(seq1,10)
    >>> dict4
    {
    'length': 10, 'width': 10, 'square': 10}
  •  |  get(...)  根据key取value,key不存在则返回None   dict[key]取值key不存在时报错
     |      D.get(k[,d]) -> D[k] if k in D, else d.  d defaults to None.
    >>> dict1
    {
    'salary': '100', 'age': '18', 'name': 'yzw'}
    >>> dict1.get('salary')
    '100'
    >>> dict1.get('sex')
    >>> dict1['salary']
    '100'
    >>> dict1['sex']
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    KeyError: 'sex'
  •  |  items(...)  以列表返回可遍历的(键, 值) 元组数组
     |      D.items() -> a set-like object providing a view on D's items
    >>> dict1.items()
    dict_items([(
    'salary', '100'), ('age', '18'), ('name', 'yzw')]) 
  •  |  keys(...)  返回所有key的列表
     |      D.keys() -> a set-like object providing a view on D's keys
    >>> dict1.keys()
    dict_keys([
    'salary', 'age', 'name'])   
  • |  pop(...)  删除字典给定键 key 所对应的值,返回值为被删除的值。key值必须给出。 否则,返回default值
     |      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
    >>> dict1.pop('salary')
    '100'
    >>> dict1
    {
    'age': '18', 'name': 'yzw'}
    >>> dict1.pop('sex')
    Traceback (most recent call last):
    File
    "<stdin>", line 1, in <module>
    KeyError:
    'sex'
    >>> dict1['sex']='man'
    >>> dict1
    {'sex': 'man', 'age': '18', 'name': 'yzw'}
  •  |  popitem(...) 随机删除一个k/v
     |      D.popitem() -> (k, v), remove and return some (key, value) pair as a
     |      2-tuple; but raise KeyError if D is empty.
    >>> dict1
    {
    'square': None, 'kind': 'human', 'width': None, 'name': 'yzw', 'length': None, 'sex': 'man', 'age': '18'}
    >>> dict1.popitem()
    (
    'square', None)
    >>> dict1.popitem()
    (
    'kind', 'human')
    >>> dict1
    {
    'width': None, 'name': 'yzw', 'length': None, 'sex': 'man', 'age': '18'}
  •  |  setdefault(...)  不存在key,则增加到字典中,若存在,则返回key对应的value
     |      D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D
    >>> dict1.setdefault('kind','human')
    'human'
    >>> dict1
    {
    'kind': 'human', 'sex': 'man', 'age': '18', 'name': 'yzw'}
    >>> dict1.setdefault('sex','femeal')
    'man'
    >>> dict1
    {'kind': 'human', 'sex': 'man', 'age': '18', 'name': 'yzw'}
  • |  update(...)  字典合并,不能存在的k/v则增加,已存在的k则覆盖更新
     |      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]
    >>> dict1
    {
    'kind': 'human', 'sex': 'man', 'age': '18', 'name': 'yzw'}
    >>> dict3
    {
    'length': None, 'width': None, 'square': None}
    >>> dict3['kind']='human'
    >>> dict3
    {
    'kind': 'human', 'length': None, 'width': None, 'square': None}
    >>> dict1.update(dict3)
    >>> dict1
    {
    'square': None, 'kind': 'human', 'width': None, 'name': 'yzw', 'length': None, 'sex': 'man', 'age': '18'}
    >>>
  •  |  values(...)  返回字典所有值的列表形式
     |      D.values() -> an object providing a view on D's values
    >>> dict1.values()
    dict_values([None,
    'human', None, 'yzw', None, 'man', '18'])

 

  • 字典遍历
    >>> dict1
    {
    'width': None, 'name': 'yzw', 'length': None, 'sex': 'man', 'age': '18'}
    >>> for i in dict1:
    ...
    print(i,dict1[i])
    ...
    width None
    name yzw
    length None
    sex man
    age
    18
    # 效率最高>>> for k,v in dict1.items():
    ...
    print(k,v)
    ...
    width None
    name yzw
    length None
    sex man
    age
    18
    #效率低>>> for k,v in enumerate(dict1):
    ...
    print(k,v)
    ...
    0 width
    1 name
    2 length
    3 sex
    4 age
    >>> for idx,k in enumerate(dict1):
    ...     print(idx,k,dict1[k])
    ...
    0 width None
    1 name yzw
    2 length None
    3 sex man
    4 age 18