1. What is a collection:
A set is an unordered sequence of non-repeating elements.
Important: The elements in the set are not repeatable and the elements in the set are still out of order.
Collection is a mutable container
The data objects in the collection are all unique (cannot be repeated)
Collection is iterable
The collection is equivalent to only the key is not worth the dictionary (the key is the data in the collection)
The elements in the collection can be set to immutable type frozenset()
When the set pop() is deleted, it is similar to the queue first in, first out.
That is to say, the collection can be set like a tuple with an unchangeable type; it can also be like a dictionary or a list by default, which can be changed iteratively; at the same time, the elements in the collection can be a list, a tuple, or a dictionary.
Second, why use collections
The characteristics of the collection determine it is simple, efficient, and fast.
For example, when using a list to remove duplicates:
list1=[1,2,3,4]
list2=[3,4,5,6]
list3=[]for i in list1:if i in list2:
list3.append(i)print(list3)
Use sets to remove duplication:
my_list =[1,2,3,4,2,3,1,2,2]
# Convert to collection
my_set =set(my_list)print(list(my_set))
[1, 2, 3, 4]
1、 Create a dictionary
You can use braces {} or set() to create a collection. Note: To create an empty collection, you must use set() instead of {}, because {} is used to create an empty dictionary.
my_set =set(('apple',))
my_set
{' apple'}
2、 Add elements (add(), update())
# add to the collection as a whole
my_set =set()
my_set.add("abc")print(my_set)
# update is to split the elements to be passed into the collection as individuals
my_set =set()
my_set.update("abc")print(my_set)
3、 Remove elements (remove(), pop(), discard())
# remove If the collection is present, delete it directly; if not, the program will report an error format: collection name.remove(element)
my_set ={11,13,15}
my_set.remove(13)print(my_set) my_set.remove(131)print(my_set)
# pop randomly delete elements in the set. If there are no elements in the set, the program will report an error
my_set ={11,13,15}
my_set.pop()print(my_set)
# discard If the element in the set exists, delete it directly; if the element does not exist, do nothing. Format:Collection name.discard(element)
my_set ={11,13,15}
my_set.discard(13)print(my_set)
my_set.discard(131)print(my_set)
4、 Intersection (&)
set1 ={9,2,3,4}
set2 ={3,4,5,16}
new_set = set1 & set2
print(new_set)
# result {3,4}
5、 Union (|)
set1 ={1,2,3,4}
set2 ={3,5,5,6}
new_set = set1 | set2
print(new_set)
# result {1,2,3,4,5,6}
6、 Difference (-)
The item is in the previous collection, but not in the latter collection.
set1 ={1,2,3,4}
set2 ={3,4,5,6}
new_set = set1 - set2
print(new_set)
# result {1,2}
7、 Symmetric Difference (^)
That is, the item is in the previous set or the next set, but does not appear in both. That is, the intersection minus the union.
set1 ={1,2,3,4}
set2 ={3,4,5,6}
new_set = set1 ^ set2
print(new_set)
# result {1,2,5,6}
8、 Subset judgment
set1 ={3,4}
set2 ={3,4,5,6}
# Determine whether set1 is a subset of set2
print(set1.issubset(set2))
# result True
9、 Parent set judgment
set1 ={3,4}
set2 ={3,4,5,6}
# Determine whether set1 is the parent set of set2
print(set1.issuperset(set2))
# result False
10、 Iteration and enumeration
s={34,56,76,34,56,54,43,23,56}for i in s:print(i) ##Output its content iteratively
for i,v inenumerate(s):print('index: %s,value: %s'%(i,v))"""
result:
index:0,value:34
index:1,value:43
index:2,value:76
index:3,value:54
index:4,value:23
index:5,value:56"""
It can be observed that the collection will automatically filter out the same elements.
Knowledge point expansion
Create collection
Note: You cannot create an empty collection using direct quantities
s = {1,2,3}
s = {“hello”,3.14,True,(2019,02,17)}
s = set() #Create an empty set
s = set(iterable) #Create a set using iterable objects
s = set(“ABC”) # s = {‘A’,’B’,’C’}
s = set([1,0,3.14,”hello”]) # s = {1,0,3.14,’hello’}
… …
This is the end of this article on what python collections can do. For more related python collections, please search for previous articles on ZaLou.Cn or continue to browse related articles below. Hope you will support ZaLou.Cn more in the future!
Recommended Posts