Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Hello everyone, I am a bowl week, a front end that does not want to be drunk (inrolled). If I am lucky enough to write an article that you like, I am very lucky

A collection of

A Python collection is basically the same as a mathematical collection. It is unordered, meaning that it cannot be accessed using an index, and there are no duplicate elements in the collection.

Create a collection

To create a collection in Python, use curly braces {} literals, or use set() to create a collection. {} must contain at least one element. You cannot use {} to create an empty dictionary. Instead, use set() to create a collection.

Set1 = {"Hello", "World"} print(type(set1), set1) # <class 'set'> {'World', 'Hello'} # print(set1[1]) # TypeError: Set2 = set("hello") # {'l', 'o', 'h', 'e'} sets will remove duplicate elements. Set3 = {} set4 = set() print(type(set3), Type (set4) # <class 'dict'> dictionary <class 'set'> list1 = [1, 2, 3, 4, 3, 5, 1, 3] # converts list collection set6 = set (list1) print (type (set6), set6) # < class 'set' > {1, 2, 3, 4, Set5 = {num for num in range(20) if num % 3 == 0} print(set5) # {0, 3, 6, 9, 12, 15, For ch in set5: print(ch)Copy the code

Note: The elements in a collection are immutable types, such as integers, floats, strings, tuples, etc. That is to say, mutable types cannot be elements of a tuple. Collections themselves are mutable types, so collections cannot be elements of a collection.

Operation of set

Set data types have a plethora of operators, including member operations, intersection operations, union operations, difference operations, comparison operations (equality, subset, superset), etc.

Members of the operation

You can check whether an element is in a collection by using the member operations in and not in, example ↓

Set1 = {" hello ", "Python", "this is "," collection ", } print("Python" in set1) # True print(" hello "in set1) # False print("set" not in set1) # False print("list" not in set1) # TrueCopy the code

Intersection and difference operation

Collections in Python, like collections in mathematics, can operate on intersection, union, difference, and so on, and can be operated on both by operators and method calls.

Example code is as follows:

Print (set1&set2) print(set1&set2) print(set1&set2) print(set1&set2) print(set1&set2) print(set1&set2) print(set1&set2) print(set1&set2) print(set1&set2) print(set1&set2) Intersection: print(set1. Intersection (set2)) # {2, 4, 6} # Using the | operator print (characters | set2) # {1, 2, 3, 4, 5, 6, 7, 8, 10} method # 2: Print (set1. Union (set2)) # {1, 2, 3, 4, 5, 6, 7, 8, 10} Print (set1-set2) # {1, 3, 5, 7} Print (set1.difference(set2)) print(set1.difference(set2)) {1, 3, 5, 7} Print (set1 ^ set2) # {1, 3, 5, 7, 8, 10} Print (set1. Symmetric_difference (set2)) # {1, 3, 5, 7, 8, 10} Symmetric difference of two sets of minus the intersection and set the print ((characters | set2) - (characters & set2)) # {1, 3, 5, 7, 8, 10}Copy the code

The intersection, union, and difference operations of sets can also be combined with assignment operations to form compound operations, as shown in the following example:

set1 = {1, 3, 5, 7} set2 = {2, 4, # 6} characters and set2 and set it characters assignment # can also through the characters, the update (set2) to implement the characters | = # set2 characters = characters | set2 print (characters) # {1, 2, 3, 4, 5, 6, 7} set3 = {3, 6, &= set3 # set1 = set1 # set1 print(set1) # {3, 6}Copy the code

Comparison operations

Two sets can be used with == and! If the elements in two sets are identical, then the result of the comparison is True, otherwise it is False; ! = and vice versa.

If all members of set A are also members of set B, then A is A subset of B, and B is A superset of A; If A child of A is not equal to B, then A is said to be A true child of B. The operators for determining subsets and supersets are < and >. Example code ↓

Set1 = {1, 3, 5} set2 = {1, 2, 3, 4, 5} set3 = set2 # < operator represents a true subset, Print (set1 < set2, set1 <= set2) # True print(set2 < set3, Print (set1. Issubset (set2)) # True print(set2. Isset2)) # True # Print (set2.issuperset(set1)) # True print(set2 > set1) # TrueCopy the code

Set method

A collection is a mutable column type. You can modify the elements of the collection using the collection type method. Example code ↓

Add (1) set1.add(2) print(set1) # {1, Update ({1, 2, 3, 4, 5}) print(set1) # {1, 2, 3, 4, 5} Discard (4) set1.discard(2) set1.discard(22) Remove (6) # KeyError: 6, remove(6) # KeyError: 6, remove(6) # KeyError: 6, remove() Print (set1.pop()) print(set1.pop()) # 1 Print (type(set1)) # <class 'set'> print(set1) # set() Set2 = {"Hello", "World"} set3 = {"Hello", "week "} set4 = {"Hi", "Python"} print(set2.isdisjoint(set3)) # False print(set2.isdisjoint(set4)) # TrueCopy the code

Immutable set

There is also a set of immutable types in Python called Frozenset, which is as different from a set as a list is from a tuple. A Frozenset is basically the same as a set except that you can’t add or remove elements. Example code ↓

set1 = frozenset({1, 3, 5, 7})
set2 = frozenset(range(1, 6))
print(set1 & set2)    # frozenset({1, 3, 5})
print(set1 | set2)    # frozenset({1, 2, 3, 4, 5, 7})
print(set1 - set2)    # frozenset({7})
print(set1 < set2)    # False
Copy the code