Detail the set collection in Python

  • 2021-11-29 08:05:42
  • OfStack

set Collection 1 in the directory Python. What is the collection? 2. How does the set set work? 1. Create set set 2, delete set set 3, access set set element 4, delete element 5 in set, add element 3 to set. Intersection and complement 1, intersection 2, union 3 and difference 4 of set set. Other methods in set 5. frozenset set

set Set in Python

1. What is a collection?

What is a collection? I believe readers and friends have never used the data type of collection. I have heard the noun set in math class. Set in mathematics is a basic concept. To put it bluntly, a pile of non-repeated numbers can form a set. Back to the data type of the collection in Python, it can be used to hold multiple non-duplicate data, and the data type of these data is immutable. These immutable types include numbers, strings and tuples. That is to say, mutable data types such as lists and dictionaries cannot be used as elements of collections.
There are two sets in Python, one is a set of set type, and the other is a set of fronzenset type. The difference between these two types of collections is that the set collection is mutable, that is, elements within the collection can be added and deleted, while the fronzenset collection is immutable. That is, you cannot add or delete elements inside the collection.
The set set is defined as {element1,element2,...,elementn }, where element1 ~ elementn denotes an unlimited number of elements in the set. Each element is uniquely 1.

2. How does the set set work?

1. Create an set collection

There are two ways to create an set collection:
Type 1: Through {} Method, and its syntax format is :{element1,element2,...,elementn}
Type 2: Through set Function, whose syntax format is set ( iterable ) Among them iterable If not, an empty set object is created. The iterable passed in must be a sequence that can be traversed, such as strings, lists, and so on. For example:


set_demo = {'1', 2, (' Code farmer ', ' Brother Fei ')}
print(set_demo)
list = [1, 'test', 1, (' Code farmer ', ' Brother Fei ')]
print(' List list Results =', list)
set_demo1 = set(list)
print(' Generated set Results =', set_demo1)

The result of the operation is:


{2, (' Code farmer ', ' Brother Fei '), '1'}
 List list Results = [1, 'test', 1, (' Code farmer ', ' Brother Fei ')]
 Generated set Results = {1, (' Code farmer ', ' Brother Fei '), 'test'}

You can see that even if there are two 1 elements in the list list, there is still only one 1 element after the set collection is generated, which means that elements with the same value in the collection will only be stored once.

2. Delete the set collection

Deleting an set collection can be done by using the del (setname) function, where setname is the name of the set collection to be deleted.

3. Access set collection elements

Because the elements in the collection are unordered, 5 Xiang uses subscripts to access elements like lists. In Python, the most common way to access collection elements is to use loops to read out the elements in the collection one by one.


set_demo = {'1', 2, (' Code farmer ', ' Brother Fei ')}
for value in set_demo:
    print(value)

The result of the operation is:


2

(' Code farmer ', ' Brother Fei ')

1

The output of the element is also unordered.

4. Delete Elements from Collection

There are two ways to delete elements in a collection:
The first type: through remove () method, its syntax structure is: setname. remove (element), where setname is the set of set to be operated and element is the element to be deleted. If the deletion fails, KeyError error will be reported.
Type 2: Through the discard () method, which uses the same method as remove () 1, except that no error will be reported if the deletion of the element fails.


set_demo = {'1', 2, (' Code farmer ', ' Brother Fei ')}
set_demo.remove('1')
print(' Delete Element 1 Subsequent collections =', set_demo)
set_demo.discard('5')
print(' Delete Element 5 Subsequent collections =', set_demo)

The result of the operation is:


 Delete Element 1 Subsequent collections = {2, (' Code farmer ', ' Brother Fei ')}
 Delete Element 5 Subsequent collections = {2, (' Code farmer ', ' Brother Fei ')}

5. Add elements to the collection

Through add function, you can add elements to the collection. Its syntax structure is: setname. add (element), where setname is the set of set to be operated and element is the element to be added. Only immutable elements, such as numbers, tuples or strings, can be added, and variable data such as lists, dictionaries and set sets cannot be added.
For example:


set_demo.add('6')
print(' Add Element 6 The result after that is =', set_demo)

The result of the operation is:


 Add Element 6 The result after that is = {2, '6', (' Code farmer ', ' Brother Fei ')}

3. Intersection and complement of set set

In mathematics, a set has an intersection union set and a complement set. Sets in Python also have these concepts.

1. Intersection

Pass & Connecting two set collections generates a new collection, which takes the common elements of the two original collections.

2. Union

Pass | Connecting two set sets generates a new set, which takes all the elements of the two original sets.

3. Difference set

Pass - Connecting two set sets generates a new set that takes elements that are present in one set but not in the other.

Take chestnuts:


set_demo = {1, 3, 5, 6}
set_demo1 = {2, 4, 6}
print(" Take the result of intersection =", set_demo & set_demo1)
print(" Take the result of union =", set_demo | set_demo1)
print(" The result of taking the difference set =", set_demo - set_demo1)

The result of the operation is


 Take the result of intersection = {6}
 Take the result of union = {1, 2, 3, 4, 5, 6}
 The result of taking the difference set = {1, 3, 5}

4. Other methods in set

dir (set) allows you to view all the methods in the set collection. 1 There are the following methods:


{2, (' Code farmer ', ' Brother Fei '), '1'}
 List list Results = [1, 'test', 1, (' Code farmer ', ' Brother Fei ')]
 Generated set Results = {1, (' Code farmer ', ' Brother Fei '), 'test'}

0

The naming of the method is more standard, and the function of the method can be guessed according to the English name, so the meaning of each method will not be repeated here. Give a direct example:


{2, (' Code farmer ', ' Brother Fei '), '1'}
 List list Results = [1, 'test', 1, (' Code farmer ', ' Brother Fei ')]
 Generated set Results = {1, (' Code farmer ', ' Brother Fei '), 'test'}

1

The result of the operation is:


{2, (' Code farmer ', ' Brother Fei '), '1'}
 List list Results = [1, 'test', 1, (' Code farmer ', ' Brother Fei ')]
 Generated set Results = {1, (' Code farmer ', ' Brother Fei '), 'test'}

2

5. frozenset Collection

frozenset collection is an immutable version of set collection. Compared with set collection, the elements in frozenset collection are immutable, that is, there is no method to add and delete elements.
The way to create an frozenset collection is through the frozenset () method. The syntax format is: frozenset (iterable). If iterable is not passed, an empty set object is created. The iterable passed in must be a sequence that can be traversed, such as strings, lists, and so on.


frozenset_demo = frozenset(list)
print("frozenset_demo The type of is =", type(frozenset_demo))
print("frozenset_demo The result of =", frozenset_demo)

The result of the operation is:


{2, (' Code farmer ', ' Brother Fei '), '1'}
 List list Results = [1, 'test', 1, (' Code farmer ', ' Brother Fei ')]
 Generated set Results = {1, (' Code farmer ', ' Brother Fei '), 'test'}

4

Related articles: