Next week ovo 2022-05-14 15:10:08 阅读数:471

pythonbasicslearnusecollections

Catalog

Wedge ：

One 、 Introduction to the collection

Two 、 The basic use of sets

​​​​​​​​​​​1、 Set creation

​​​​​​​​​​​​​​2、 An increase in assembly （ Disorderly increase ）

​​​​​​​update()： Add elements to the set iteration

3、 The deletion of a set

​​​​​​​remove(item)： Delete the specified element in the collection （item----- Elements to delete ）

​​​​​​​discard() Method  : Delete the specified element in the collection

​​​​​​​pop()： Randomly delete an element

​​​​​​​clear()： Empty the set

​​​​​​​del     ： Delete the collection

​​​​​​​​​​​​​​4、 Other operations of the collection

​​​​​​​4.1、 intersection （&  perhaps intersection）

​​​​​​​4.2 、 Combine （| perhaps union）

​​​​​​​4.3 、 Difference set （- perhaps difference）

​​​​​​​4.4、 Anti intersection  （^ perhaps symmetric_difference）

​​​​​​​4.5、 Subsets and supersets

​​​​​​​4.6、frozenset Immutable set , Make the set immutable .

​​​​​​​4.7、isdisjoint()

​​​​​​​4.8、​​​​​​​symmetric_difference_update()

Conclusion ：

# Wedge ：

This blog mainly introduces to your friends Python in < The most basic operation method of collection >, Take you to master Python Basics , I hope you can gain more knowledge here ！ Let's learn together ！ Progress together

The author hopes ： Due to the limitation of blogger level , Blog will inevitably have mistakes and inaccuracies , I also very much hope that you can point out in the message area . Let's encourage .

Finally, if you think the blogger's article is good , Please support the third company

# One 、 Introduction to the collection

Sets are unordered , No duplicate data sets , The elements in it are hashable ( Immutable type ), But the set itself is not hashable （ So the set can't do the key of the dictionary ） Of . Here are the two most important points of the collection ：

• duplicate removal , Turn a list into a collection , It will automatically go to heavy .

subject ：

Will list [1,2,2,3,4,5,5,66] Data De duplication in

In the face of this problem, if there is no problem-solving method , It's still a difficult method without algorithm , So choose the simple method of converting a list into a collection , The following is how to solve the problem :

# duplicate removal
>>>li = [1,2,2,3,4,5,5,66]
>>>print(' The list before de duplication is ',li)
# Convert list to set
>>>set1 = set(li)
# Re convert the de duplicated set into a list
>>>li = list(set1)
>>>print(' The list after weight removal is ',li)
The list before de duplication is [1, 2, 2, 3, 4, 5, 5, 66]
The list after weight removal is [1, 2, 3, 4, 5, 66]
• Relationship testing , Test the intersection of two sets of data 、 Difference set 、 Union, etc .

# ​​​​​​​ Two 、 The basic use of sets

## ​​​​​​​​​​​1、 Set creation

You can use braces  { }  perhaps  set()  Function to create a collection , Be careful ： To create an empty collection, you must use the  set()  instead of  { }, because  { }  Is used to create an empty dictionary .

Create format ：

# Create set set1
>>>set1 = set({1,2,'a'})
# Create set set2
>>>set2 = {1,2,'a'}
>>>print(set1,set2)
{1, 2, 'a'} {1, 2, 'a'}

## ​​​​​​​​​​​​​​2、 An increase in assembly （ Disorderly increase ）

>>>set = {'a','b','c'}
>>>print(set)
{'a','b','d','c'}
• ### ​​​​​​​update()： Add elements to the set iteration

>>>set = {'hjjk','jack','lose'}
>>>set.update('abc')
>>>print(set)
{'a', 'jack', 'b', 'lose', 'hjjk', 'c'}

## 3、 The deletion of a set

• ### ​​​​​​​remove(item)： Delete the specified element in the collection （item----- Elements to delete ）

