This is the 10th day of my participation in Gwen Challenge
A collection of abstract
Set and is one of Python’s built-in data types and is a mutable data type.
- Data items are enclosed by curly braces {}
- The sum of sets is unordered mutable
- The bottom layer of a collection is implemented by a dictionary, and all of its elements are key objects of the dictionary, so they cannot be repeated and unique
The set and dictionary comparison table is as follows:
Collections have better dictionary performance than lists and tuples, and can perform lookup, add, and delete operations in constant time complexity O(1).
Now that we have learned about python’s data types, we have compiled a list of the features listed below:
1. Create a collection
1.1 Create using curly braces
A collection is typically created in curly braces
Set = {value1,value2,.... }Copy the code
If we define a Set Set ={“JueJing”,30,(1,2)}
Set2 = {"JueJing",30,(1,2)}Copy the code
1.2 Create using set()
The set() function converts iterables such as lists and tuples into collections.
- If duplicate data exists in the original data, only one duplicate data is retained
Set = Set (List) print(Set)Copy the code
2. Basic methods
2.1 Adding Elements
- The add () function
Use the add() function to add new elements to the collection.
- If the new element already exists, no operation is performed.
>>> Set = {"JueJing",30}
>>> Set.add("Reading")
>>> Set
{'JueJing', 'Reading', 30}
Copy the code
If you add an existing element, the Set element does not change
>>> Set.add("JueJing")
>>> Set
{'JueJing', 'Reading', 30}
>>>
Copy the code
- The update () function
You can also use the update() function to add new elements
>>> Set = {"JueJing",30}
>>> Set.update(["Reading"])
>>> Set
{'JueJing', 'Reading', 30}
>>>
Copy the code
2.2 Removing Elements
operation | operation | example | The return value |
---|---|---|---|
Remove the collection | remove() | s.remove(x) | If no value is returned, an exception is thrown |
Remove the collection | discard() | s.discard(x) | There is no return value |
Random delete | pop() | s.pop() | Return delete object |
Empty the collection | clear() | s.clear() | There is no return value |
- Remove () function
The remove() function removes the specified collection element.
If the element does not exist, an exception is thrown.
>>> Set = {"JueJing",30} >>> set.remove (30) >>> Set {'JueJing'} >>> Set. Remove ("Jue") # "Jue recent call last): File "<stdin>", line 1, in <module> KeyError: 'Jue' >>>Copy the code
- The discard () function
The discard() function also removes the specified element, but if the element does not exist, the step throws an exception.
> > > Set. The discard (30) > > > the Set {} 'JueJing' > > > Set. The discard (" Jue ") # does not exist, "Jue" element system without throwing an exception > > >Copy the code
- Pop () function
The pop() function randomly removes an element from the collection.
Because the bottom layer of a collection is a dictionary, the collection is also unordered (there is no notion of the first element, the last element), and the pop() function can be used to delete the elements of the collection at once.
> > > Set = {30} "JueJing" > > > Set. The pop () 'JueJing' > > > Set. The pop () 30 > > > Set. The pop () # when collection is empty, Traceback (most recent call last): File "<stdin>", line 1, in <module> KeyError: 'pop from an empty set' >>>Copy the code
- The clear () function
The clear() function clears the entire collection.
>>> Set = {"JueJing",30}
>>> Set.clear()
>>> Set
set()
>>>
Copy the code
2.3 count
Use len() to count the number of collection elements
>>> Set = {"JueJing",30}
>>> len(Set)
2
>>>
Copy the code
3. Perform other operations
3.1 Determine whether elements exist
key in set
Copy the code
>>> Set = {"JueJing",30}
>>> "JueJing" in Set
True
>>> "Jue" in Set
False
Copy the code
3.2 operation
Sets can also perform intersection, union and difference operations
>>> Set = {"JueJing",30}
>>> SetN = {3,"Jue","JueJing"}
>>> Set|SetN
{'JueJing', 3, 'Jue', 30}
>>> Set&SetN
{'JueJing'}
>>> Set-SetN
{30}
>>> Set.union(SetN)
{'JueJing', 3, 'Jue', 30}
>>> Set.intersection(SetN)
{'JueJing'}
>>> Set.difference(SetN)
{30}
>>>
Copy the code
conclusion
Python collections are very useful for efficiently removing duplicate values from data structures such as lists and performing common mathematical operations such as union and intersection.
One challenge people often encounter is when to use various data types, such as collections or dictionaries.
In different work scenarios, after learning, we should be more leisurely to choose to use data types, making our code more beautiful.
Well, that’s all for this episode. See you next time