[Python learning tutorial] detailed tutorial on collection operation

There are two container types that Python can traverse.

  • Sequence type: including string, list and Yuanzu
  • Mapping (hash) type: contains dictionary and collection

The sequence type is a linear table. Like an array, it opens up a continuous space in memory for continuous storage. When looking for an element, you need to rent a comparison from the beginning. Therefore, the query efficiency of the sequence is O(n), that is, a sequence with length N generally requires n operations to query whether a variable is in it.

The mapping type is Hash and is based on Hash algorithm. The storage location of variables in the mapping is calculated by Hash. When accessing, the storage location of variables is calculated by Hash. When storing, the storage location is calculated by Hash. When searching, the storage location is calculated by Hash. Therefore, the query efficiency of mapping type is O(1), that is, no matter how large the amount of data is, Queries always require only one operation.

In Hash algorithm, the calculated position of different variables is unique. In order to keep the calculated position of the same variable unchanged every time, it is required that the variable is of immutable type.

In Python, immutable types include: None, number (integer, floating point, Boolean), string, Yuanzu, which can be hashed. The container types such as list, dictionary and collection are not fixed in length, so they can not be hashed, that is, they can not be used as the key of Hash.

Python's dictionary is a very typical Hash type (called HashMap in Java), that is, the address storage value is calculated from the fixed length key. Therefore, the key of the dictionary supports None, numbers (integer, floating point, Boolean), strings, and primitives.
The following are legal dictionaries.

{'name': 'kevin', 'age': 12, 'skills': ['python', 'java']}
{1: 'kevin', 2: 'eric'}
{True: 1, False: 0, None: -1}
{('kevin', 'cn'): {'name': 'keviin', 'age': 12}}

When we only need to store key but not value, we get a collection type, such as

'''
No one answers the problems encountered in learning? Xiaobian created a Python exchange of learning QQ Group: 531509025
 Look for like-minded friends to help each other,There are also good videos and tutorials in the group PDF e-book!
'''
s = {'hello', 'hi', 'how are you'}
print(type(s))

Printed out is a set type.

Note: the empty collection cannot be created with S = {}, so s is a dictionary type, and the empty collection should be created with s = set().

As a hash type, the elements in the set are also required to be of fixed length and can be hashed. It supports None, number (integer, floating point, Boolean), string and Yuanzu. At the same time, the elements in the set cannot be repeated.
In daily use, compared with lists and dictionaries, the appearance rate of collections is not high. However, some operations can be completed efficiently through collections, such as.

1. List de duplication (note that the original order will be changed)

l = [3,2,1,5,6,3,2,6,1,4]
l2 = list(set(l)) 

When converted to a set, it will automatically de duplicate and sort, and get [1, 2, 3, 4, 5, 6] in the return list.

2. Intersection, union and difference sets

This is the set operation we learned in middle school. It is very effective when comparing two groups of data. For example, we have two groups of data.

'''
No one answers the problems encountered in learning? Xiaobian created a Python exchange of learning QQ Group: 531509025
 Look for like-minded friends to help each other,There are also good videos and tutorials in the group PDF e-book!
'''
l1 = ['kevin', 'eric', 'lily', 'niudun', 'sofia', 'lisi']
l2 = ['sofa', 'sofia', 'zhangsan', 'wangwu', 'lisi']

How to quickly find the difference between the two?

# Convert to collection
s1 = set(l1) 
s2 = set(l2)

# Union: all of the two, and de duplication
all = list(s1 | s2)     # Get ['niudun ',' sofa ',' Sofia ',' Zhangsan ',' Wangwu ',' Lisi ',' Lily ',' Eric ',' Kevin ']

# Intersection: the overlapping part of the two
same = list(s1 & s2)   # Get ['lisi ',' Sofia ']

# Difference set: there is a difference set in l1 but not in l2
diff = list(s1 - s2)  # Get ['lily ',' Eric ',' Kevin ',' niudun '], or' s2-s1 'to get l2 without l1

Similarly, all the key values of the dictionary obtained by. keys() of the dictionary are of a collection like type. When each value value of the dictionary does not contain a variable type (i.e. there is no nested list or dictionary), the. items() method of the dictionary also supports collection operation.

d1 = {'name': 'kevin', 'age': 14, 'gender': 'male', 'class': 15, 'grade': 9, 'chinese': 94, 'math': 63, 'english': 82}
d2 = {'english': 82, 'age': 14, 'class': 15, 'chinese':94,'name': 'kevin', 'gender': 1, 'zhengzhi': 79, 'grade': 9}

Suppose we want to compare the differences between the two sets of data

'''
No one answers the problems encountered in learning? Xiaobian created one Python exchange of learning QQ Group: 531509025
 Look for like-minded friends to help each other,There are also good videos and tutorials in the group PDF e-book!
'''
# There are key s in d1 but not in d2
d1.keys() - d2.keys()   # Get {'math'}

# Keys in d2 but not in d1
d2.keys() - d1.keys()  # Get {'zhengzhi'}

# Different items in d1 and d2
```python
d1.items() - d2.items()   # {('math', 63), ('gender', 'male')}
d2.items() - d1.items()   # {('zhengzhi', 79), ('gender', 1)}

When using. items() for collection operation, the dictionary is required to have no nested list, dictionary and other variable types

Tags: Python

Posted on Sat, 20 Nov 2021 04:05:58 -0500 by balacay