14. Collection in python, 14. python collection
What is a set? As it literally means, a bunch of things are merged together. At first glance, it seems that there is no difference with the container. Well, the collection is also a container.
Before learning about the differences between these containers, let's take a look at how the collection is created:
A = set () # unchangeable set B = frozenset () # variable set print aprint B
There are two types of sets: immutable and variable. The differences between the two are analyzed later.
However, what does it mean to create two empty sets.
To make it meaningful, Let's first look at the most important functions of the Set:Deduplication.
a = ('aaa',123,123,123)b = set(a)print b
We can see that the duplicate element 123 in ancestor a has only one in the set. Therefore, we summarize the concept of a set:
A set is to put different elements together to form a set object.. (Does not affect the original object)
At this time, the students with intelligence can see a square bracket in the Set, and immediately think of the List, and try to use the index to set the value:
a = ('aaa',123,123,123)b = set()print b[0]
But not.
BecauseThe set is unordered.Is the same as the dictionary.
So can we regard the set as a special dictionary, but there is no key, so we cannot take the value through the key..
In a sense, it is acceptable. However, a set can be divided into variable sets and immutable sets. immutable sets cannot modify elements like dictionaries.
In addition:
a = 'abcdaabbccdd'b = set(a)print b
Because strings are also sequences, they can also be used to remove duplicates, but they are generally useless.
So what if I deduplicate the dictionary? We learned in the dictionary that the dictionary will automatically process duplicate key names. Can the value be de-duplicated?
a = {'a':123,'b':123}b = set(a)print b
It seems that deduplication of keys is useless. Unless you want to combine the keys into a set, deduplication is of little significance. Depends on the business needs.
Here, my summary of deduplication is:Remove duplicate referencesI believe that people who have read the instructions in the python sequence can understand what this means.
After knowing the essence of deduplication, we have this requirement. I have two containers. I want to get the result of deduplication for these two containers.
Then I write:
a = ('scolia',123,456,666)b = ('scolia',666)c = set(a,b)print c
The result is a big dashboard, that is, if there is only one parameter, two results are given.
Okay. First, let's see how the parameter is accepted internally:
First, the _ init _ here is the constructor, which will be described during class creation. Now, as long as we know that our parameters are passed to seq = (), we can use this method to transmit a ancestor.
Then you can write it like this:
a = ('scolia',123,456,666)b = ('scolia',666)c = set((a,b))print c
The results are not what we think.
It seems that the deduplication of a set is for an object, so we can summarize the set:
A set removes repeated references to elements in an object (a sequence or a class sequence dictionary.
To achieve this, we must first splice two objects:
a = ('scolia',123,456,666)b = ('scolia',666)c = set(a+b)print c
There are more than one splicing sequence. For more information, see my previous blog.
Different types of sequences must be forcibly converted into one type; otherwise, an error is returned.
a = ('scolia',123,456,666)b = ['scolia',666]c = set(a+tuple(b))print c
Next, let's start learning its built-in methods.
The old method is to look at the help documentation first.
Because the set is variable and immutable, it is described separately:
Help on class set in module _ builtin __: class set (object) | set ()-> new empty set object | set (iterable) -> new set object | Build an unordered collection of unique elements. | Methods defined here: | _ and __(...) | x. _ and _ (y) <=> x & y | _ cmp __(...) | x. _ cmp _ (y) <=> cmp (x, y) | _ contains __(...) | x. _ contains _ (y) <=> y in x. | _ eq __(...) | x. _ eq _ (y) <=> x = y | _ ge __(...) | X. _ ge _ (y) <==> x> = y | _ getattribute __(...) | x. _ getattribute _ ('name') <=> x. name | _ gt __(...) | x. _ gt _ (y) <==> x> y | _ iand __(...) | x. _ iand _ (y) <=> x & = y | _ init __(...) | x. _ init __(...) initializes x; see help (type (x) for signature | _ ior __(...) | x. _ ior _ (y) <=> x | = y | _ isub __(...) | x. _ isub _ (y) <=> x-= y | _ iter __(...) | x. _ iter _ () <=> iter (x) | _ ixor __(...) | X. _ ixor _ (y) <=> x ^ = y | _ le __(...) | x. _ le _ (y) <=> x <= y | _ len __(...) | x. _ len _ () <=> len (x) | _ lt __(...) | x. _ lt _ (y) <==> x <y | _ ne __(...) | x. _ ne _ (y) <=> x! = Y | _ or __(...) | x. _ or _ (y) <=> x | y | _ rand __(...) | x. _ rand _ (y) <==> y & x | _ reduce __(...) | Return state information for pickling. | _ repr __(...) | x. _ repr _ () <=> repr (x) | _ ror __(...) | x. _ ror _ (y) <=> y | x | _ rsub __(...) | x. _ rsub _ (y) <=> y-x | _ rxor __(...) | x. _ rxor _ (y) <=> y ^ x | _ sizeof __(...) | S. _ sizeof _ ()-> size of S in memory, in bytes | _ sub __(...) | x. _ sub _ (y) <=> x-y | _ xor __(...) | x. _ xor _ (y) <=> x ^ y | add (...) | Add an element to a set. | This has no effect if the element is already present. | clear (...) | Remove all elements from this set. | copy (...) | Return a shallow copy of a set. | difference (...) | Return the difference of two or more sets as a new set. | (I. e. all elements that are in this set but not the others .) | difference_update (...) | Remove all elements of another set from this set. | discard (...) | Remove an element from a set if it is a member. | If the element is not a member, do nothing. | intersection (...) | Return the intersection of two or more sets as a new set. | (I. e. elements that are common to all of the sets .) | intersection_update (...) | Update a set with the intersection of itself and another. | isdisjoint (...) | Return True if two sets have a null intersection. | issubset (...) | Report whether another set contains this set. | issuperset (...) | Report whether this set contains another set. | pop (...) | Remove and return an arbitrary set element. | Raises KeyError if the set is empty. | remove (...) | Remove an element from a set; it must be a member. | If the element is not a member, raise a KeyError. | effecric_difference (...) | Return the specified Ric difference of two sets as a new set. | (I. e. all elements that are in exactly one of the sets .) | effecric_difference_update (...) | Update a set with the specified Ric difference of itself and another. | union (...) | Return the union of sets as a new set. | (I. e. all elements that are in either set .) | update (...) | Update a set with the union of itself and others. | ---------------------------------------------------------------------- | Data and other attributes defined here: | _ hash _ = None | _ new _ = <built-in method _ new _ of type object> | T. _ new _ (S ,...) -> a new object with type S, a subtype of TSet
It can be divided into the following categories:
1. Operator-related
3. built-in Function Correlation (to be summarized later)
4. General built-in Methods
1. Operator-related
I don't know if you still remember the set in mathematics, that is, the intersection, the set, or something. If you forget it, please repair it yourself.
The following is a summary in a table:
| Mathematical symbols |
Python symbol |
Description |
| Bytes |
& |
Intersection, such as a & B |
| Bytes |
| |
Union, such as a | B |
| -Or \ |
- |
Difference or relative complement |
| △ |
^ |
Symmetric Difference |
| Bytes |
< |
Real subset |
| Bytes |
<= |
Subset |
| Bytes |
> |
Real superset |
| Bytes |
> = |
Superset |
| = |
= |
Equal to, two sets are equal |
| = |
! = |
Not equal |
| ε |
In |
Yes. It is an element in it. |
| Bytes |
Not in |
Does not belong |
This part of content can only be called by the math. I won't talk about it anymore.
2. Common built-in Methods
Although a bunch of methods are listed in the help document, but the set is divided into variable and immutable, so some methods, such as modification operations can only be used in a variable set, therefore, there are two built-in methods:
1. All sets can be used:
| Method |
Description |
| A. issubset (B) |
If a is a subset of B, True is returned; otherwise, False is returned. |
| A. issuperset (B) |
If a is a superset of B, True is returned; otherwise, False is returned. |
| A. intersection (B) |
Returns the intersection of a and B. |
| A. union (B) |
Returns the Union of a and B. |
| A. difference (B) |
Returns a new set, which is after the elements a and B of the set are removed. |
| A. effecric_difference (B) |
Returns a new set, which is a member of a or B, but not a member of B. |
| A. copy () |
Returns a shortest copy. |
2. The set can be modified:
| Method |
Description |
| A. add () |
Add a new object to the set |
| A. clear () |
Clear all objects in the Set |
| A. pop () |
Delete any element in a and return it. If it is null, an error is returned. |
| A. remove (obj) |
Delete the obj element in a. If no error is found, an error is returned. |
| A. discard (obj) |
Delete the obj element in "a". When no object is found, nothing is done. "None" is returned. |
| A. update (B) |
Modify a with the element in B. In this case, a contains the members of a or B. Equivalent to merge |
| A. intersection_update (B) |
Update a set with the intersection of a and B |
| A. difference_update (B) |
Remove elements like B from |
| A. effecric_difference (B) |
Change a to the symmetric difference between a and B. |
Here, the basic mathematical concepts such as intersection and population and subset supersets are no longer repeated. Here we will talk about difference population and symmetric difference:
1. differential compensation
a =set( 'scolia')b = set('good')print a - bprint a.difference(b)
It is actually killing 'O' IN a, and killing 'O' is because there are 'O' in both sets '.
I wanted to explain it in a mathematical formula, but my mathematics has been forgotten in the Pacific Ocean.
2. Symmetric Difference
a =set( 'scolia')b = set('good')print a ^ bprint a.symmetric_difference(b)
The intersection of the two types is eliminated, and the rest is taken out to form a new set.
Now let's talk about the collection. I don't have to remember all these things. I just need to check them again when I use it.
Reference: click here