Chinese history [Yandi Shennong tasted hundreds of herbs]:
In ancient China, the leader of the tribe surnamed Jiang was honored as Shennong, also known as Lieshan. It is said that "Shennong tastes all kinds of grass" and teaches people medical treatment and farming. The Chinese regard it as the legendary inventor and patron saint of agriculture and medicine. At the junction of Sichuan, Hubei and Shaanxi in China, it is said that Shennongjia mountain is the place where Shennong tastes all kinds of grass.
-Source: full history APP
Today, we talk about the dictionary type of python. Dictionary and list are the two most commonly used data types. If necessary, you can also go directly to my github to view all notes:
https://github.com/JackKoLing/python_notes_with_ten_days
As the saying goes, "a good memory is better than a bad pen". If you write more and remember more, you will never be wrong. More insistence for no reason and less utilitarian pursuit.
For environment configuration, please refer to the following two articles:
- win10 version @ Xiao Bao, official account: CV's way
[configuration of deep learning environment] configure the deep learning environment in hands-on combat (win10 version) - Linux version @ Xiao Bao, official account: CV's way
[configuration of deep learning environment] configure the deep learning environment with hands-on practice (Ubuntu version 20.04).
1 dictionary type: dict
- Expression symbol: {}
- Dictionaries are also called associative arrays or hash tables in other programming languages
2 features
- Element access through key
- unordered set
- Variable type container, so its length is variable, and supports heterogeneity and nesting
3 form
- {key1:value1, key2:value2, ...}
- {}: empty dictionary
d1 = {'x':32, 'y':[1, 2, 3, 4]} print(d1['y']) print(d1['y'][1:]) >>> [1, 2, 3, 4] [2, 3, 4]
Note: > > > means output, the same below.
len(d1) >>> 2 d1['x'] = 504 print(d1) >>> {'x': 504, 'y': [1, 2, 3, 4]}
4 built in functions
d1.clear() # Empty dictionary elements print(d1, len(d1)) >>> {} 0 d1 = {'x': 504, 'y': [1, 2, 3, 4]} d2 = d1.copy() print(d2) >>> {'x': 504, 'y': [1, 2, 3, 4]} d3 = d1 print(d3) print(id(d1), id(d2), id(d3)) # It can be seen that using the copy function is to create a new dictionary object, while direct copy is to point to the same object >>> {'x': 504, 'y': [1, 2, 3, 4]} 2673290029528 2673290030608 2673290029528
help(dict.fromkeys) >>> Help on built-in function fromkeys: fromkeys(iterable, value=None, /) method of builtins.type instance Create a new dictionary with keys from iterable and values set to value. d4 = dict.fromkeys((1, 2, 3), 'go') # Create a new dictionary, where parameter 1 is key and parameter 2 is value. The default is None print(d4) >>> {1: 'go', 2: 'go', 3: 'go'}
print(d1.get('x')) # Get the value corresponding to the parameter print(d1.get('k')) # If there is no such key, no exception will be thrown. But if you use d1['k '], there will be exceptions >>> 504 None
print(help(dict.items)) print('\n') print(d1.items()) # Returns a list where the element is a binary group. That is, the dictionary becomes a tuple list t1, t2 = d1.items() # This is also called variable unpacking print(t1) print(t2) >>> Help on method_descriptor: items(...) D.items() -> a set-like object providing a view on D's items None dict_items([('x', 504), ('y', [1, 2, 3, 4])]) ('x', 504) ('y', [1, 2, 3, 4])
m1, m2 = {'x':1, 'y':2} # Note that this dictionary unpacks and exports key, not value print(m1) print(m2) >>> x y print(d1.items()) # Returns a list of key value tuples print(d1.keys()) # Return to key list print(d1.values()) # Return value list >>> dict_items([('x', 504), ('y', [1, 2, 3, 4])]) dict_keys(['x', 'y']) dict_values([504, [1, 2, 3, 4]]) print(d1.pop('x')) # The pop-up key is the value of the parameter print(d1) >>> 504 {'y': [1, 2, 3, 4]}
d2 = {'x':1, 'y':2, 'z':3} print(d2.popitem()) # Pop up the last (perhaps random) key value tuple print(d2.popitem()) print(d2) >>> ('z', 3) ('y', 2) {'x': 1} help(dict.popitem) >>> Help on method_descriptor: popitem(...) D.popitem() -> (k, v), remove and return some (key, value) pair as a 2-tuple; but raise KeyError if D is empty.
d1 = {'x':1, 'y':2} d2 = {'m':21, 'n':76, 'y':44} d1.update(d2) # Merge dictionaries. If there is the same key, the dictionary in the parameter shall prevail and the old one shall be overwritten print(d1) >>> {'x': 1, 'y': 44, 'm': 21, 'n': 76} d3 = dict(name='Jackko', age=24, gender='m') # Define the dictionary. Note that key s are string type or number type print(d3) >>> {'name': 'Jackko', 'age': 24, 'gender': 'm'} print(d3.setdefault('name', None)) # The function is similar to get() to get the value of parameter 1 print(d3.setdefault('weight', 120)) # If not, add the parameter key value to the dictionary and return the parameter value print(d3) >>> Jackko 120 {'name': 'Jackko', 'age': 24, 'gender': 'm', 'weight': 120} zip('xyz', '123') # The sequence is one-to-one corresponding to form tuples, and the object is returned to save memory. You need to use list() to view print(list((zip('xyz', '123')))) print(list((zip('xyzm', '123')))) # Only one-to-one correspondence, no more print(list((zip('xyz', '1234')))) >>> [('x', '1'), ('y', '2'), ('z', '3')] [('x', '1'), ('y', '2'), ('z', '3')] [('x', '1'), ('y', '2'), ('z', '3')] d4 = dict(zip('xyz', '123')) # Build dictionary print(d4) >>> {'x': '1', 'y': '2', 'z': '3'}
[statement]: learning notes are based on personal arrangement of various learning resources on the Internet.
The above is the content of this issue. The next issue introduces the collection types of python.
My name is Xiao Bao, a computer vision enthusiast, learner and follower. Welcome to study with me.