Python 3-basic-09-dictionary

Dictionary features:

1. Disordered

2.key unique

ps: similar to map in java

1, Dictionary creation

Each key = > value pair of the dictionary is separated by colon, and each key value pair is separated by comma. The whole dictionary is included in curly brackets {}, with the format as follows:

1. _ dict={key:value} (common)

2. _dict=dict(((key,value),))

_dict1 ={"name":"lvyq","age":12}
_dict2=dict((("name","lvyq"),("age",12),))
print(type(_dict1),_dict1)
print(type(_dict2),_dict2)

2, Dictionary operation

1. Query by key

1.value=_dict['key']

2.value = _dict.get('key')

_dict = {'name':'lvyq','age':12}
name = _dict['name']
age = _dict.get('age')
print(name)
print(age)

Operation results

lvyq

12

Other methods

methodexplain
_dict.keys()Get all keys. In Python 3, it is not a list type, but a dict_keys type, which is optimized by Python 3. If you use it, you need to use type conversion
_dict.values()

Get all values

_dict.items()Get all key value pairs
_dict = {'name':'lvyq','age':12}
#Query all keys() dict_keys is not a list type, but a dict_keys type, which needs list conversion. Optimization of Python 3
print("Value obtained:{},Corresponding type:{},After converting to list:".format(_dict.keys(),type(_dict.keys())),list(_dict.keys()))
print("Value obtained:{},Corresponding type:{},After conversion to tuples:".format(_dict.values(),type(_dict.values())),tuple(_dict.values()))
print("Value obtained:{},Corresponding type:{},After converting to dictionary:".format(_dict.items(),type(_dict.items())),dict(_dict.items()))

Operation results

2. Modification

2. Add

1. _dict('key ') =' value '

When the key exists in the dictionary, it will be updated. When it does not exist, the corresponding key value pair will be added to the dictionary

_dict = {'name':'lvyq','age':12}
print("Original value:%s"%_dict)
_dict['age']=22
print("After modification:%s"%_dict)
_dict['job']='IT'
print("After modification:%s"%_dict)

Operation results

Original value: {name ':'lvyq','age ': 12}
After modification: {name ':'lvyq','age ': 22}
After modification: {name ':'lvyq', 'age': 22, 'job':'It '}

2

setdefault('key ',' value ')

The difference is that when the key exists in the dictionary, it will not be updated, and the value corresponding to the key to be added will be returned. If it does not exist, the value corresponding to the added key value pair will be added and returned

_dict = {'name':'lvyq','age':12}
print("Before adding:%s"%_dict)
_set_vauel=_dict.setdefault('age',62)
print("After modification:{},The modified value is:{}".format(_dict,_set_vauel))
_set_vauel=_dict.setdefault('job','IT')
print("After modification:{},The modified value is:{}".format(_dict,_set_vauel))

Operation results

Before adding: {name ':'lvyq','age ': 12}
After modification: {name ':'lvyq','age ': 12}, the modified value is: 12
After modification: {name ':'lvyq', 'age': 12, 'job':'IT '}, the modified value is: IT

3. Modification

1. Use_ dict['key '], the value can be modified. (mentioned above)

2.update()_ The value of dict2 is updated to_ In dict, when the same key exists in two dictionaries, the value corresponding to the key in the later dictionary is updated to the value corresponding to the key in the previous dictionary

_dict.update(_dict2)

_dict = {'name':'lvyq','age':12}
print("_dict Value of:%s"%_dict)
_dict2={'name':'Xiao Ming','job':'IT'}
print("_dict2 Value of:%s"%_dict2)
_dict.update(_dict2)
print("After update_dict Value of:%s"%_dict)

Operation results

_ Value of dict: {name ':'lvyq','age ': 12}
_ Values of dict2: {name ':' Xiaoming ',' job ':' it '}
After update_ Values of dict: {name ':' Xiaoming ',' age ': 12,' job ':' it '}

4. Delete

1.del _dict['key '] delete key value pairs through key

2._dict.pop('key ') is deleted by pressing the key and the corresponding value is returned

3._dict.clear() clear

4._dict.popitem() randomly deletes a group of key value pairs (basically not used)

_dict = {'name':'lvyq','age':12,'Job':'IT'}
#Delete key value pairs according to key
del _dict['name']
print("del Results after deletion:%s"%_dict)
#Delete and return the value of the key value pair
age =_dict.pop('age')
print("delete age after:{},delete key Corresponding value{}".format(_dict,age))
#empty
_dict.clear()
print(_dict)

Operation results

del delete result: {'age': 12, 'job': it '}
After deleting age: {'job':'It '}, delete the value 12 corresponding to the key
{}

Others

In addition to the two mentioned above, the dictionary can also be created by using fromkeys()

dict.fromkeys(seq,'value')

Create a new dictionary, use the element in seq sequence as the key of the dictionary, and value is the initial value corresponding to all keys of the dictionary

_dict=dict.fromkeys(['one','two','three'],'value')
_dict2=dict.fromkeys('ABC','value')
_dict3=dict.fromkeys(['one','two','three'],[1,2])
print(_dict)
print(_dict2)
print(_dict3)

Operation settlement

{'one': 'value', 'two': 'value', 'three': 'value'}
{'A': 'value', 'B': 'value', 'C': 'value'}
{'one': [1, 2], 'two': [1, 2], 'three': [1, 2]}

2, Dictionary traversal

_dict = {'name':'lvyq','age':12}
for i in _dict:
    print("Traversal 1",i,dict[i])
for i,v in _dict.items():
    print("Traversal 2",i,v)

The first one is recommended. The first one is relatively efficient, and the second one involves the process of data type conversion

Keywords: Python Back-end

Added by litarena on Sat, 12 Feb 2022 05:21:09 +0200