Common methods and functions of Python dictionary dict

dict={'name':'Joe','age':18,'height':60}

  1. clear
    dict.clear()
    #运行结果{}
  2. pop, remove the key-value pair of the specified key and return vlaue (if there is no such key, return the specified value), popitem, remove the last key-value pair by default
    print(dict.pop('age'))
    print(dict)
    #结果18,{'name': 'Joe', 'height': 60}
    print(dict.pop('agea','erro'))
    print(dict)
    #结果erro,{'name': 'Joe', 'age': 18, 'height': 60}
    print(dict.popitem())
    print(dict)
    #结果('height', 60),{'name': 'Joe', 'age': 18}
  3. del, another way to delete a dictionary
    del dict['age']
    print(dict)
    #结果{'name': 'Joe', 'height': 60}
  4. get, returns the value of the specified key, if the value is not in the dictionary, it returns the default value, which is equivalent todict.__getitem__('name')
    print(dict.get('name'))
    #结果Joe
    print(dict.get('hobby'))
    #结果None
    print(dict.get('hobby','basketball'))
    #结果basketball
  5. setdefault, similar to get(), but if the key does not exist in the dictionary, the key will be added and the value will be set to default
    print(dict.setdefault('hobby'))
    print(dict)
    #结果None,{'name': 'Joe', 'age': 18, 'height': 60, 'hobby': None}
    print(dict.setdefault('hobby','basketball'))
    print(dict)
    #结果basketball,{'name': 'Joe', 'age': 18, 'height': 60, 'hobby': 'basketball'}
  6. update, update the dictionary, if there is a key, update the vlaue corresponding to the key, if not, add
    dict.update({'age':20})
    print(dict)
    #结果{'name': 'Joe', 'age': 20, 'height': 60}
    dict.update({'hobby':'run'})
    print(dict)
    #结果{'name': 'Joe', 'age': 18, 'height': 60, 'hobby': 'run'}
  7. fromkeys, create a new dictionary with seq as the key and vlaue as the initial value of the dictionary
    seq = ('a', 'b', 'c')
    print(dict.fromkeys(seq))
    #结果{'a': None, 'b': None, 'c': None}
    print(dict.fromkeys(seq,'oh'))
    #结果{'a': 'oh', 'b': 'oh', 'c': 'oh'}
  8. Dictionary printing, value, etc.
    print(dict.items())
    print(dict.values())
    print(dict.keys())
    #结果
    dict_items([('name', 'Joe'), ('age', 18), ('height', 60)])
    dict_values(['Joe', 18, 60])
    dict_keys(['name', 'age', 'height'])
  9. Dictionary traversal, traversal key
    for i in dict:
    print(i)
    #结果
    name
    age
    height
    #相同效果的遍历如下:
    for key in dict.keys():
    print(key)
    #
  10. Dictionary traversal, traversal value
    for vlaue in dict.values():
    print(vlaue)
    #结果
    Joe
    18
    60
  11. Dictionary traversal, traversal item
    #10.1输出为元组的方式
    for item in dict.items():
    print(item)
    #结果
    ('name', 'Joe')
    ('age', 18)
    ('height', 60)
    #10.2输出为字符串的方式
    for key,vlaue in dict.items():
    print(key,vlaue)
    #结果
    name Joe
    age 18
    height 60
    #输出为字符串的另一种方式
    for i in dict:
    print(i,dict[i])
  12. Dictionary traversal

Guess you like

Origin blog.51cto.com/xxy12345/2544446