Use Python's set() function to create a set
In Python, a set (set) is an unordered, variable data type, which is composed of Made up of only non-repeating elements. Compared with lists and tuples, sets have faster search speeds and do not allow duplicate elements. Sets are a very useful data structure when working with data that requires uniqueness.
The set() function in Python can be used to create a new collection. When using the set() function, you can pass in an iterable object as a parameter, such as a list, tuple or string. Here is some sample code showing how to create a collection using the set() function:
Creating an empty collection
my_set = set() print(my_set) # 输出 set()
Creating an empty collection containing elements Collection of
my_set = set([1, 2, 3, 4, 5]) print(my_set) # 输出 {1, 2, 3, 4, 5}
Create a collection containing strings
my_set = set("Hello") print(my_set) # 输出 {'o', 'l', 'H', 'e'}
In the above example, we can see that the set() function creates Create a collection and automatically remove duplicate elements. The collection is unordered, so its output order is random.
In addition to using the set() function, we can also use curly braces ({}) to create a set. But be aware that you can only use the set() function to create an empty collection, because {} will create an empty dictionary.
The function of sets is very powerful. It supports many common set operations, such as union, intersection, difference and symmetric difference. Here is some sample code:
Union
set1 = set([1, 2, 3]) set2 = set([3, 4, 5]) union_set = set1.union(set2) print(union_set) # 输出 {1, 2, 3, 4, 5}
Intersection
set1 = set([1, 2, 3]) set2 = set([3, 4, 5]) intersection_set = set1.intersection(set2) print(intersection_set) # 输出 {3}
Difference
set1 = set([1, 2, 3]) set2 = set([3, 4, 5]) difference_set = set1.difference(set2) print(difference_set) # 输出 {1, 2}
Symmetric difference set
set1 = set([1, 2, 3]) set2 = set([3, 4, 5]) symmetric_difference_set = set1.symmetric_difference(set2) print(symmetric_difference_set) # 输出 {1, 2, 4, 5}
Through the above example code, we can see that the operation of the set is very simple and intuitive. In actual programming, sets can be used to remove duplicate elements, determine whether an element exists, determine whether two sets intersect, etc.
In addition to basic operations, collections also support methods such as adding elements, deleting elements, and finding the length of the collection. For example:
Add element
my_set = set([1, 2, 3]) my_set.add(4) print(my_set) # 输出 {1, 2, 3, 4}
Delete element
my_set = set([1, 2, 3]) my_set.remove(2) print(my_set) # 输出 {1, 3}
Find the length of the set
my_set = set([1, 2, 3]) length = len(my_set) print(length) # 输出 3
Summary:
This article introduces how to use Python's set() function to create a set, and demonstrates the basic operations of sets and common set operations. Set is a very useful data structure. It can be used for operations such as removing duplicate elements, determining whether elements exist, and finding intersections and unions. In daily programming, rational use of collections can improve the efficiency and readability of the program.
The above is the detailed content of Create a collection using Python's set() function. For more information, please follow other related articles on the PHP Chinese website!