A set is an unordered sequence of non-repeating elements.
Sets can be created using curly braces { } or the set() function.
student = {'Little Ming', 'xiaohong', 'adm'} print('data type of student', type(student)) # student's datatype <class 'set'>.
Basic operations on sets
1. Adding elements
add()
Function:
Used to add an element to a collection, if the element already exists in the collection then the function is not executed.
Usage:
(item) parameters: item:Elements to be added to the collection
a_list = ['python', 'django', 'django', 'flask'] a_set = set() a_set.add(a_list[0]) a_set.add(a_list[1]) a_set.add(a_list[2]) a_set.add(a_list[-1]) print(a_set) # {'flask', 'django', 'python'} # Duplicate elements not added to the set a_set.add(True) a_set.add(None) print(a_set) # {True, None, 'django', 'python', 'flask'} # The set is unordered
As evidenced by the example above:
1. A set is a sequence of non-repeating elements
2. Sets are unordered
update()
Function:
Add a new collection (list, element, string), ignoring the elements in the geometry if they exist in the original collection.
Usage:
(iterable) parameters: iterable:set (mathematics)、listings、tuple、string (computer science)
# update a_tuple = ('a', 'b', 'c') a_set.update(a_tuple) print(a_set) # {True, None, 'a', 'django', 'c', 'flask', 'b', 'python'} a_set.update('python') print(a_set) # {True, 'o', 't', None, 'h', 'a', 'django', 'c', 'flask', 'y', 'n', 'b', 'python', 'p'}
2、Remove elements
remove()
Function:
Removes an element from the set, if the element does not exist an error will be reported.
Methods:
(item) parameters: iten:An element in the current set
clear()
Function:
Empty all elements of the current collection
Usage:
(item) parameters: iten:An element in the current set
Important Notes:
- Collections can't get elements by index
- Collections can't get any method for an element
- A set is just a temporary type used to handle lists or tuples, he is not suitable for storing and transmitting
a_set.remove('python') print(a_set) # {'p', True, None, 'y', 'a', 't', 'o', 'flask', 'n', 'b', 'h', 'django', 'c'} a_set.clear() print(a_set) # set() a_set.remove('django') # KeyError: 'django'
3. Intersection of sets
What is intersection?
The set of identical elements possessed by two sets of subsheets a, b is called the intersection of a and b
intersection()
Function:
Returns elements that are contained in two or more sets, i.e., intersections
Usage:
a_set.intersection(b_set...) parameters: b_set...: One or more collections compared to the current collection return value: Returns the intersection of the original set and the comparison set
a = ['dewei', 'xiaomu', 'xiaohua', 'xiaoguo'] b = ['xiaohua', 'dewei', 'xiaoman', 'xiaolin'] c = ['xiaoguang', 'xiobai', 'dewei', 'xiaooyuan'] a_set = set(a) b_set = set(b) c_set = set(c) print(a_set, b_set, c_set) result = a_set.intersection(b_set, c_set) xiaotou = list(result) print('{}It's this thief.'.format(xiaotou[0]))
3. Concatenation of sets
What is concatenation?
- The elements possessed by the two set subtables a and b (with duplicates removed) are the concatenation of a and b
union()
Function:
- Returns the concatenation of multiple sets, i.e., contains all the elements of the set, with duplicate elements commanding one occurrence
Usage:
a_set.union(b_set...) parameters: b_set...:Compare one or more collections with the current collection return value: Returns the concatenation of the original set and the comparison set
a_school = ['Half-day Friday', 'Free weekend training', 'Friday off'] b_school = ['Dismissal time changed from 6:00 to 5:00', 'Leave less homework', 'Change the comfortable seat'] c_school = ['Leave less homework', 'Half-day Friday', 'Catering improvements'] a_set = set(a_school) b_set = set(b_school) c_set = set(c_school) print(a_set) print(b_set) print(c_set) # help_data = a_set.union(b_set, c_set) help_data = a_set.union(b_school, c_school) print(help_data)
summarize
That's all for this post, I hope it helped you and I hope you'll check back for more from me!