Detailed explanation of Python collection set (super detailed)

Detailed explanation of Python built-in functions/methods - collection set

A collection (set) is an unordered sequence of elements that do not repeat .

1. Construct a collection

You can use curly braces { } or the set() function to create a collection. Note: To create an empty collection, you must use set() instead of { } , because { } is used to create an empty dictionary.

1. Use braces{}

basket = {
    
    'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
print(basket)  # {'apple', 'orange', 'banana', 'pear'}

When using print()函数printout, the repeated elements in the collection will be automatically removed , and the order returned by each printout is not consistent with the initial one .

2. Using set()functions

fruit = set(("apple", "banana", "cherry"))  # 请留意这个双括号
print(fruit)  # {'banana', 'apple', 'cherry'}

When using set()函数to create a collection, be sure to pay attention to the double brackets .

2. Basic calculation

After the above study, we already know how to create collections. There are four types of calculations between two sets, namely -, , |, &and ^.

First create two collections, which are convenient for the following calculations.

a = set('abcdefg')
b = set('abg')

2.1 -

print(a - b)  # {'e', 'd', 'f', 'c'}

a - b means: elements contained in set a but not contained in set b

2.2 |

print(a | b)  # {'d', 'b', 'e', 'c', 'g', 'f', 'a'}

a | b means: all elements contained in set a or set b

2.3 &

print(a & b)  # {'a', 'b', 'g'}

a & b means: elements contained in both set a and set b

2.4 ^

print(a ^ b)  # {'e', 'c', 'd', 'f'}

a ^ b means: do not contain elements of set a and set b at the same time

3. Access the project

We cannot access the items in the set by referring to the index, because the set is unordered and the items do not have an index .

But you can use the for loop to traverse the set items, or use the in keyword to query whether the specified value exists in the set.

example

Iterate over the collection, and print the values:

fruit = {
    
    'apple', 'banana', 'cherry'}
for key in fruit:
    print(key)

Checks whether "banana" exists in the set

fruit = {
    
    'apple', 'banana', 'cherry'}
print('banana' in fruit)

If output True, it means that the specified value exists in the set; if output False, it means that the specified value does not exist in the set.

4. Built-in functions

4.1 Print output print()

1. print()Function

From the above construction set, we can know that the function of print() is to print out the content or value in parentheses () .

4.2 Calculate len() of elements in the set

2. len()Function

When we want to get the length of the set, that is, determine how many items are in the set, we use the len() method.

print(len(basket))  # 4
print(len(fruit))  # 3

We noticed that when constructing the above-mentioned sets of basket and fruit, 6 elements are filled in {} , and when we use the len() function to fix the length, we will find that it automatically removes duplicate elements before returning.

4.3 return variable type type()

3. type()Function

print(type(basket))  # <class 'set'>

When we use {} or set() to construct a collection, use the type() function on it, and it will output set, indicating that this is a collection.

4.4 Delete collection del

4. delFunction

We have constructed the two collections of basket and fruit, and if we need to delete them, use the del function .

fruit = set(("apple", "banana", "cherry"))  # 请留意这个双括号
del fruit
print(fruit)  # NameError: name 'fruit' is not defined.

Note: After using the del() function to delete the collection, if you use the print() function to print, an error will be reported NameError: name 'fruit' is not defined., which means that the fruit has not been defined, indicating that it has been deleted.

5. Built-in methods

5.1 Add elements add(), updata()

1. add()Method

The add() method is used to add an element to the collection, if the added element already exists in the collection, it will do nothing.

grammar

set.add(element)

parameter value

parameter describe
element required. The element to add to the collection.

example

Add an element to the fruits collection:

fruits = {
    
    "apple", "banana", "cherry"}
fruits.add("orange") 
print(fruits)

Output result:

{
    
    'apple', 'banana', 'orange', 'cherry'}

If the element already exists, the add() method will not add the element.

fruits = {
    
    "apple", "banana", "cherry"}
fruits.add("apple")
print(fruits)

Output result:

{
    
    'apple', 'banana', 'cherry'}

2. updata()Method

The update() method is used to modify the current collection. New elements or collections can be added to the current collection. If the added element already exists in the collection, the element will only appear once, and duplicates will be ignored.

grammar

set.update(set)

parameter value

parameter describe
set required. Insert the collection into the current collection.

example

Merge two collections so that duplicate elements appear only once:

x = {
    
    "apple", "banana", "cherry"}
y = {
    
    "google", "runoob", "apple"} 
x.update(y) 
print(x)

Output result:

{
    
    'banana', 'apple', 'google', 'runoob', 'cherry'}

5.2 Remove elements remove(), discard(), pop()

1. remove()Method

The remove() method is used to remove the specified element from the collection.

This method differs from the discard() method because the remove() method throws an error when removing an element that does not exist, while the discard() method does not.

grammar

set.remove(item)

parameter value

parameter describe
item required. Items to retrieve and delete.

example

Remove the element banana:

fruits = {
    
    "apple", "banana", "cherry"}
fruits.remove("banana") 
print(fruits)

Output result:

{
    
    'cherry', 'apple'}

2. discard()Method

The discard() method is used to remove the specified collection elements.

This method differs from the remove() method because the remove() method throws an error when removing an element that does not exist, while the discard() method does not.

grammar

set.discard(value)

parameter value

parameter describe
value required. The item to retrieve and delete.

example

Remove the element banana from the collection:

fruits = {
    
    "apple", "banana", "cherry"}
fruits.discard("banana") 
print(fruits)

Output result:

{
    
    'cherry', 'apple'}

3. pop()Method

The pop() method is used to randomly remove an element.

grammar

set.pop()

parameter value

No parameter value.

example

Remove an element at random:

fruits = {
    
    "apple", "banana", "cherry"}
fruits.pop()  
print(fruits)

Output result:

{
    
    'apple', 'banana'}

Items are removed using the pop() method , but this method will remove the last item.

Also keep in mind that a set is unordered , so you don't know what item is being removed.

5.3 Empty collection clear()

1. clear()Method

The clear() method is used to remove all elements in the collection.

grammar

set.clear()

parameter value

No parameters.

example

Remove all elements from the fruits collection:

fruits = {
    
    "apple", "banana", "cherry"}
fruits.clear()
print(fruits)

Output result:

set()

Special attention: the clear() function is to remove all elements in the collection , and the del function is to delete the collection .

Therefore, when using the clear() method or the del function on a collection, and using the print() function to print out the output respectively , the former will return an empty collection set(), while the latter will report an error.NameError: name 'fruit' is not defined.

clear() deletes and clears the collection from memory, but the memory address is not deleted, and del() will delete it from memory.

5.4 Merge collections union(), update()

In Python, there are several ways to join two or more collections.

You can use the union() method to return a new collection containing all the items in both collections, or you can use the update() method to insert all the items in one collection into the other.

1. union()Method

The union() method returns the union of two collections, which contains all the elements of the collection, and duplicate elements will only appear once.

grammar

set.union(set1, set2 ...)

parameter value

parameter describe
set1 required. The collection to integrate.
set2 optional. Another collection to integrate. You can compare as many collections as you want. Sets are separated by commas.

example

Merge two collections so that duplicate elements appear only once:

set1 = {
    
    'a', 'b', 'c'}
set2 = {
    
    1, 2, 3, 'c'}
print(set1.union(set2))

Output result:

{
    
    1, 2, 'b', 3, 'c', 'a'}

Merge multiple collections:

set1 = {
    
    'a', 'b', 'c'}
set2 = {
    
    1, 2, 3}
set3 = {
    
    'A', 'B'}
print(set1.union(set2, set3))

Output result:

{
    
    1, 2, 3, 'B', 'b', 'A', 'a', 'c'}

2. update()Method

The update() method has been explained before, and it is only shown as an example here.

example

The update() method inserts the items in set2 into set1 :

set1 = {
    
    'a', 'b', 'c'}
set2 = {
    
    1, 2, 3}
set1.update(set2)
print(set1)

Output result:

{
    
    1, 2, 3, 'b', 'a', 'c'}

Notes :

  • Both union() and update() will exclude any duplicates.
  • There are other ways to join two collections and only keep duplicates, or never keep duplicates.

5.5 Copy collection copy()

copy()method for copying a collection.

grammar

set.copy()

parameter value

No parameters.

example

Copy the fruits collection:

fruits = {
    
    "apple", "banana", "cherry"}
print(fruits.copy())

Output result:

{
    
    'apple', 'banana', 'cherry'}

5.6 Determine whether to contain the same element isdisjoint()

isdisjoint()The method is used to determine whether the two collections contain the same element, if not return True, otherwise return False.

grammar

set.isdisjoint(set)

parameter value

parameter describe
set required. The collection in which to retrieve identical items.

example

Determine whether there is an element in the set y that contains the set x:

x = {
    
    'a', 'b', 'c'}
y = {
    
    'x', 'y', 'z'}
print(x.isdisjoint(y))

Output result:

True

Note : When the isdisjoint() method judges whether two collections contain the same elements, if they do not contain the same elements, it returns True , and if they contain the same elements, it returns False

5.7 Return intersection intersection(), intersection_update()

1. intersection()Method

The intersection() method is used to return the elements contained in two or more collections, that is, the intersection.

grammar

set.intersection(set1, set2 ... etc)

parameter value

parameter describe
set1 required. The collection in which to retrieve identical items.
set2 optional. Other collections where the same items need to be retrieved. You can compare as many collections as you like. Sets are separated by commas.

example

Return a new collection whose elements are contained in both collection x and collection y:

x = {
    
    'a', 'b', 'c'}
y = {
    
    'x', 'y', 'z', 'c'}
print(x.intersection(y))

Output result:

{
    
    'c'}

Compute the intersection of multiple sets:

x = {
    
    'a', 'b', 'c'}
y = {
    
    'x', 'y', 'z', 'c'}
z = {
    
    'c', 'a', 'd'}
print(x.intersection(y, z))

Output result:

{
    
    'c'}

The returned collection contains only items present in both collections, or in all collections if more than two collections were used for comparison.

2. intersection_update()Method

The intersection_update() method is used to obtain elements that overlap in two or more collections, that is, to calculate the intersection.

grammar

set.intersection_update(set1, set2 ... etc)

parameter value

parameter describe
set1 required. The collection in which to retrieve equal items.
set2 optional. Another collection in which to retrieve equal items. You can compare as many collections as you like. Sets are separated by commas.

example

Remove elements from collection x that are not in collection y:

x = {
    
    'a', 'b', 'c'}  # y 集合不包含 a,被移除
y = {
    
    'x', 'y', 'b', 'c'}
x.intersection_update(y)
print(x)

Output result:

{
    
    'b', 'c'}

Compute the union of multiple sets:

x = {
    
    'a', 'b', 'c'}
y = {
    
    'c', 'd', 'e'}
z = {
    
    'f', 'g', 'c'}
x.intersection_update(y, z)
print(x)

Output result:

{
    
    'c'}

The intersection_update() method is different from the intersection() method, because the intersection() method returns a new collection, while the intersection_update() method removes non-overlapping elements from the original collection.

5.8 Judgment subset issubset(), issuperset()

1. issubset()Method

The issubset() method is used to determine whether all elements of the collection are contained in the specified collection, and return True if yes, otherwise False.

grammar

set.issubset(set)

parameter value

parameter describe
set required. A collection in which to retrieve equal items.

example

Return True if all items in collection x are present in collection y:

x = {
    
    'a', 'b', 'c'}
y = {
    
    'x', 'y', 'z', 'a', 'b', 'c'}
print(x.issubset(y))

Output result:

True

Return False if not all are included:

x = {
    
    'a', 'b', 'c'}
y = {
    
    'x', 'y', 'z', 'a'}
print(x.issubset(y))

Output result:

False

Note: x.issubset(y)When used, simply, when all the items in the collection x exist in the collection y, return True , otherwise (one or more items in the collection x do not exist in the collection y, return False ).

2. issuperset()Method

The issuperset() method is used to determine whether all elements of the specified collection are contained in the original collection, and return True if it is, otherwise False.

grammar

set.issuperset(set)

parameter value

parameter describe
set required. The collection in which to retrieve equal items.

example

Determine whether all elements of set y are contained in set x:

x = {
    
    'a', 'b', 'c', 'x', 'y', 'z'}
y = {
    
    'a', 'b', 'c'}
print(x.issuperset(y))

Output result:

True

Return False if not all are included:

x = {
    
    'a', 'x', 'y', 'z'}
y = {
    
    'a', 'b', 'c'}
print(x.issuperset(y))

Output result:

False

issuperset() is similar to issubset() , and returns True if all are included, otherwise (one or more are not included) returns False.

5.9 差集 difference() 、difference_update()

1. difference()Method

The difference() method is used to return the difference set of the collection, that is, the returned collection elements are included in the first collection, but not included in the second collection (parameter of the method).

grammar

set.difference(set)

parameter value

parameter describe
set required. The item to check for differences.

example

Returns a collection with elements contained in collection x but not in collection y:

x = {
    
    'a', 'b', 'c', 'x', 'y', 'z'}
y = {
    
    'a', 'b', 'c'}
print(x.difference(y))

Output result:

{
    
    'x', 'y', 'z'}

2. difference_update()Method

The difference_update() method is used to remove elements that exist in both collections.

grammar

set.difference_update(set)

parameter value

parameter describe
set required. The collection to check for differences.

example

Remove elements contained in both collections:

x = {
    
    'a', 'b', 'c', 'x', 'y', 'z'}
y = {
    
    'a', 'b', 'c'}
x.difference_update(y)
print(x)

Output result:

{
    
    'x', 'z', 'y'}

The difference between the difference_update() method and the difference() method is that the difference() method returns a new collection that removes the same elements , while the difference_update() method directly removes elements from the original collection without returning a value .

5.10 Remove elements symmetric_difference(), symmetric_difference_update()

1. symmetric_difference()Method

The symmetric_difference() method returns a set of unique elements in the two sets, that is, elements that exist in both sets will be removed.

grammar

set.symmetric_difference(set)

parameter value

parameter describe
set required. A collection of items to check for matches.

example

Returns a new collection of two collections with duplicate elements removed from both collections:

x = {
    
    'a', 'b', 'c', 'x', 'y', 'z'}
y = {
    
    'a', 'b', 'c', 'Q', 'W', 'E'}
print(x.symmetric_difference(y))

Output result:

{
    
    'x', 'Q', 'z', 'E', 'y', 'W'}

2. symmetric_difference_update()Method

The symmetric_difference_update() method removes elements in the current collection that are the same as in another specified collection, and inserts different elements in another specified collection into the current collection.

grammar

set.symmetric_difference_update(set)

parameter value

parameter describe
set required. A collection of items to check for matches.

example

Remove the duplicate elements in the original collection x and the collection y, and insert the non-duplicate elements into the collection x:

x = {
    
    'a', 'b', 'c', 'x', 'y', 'z'}
y = {
    
    'a', 'b', 'c', 'Q', 'W', 'E'}
x.symmetric_difference_update(y)
print(x)

Output result:

{
    
    'z', 'Q', 'x', 'W', 'E', 'y'}

6. Summary

function describe
print() 打印输出
len() 计算集合内元素
type() 返回变量类型
del 删除集合
方法 描述
add() 为集合添加元素
update() 给集合添加元素
remove() 移除指定元素
discard() 删除集合中指定的元素
pop() 随机移除元素
clear() 移除集合中的所有元素
union() 返回两个集合的并集
copy() 拷贝一个集合
isdisjoint() 判断两个集合是否包含相同的元素,如果没有返回 True,否则返回 False
intersection() 返回集合的交集
intersection_update() 返回集合的交集
issubset() 判断指定集合是否为该方法参数集合的子集
issuperset() 判断该方法的参数集合是否为指定集合的子集
difference() 返回多个集合的差集
difference_update() 移除集合中的元素,该元素在指定的集合也存在
symmetric_difference() 返回两个集合中不重复的元素集合。
symmetric_difference_update() 移除当前集合中在另外一个指定集合相同的元素,并将另外一个指定集合中不同的元素插入到当前集合中

以上就是本文的全部内容啦!如果对您有帮助,麻烦点赞啦!收藏啦!欢迎各位评论区留言!!!

最后编辑于:2022/7/23 15:43

Guess you like

Origin blog.csdn.net/m0_70885101/article/details/125948550