We learned the concept of sets when we were studying mathematics. There is also a data type called sets in Python, which is also used to save unique elements. Simply put, a set contains some unique elements.
Elements have the following properties:
1) Disorder , the elements stored in the collection are not in order.
2) Diversity , the collection can store elements of multiple data types.
3) Uniqueness , the elements in the set are unique and will not appear repeatedly.
There are two ways to create a collection, one is to create it directly using the characteristic symbol ' {} ' of the collection, and the other is to create it using set() function forced type conversion.
First of all, we need to know the difference between a set and a dictionary. A set is a structure enclosed by '{}', and each element is separated by ','.
Collections and dictionaries are enclosed in curly braces, but colons are not used between collections.
The structure of the collection is:
my_set={element 1, element 2, element 3}
my_set is the name of the set to be created, and the elements in the brackets are the elements in the set.
>>>my_set={1,2,3,4,5,6}>>>my_set{1,2,3,4,5,6}
Using set() you can directly create an empty collection, or you can directly convert other types of structures into sets.
my_set=set(iteration)
my_set is the name of the collection to be created, the set() function is forced type conversion, and iteration is an iterable object, which can be a tuple, list, or range object.
>>>m=set('12345')#Forced string conversion>>>n=set([1,2,3,4,5])#Forced conversion list>>>k=set(range(1, 6))#Force range() object >>>i=set((1,2,3,4,5))#Force tuple>>>m{'4','3','5' ,'1','2'}>>>n{1,2,3,4,5}>>>k{1,2,3,4,5}>>>i{1,2,3, 4,5}
Before learning to add and delete elements, we can recall the del() method. The del() method can directly delete the entire collection. The syntax format is:
delmy_set
Sets are mutable sequences, so we can add and delete elements in them.
To add elements, use the add() method. The syntax format is as follows:
my_set.add(x)
my_set is the collection name, x is the element to be inserted.
>>>my_set#{1,2,3,4,5,6} before inserting>>>my_set.add('Insert a new element x')>>>my_set#{1,2,3,4 after inserting ,5,6,'Insert a new element x'}
To delete an element, you can use the pop() method or remove() method of the collection to delete an element, or you can use the clear() method to clear the collection.
It should be noted that the remove() method removes the specified element, while the pop() method directly deletes the first element in the collection and outputs it, and the clear() method directly clears all elements in the collection.
Look at the following example:
>>>my_set{1,2,3,4,5,6,'Insert a new element x'}>>>my_set.pop()# will delete and output the first element 1>>>my_set{2, 3,4,5,6,'Insert a new element x'}>>>my_set.remove(6)#Directly specify the delete 6>>>my_set{2,3,4,5,'Insert a new element x' }>>>my_set.clear()#Clear the set>>>my_set#The print result is displayed as an empty set set()
There are many ways to use sets. In the process of solving the problem, if we want to delete duplicate elements in the list, we can easily delete the redundant elements by using the set() method to force type conversion. In the next chapter, we will learn about the intersection of sets. , union and difference set.