>>>set= {'a','b','c','d'}
>>>set.remove('a')
>>>print(set)
{'c', 'b', 'a'}
• ### ​​​​​​​discard() Method  : Delete the specified element in the collection

discard() Method is used to remove the specified collection element .

This method is different from remove() Method , because remove() Method to remove a nonexistent element will cause an error , and discard() No way .

>>>set= {'a','b','c','d'}
>>>print(set)
{'a', 'b', 'd', 'c'}
• ### ​​​​​​​pop()： Randomly delete an element

>>>set= {'a','b','c','d'}
>>>set.pop()
>>>print(set)
{'c', 'b', 'a'}
• ### ​​​​​​​clear()： Empty the set

>>>set= {'a','b','c','d'}
>>>set.clear()
>>>print(set)
set()
• ### ​​​​​​​del     ： Delete the collection

>>>set = {'a','b','c','d'}
>>>del set
>>>print(set)
<class 'set'>

## ​​​​​​​​​​​​​​4、 Other operations of the collection

• ### ​​​​​​​4.1、 intersection （&  perhaps intersection）

>>>set1 = {1,2,3,4,5}
>>>set2 = {4,5,6,7,8}
>>>print(set1 & set2)
>>>print(set1.intersection(set2))
{4, 5}
{4, 5}
• ### ​​​​​​​4.2 、 Combine （| perhaps union）

>>>set1 = {1,2,3,4,5}
>>>set2 = {4,5,6,7,8}
>>>print(set1 | set2)
>>>print(set2.union(set1))
{1, 2, 3, 4, 5, 6, 7,8}
{1, 2, 3, 4, 5, 6, 7,8}
• ### ​​​​​​​4.3 、 Difference set （- perhaps difference）

>>>set1 = {1,2,3,4,5}
>>>set2 = {4,5,6,7,8}
>>>print(set1 - set2)
>>>print(set1.difference(set2))
{1, 2, 3}
{1, 2, 3}
• ### ​​​​​​​4.4、 Anti intersection  （^ perhaps symmetric_difference）

Is to remove the same part from two sets , The union of the rest

>>>set1 = {1,2,3,4,5}
>>>set2 = {4,5,6,7,8}
>>>print(set1 ^ set2)
>>>print(set1.symmetric_difference(set2))
{1, 2, 3, 6, 7, 8}
{1, 2, 3, 6, 7, 8}
• ### ​​​​​​​4.5、 Subsets and supersets

>>>set1 = {1,2,3}
>>>set2 = {1,2,3,4,5,6}
>>>print(set1 < set2) # The return is bool value
>>>print(set1.issubset(set2)) # The two are the same , It's all about instructions set1 yes set2 A subset of .
True
True
>>>print(set2 > set1)
>>>print(set2.issuperset(set1)) # The two are the same , It's all about instructions set2 yes set1 Superset .
True
True
• ### ​​​​​​​4.6、frozenset Immutable set , Make the set immutable .

s = frozenset('carry')
print(s,type(s))
frozenset({'c', 'y', 'b', 'r'}) <class 'frozenset'>
• ### ​​​​​​​4.7、isdisjoint()

isdisjoint() Method is used to determine whether two sets contain the same element , If No, return True, Some words return False.

>>>set1 = {1,2,3}
>>>set2 = {1,2,3,4,5,6}
>>>z= set1.isdisjoint(set2)
>>>print(z)
False
• ### ​​​​​​​4.8、​​​​​​​symmetric_difference_update()

Remove the same elements from the current collection in another specified collection , And insert different elements of another specified collection into the current collection .

>>>set1 = {1,2,3,4,5}
>>>set2 = {4,5,6,7,8}
>>>set1.symmetric_difference_update(set2)
>>>print(set1)
{1, 2, 3, 6, 7, 8}

# Conclusion ：

Thank you for seeing here ：

In an unpublished article, Lu Xun said ：“ If you understand the code, you must operate it yourself, so that you can better understand and absorb .”
One last sentence ： A man can succeed in anything he has unlimited enthusiasm , Let's make progress together