Python set function usage, how much do you know

Mondo Technology Updated on 2024-01-30

This article describes PythonsetMultifaceted applications of functions. 1.By learning the basics, the reader learns how to create a collection and perform basic operations2.and learned to use the collection method to make modifications. 3.We used a set inference, showing it creating a set in a single line of **4.EmphasizedsetIt is used in real-world scenarios such as deduplication, membership testing, and mathematical calculations.

Each paragraph** is accompanied by a detailed explanation.

Create a collection:useset()constructor or use curly bracesto create a collection. Elements in a collection are unique, and duplicate elements are automatically ignored.

An example of creating a collection. 

my_set = set([1, 2, 3, 3, 4, 5])

my_set

Explanation:The above ** passedset()The function creates a collectionmy_set, which contains unique elements。Duplicate elements are automatically culled.

Basic Operations:Collections support basic operations such as union, intersection, and difference.

set1 = 

set2 =

union_set = set1 |set2 union.

intersection set = set1 & set2 intersection.

difference set = set1 - set2 difference.

union_set,intersection_set,difference_set

Explanation:The above ** demonstrates the basic collection operation. union_setis the union of two setsintersection_setIt's an intersection,difference_setIt's the difference.

Collection Method:setA variety of methods are provided such as:add()remove()discard()and so on to modify the collection.

my_set = 

my_set.add(6) to add an element.

print(my_set)

my_set.remove(3) removes, which throws an error if it doesn't exist.

print(my_set)

my_set.discard(3) removes the element and does nothing if it doesn't exist.

print(my_set)

Explanation:This shows the use of the collection method. add()Used to add elements,remove()Used to remove elements,discard()It is also to remove the element, but not to throw an error if the element does not exist.

Set Derivation:Similar to list inferences, collection inferences allow you to create a set from a single line.

squared_set =
Explanation:The above ** uses set derivation to create a set of squared values from 1 to 5squared_set

Application Scenarios:setIt is often used in scenarios such as deduplication, membership testing, and arithmetic.

unique_numbers = set([1, 2, 2, 3, 4, 4, 5])

is_member = 3 in unique_numbers

is_member

Explanation:This paragraph is shownsetApplication in deduplication and membership testing. unique_numbersis a collection of unique numbers, whileis_memberUsed to check if the number 3 is in the set.

In summary,setFunctions have demonstrated their powerful collection processing capabilities in Python. With clear basic usage and advanced features, developers can more flexibly manipulate aggregate data, improving readability and execution efficiency. Deeper understandingsetFunctions will bring more convenience to collection operations in Python programming, and are important tools for every Python developer to master.

Related Pages