Home  >  Article  >  Backend Development  >  What are sets in Python? Simple collection operations

What are sets in Python? Simple collection operations

青灯夜游
青灯夜游Original
2019-01-19 16:40:1810733browse

What are sets in Python? This article will introduce you to Python collections and how to perform simple operations on collections. I hope it will be helpful to you.

#What are sets in Python?

In Python, a set is an unordered sequence of non-repeating elements. It is iterable and has no duplicate elements (each element is unique). [Recommended related video tutorials: Python Tutorial]

The collection of Python is similar to the collection of mathematical concepts, with the following additional conditions:

○ Collection The elements in cannot be repeated.

○ The elements in the collection are immutable (cannot be modified), but the entire collection is mutable.

○ No index is attached to any element in the python set. Therefore, they do not support any indexing or slicing operations.

Note:

1. Sets in python are usually used for mathematical operations, such as union, intersection, comparison, etc.

2. The main advantage of using a set compared to a list is that it has a highly optimized method for checking whether a specific element is contained in the set.

Simple operations of collections

1. Create a collection

You can use curly brackets { } Or use the set() function to create a set

Note: To create an empty set, you must use set() instead of {}, because {} is used to create an empty dictionary.

Example: Create a set by using the set() function or by placing all elements in a pair of curly braces. Notice how the order of the elements in the result changes.

Days=set(["Mon","Tue","Wed","Thu","Fri","Sat","Sun"])
Months={"Jan","Feb","Mar"}
Dates={21,22,17}
print(Days)
print(Months)
print(Dates)

Output:

What are sets in Python? Simple collection operations

2. Access the values ​​in the collection

We cannot access a single value in the collection value, only all elements can be accessed. A list of individual elements can be obtained by looping through the collection. Example:

Days=set(["Mon","Tue","Wed","Thu","Fri","Sat","Sun"])
for d in Days:
print(d)

Output:

What are sets in Python? Simple collection operations

3. Add elements

Use the add() method to add elements Add to collection

Days=set(["Mon","Tue","Wed","Thu","Fri","Sat"])
print(Days)
Days.add("Sun")
print(Days)

Output:

What are sets in Python? Simple collection operations

4. Remove elements

Use discard() method Remove elements from the collection. Example:

Days=set(["Mon","Tue","Wed","Thu","Fri","Sat"])
print(Days)
Days.discard("Tue")
print(Days)

Output:

What are sets in Python? Simple collection operations

5. Merge two collections

can be on both collections Performing a union operation produces a new set containing all the distinct elements from both sets. Example:

DaysA = set(["Mon","Tue","Wed"])
DaysB = set(["Wed","Thu","Fri","Sat","Sun"])
AllDays = DaysA|DaysB
print("A集合 ",DaysA)
print("\n")
print("B集合",DaysB)
print("\n")
print("A,B的并集:",AllDays)

Output:

What are sets in Python? Simple collection operations

In the above example, the element "Wed" appears in both collections, but in the new collection it will only There is one.

6. Find the same elements of two collections

You can perform an intersection operation on two collections, and a new collection will be generated, which only contains elements from the two collections. public elements. Example:

DaysA = set(["Mon","Tue","Wed","Sun"])
DaysB = set(["Wed","Thu","Fri","Sat","Sun"])
AllDays = DaysA & DaysB
print("A集合 ",DaysA)
print("\n")
print("B集合",DaysB)
print("\n")
print("A,B的交集:",AllDays)

Output:

What are sets in Python? Simple collection operations

7. Calculate the number of set elements

Use len () method to calculate the number of elements in the set, for example:

DaysA = set(["Mon","Tue","Wed"])
DaysB = set(["Mon","Tue","Wed","Thu","Fri","Sat","Sun"])
print("\n")
print("A集合:",DaysA)
print("元素个数为:",len(DaysA))
print("\n")
print("B集合:",DaysB)
print("元素个数为:",len(DaysB))

Output:

What are sets in Python? Simple collection operations

8. Determine whether the specified element exists in the set

You can use the operator in to determine whether the specified element exists in the collection. It will return True if it exists and False if it does not exist.

Example: Determine whether the elements "Runoob" and "Fri" are in the set Days

Days = set(["Mon","Tue","Wed","Thu","Fri","Sat","Sun"])
x="Runoob" in Days
y="Fri" in Days
print(x)
print("\n")
print(y)

Output:

What are sets in Python? Simple collection operations

9. Clear the collection

You can use clear() to clear the collection, for example:

Days = set(["Mon","Tue","Wed","Thu","Fri","Sat","Sun"])

print(Days.clear())

Output:

What are sets in Python? Simple collection operations

The above is the entire content of this article, I hope it will be helpful to everyone's study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of What are sets in Python? Simple collection operations. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more