This section focuses on the introduction python The set in
Definition of set , The basic operation of a set , Common functions in Collections
Tips : The following is the main body of this article
adopt set Function to create a collection , Out of commission {} To create an empty collection
a_set = set() # Empty set
a_set = set([1, 2, 3]) # Pass in a list or tuple
b_set = {
1, 2, 3} # Incoming elements
c_set = {
} # Error definition ,{} The default is the dictionary
case
# Simple use of collections
a = set()
print(type(a))
b = set(['python', 'django', 'flask'])
print(b)
c = {
(1, 2, 3), '123', 1}
print(c)
d = {
}
print(d, type(d))
a_list = ['python', 'django', 'python', 'flask']
b_set = set(a_list)
print(b_set)
Running results
Collection needs attention b Instead of adding a list to a collection , Instead, you add elements from the list to the collection
d By default, dictionaries are created instead of collections
<class 'set'>
{
'django', 'python', 'flask'}
{
'123', 1, (1, 2, 3)}
{
} <class 'dict'>
{
'django', 'python', 'flask'}
Used to add an element to the collection , If the element already exists in the collection, the function does not execute
set.add(item)
item Elements to add to the collection
case:
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)
a_set.add(True)
a_set.add(None)
print(a_set)
Running results
{
'python', 'flask', 'django'}
{
True, 'django', 'flask', 'python', None}
The last element added to the set is not necessarily at the last edge of the set , Sets are unordered
Add a new collection ( Or list 、 Tuples 、 character string ), If the elements in the new set exist in the original set, they will be ignored
set.update(iterable)
iterable: aggregate , List tuple string
case:
a_tuple = ('a', 'b', 'c')
a_set = set()
a_set.update(a_tuple)
print(a_set)
a_set.update('python')
print(a_set)
Running results :
{
'a', 'b', 'c'}
{
'h', 'c', 'o', 'n', 'a', 'b', 'y', 'p', 't'}
The elements in the string are broken up and added to the collection
have access to update Instead of a function add function
update Think of it as add The advanced version of , You can add more than one member at a time
Delete an element in the collection , If the element does not exist, an error will be reported
set.remove(item)
item: An element in the current collection , Be careful It's not an index , Set has no index
Empty all elements in the current collection
set.clear()
No parameter
del Only the collection itself can be deleted directly
case:
a_set = set()
a_list = ['python', 'django', 'django', 'flask']
a_set.update(a_list)
print(a_set)
print('func remove')
a_set.remove('python')
print(a_set)
print('func clear')
a_set.clear()
print(a_set)
print('func del')
del(a_set)
Running results :
{
'python', 'flask', 'django'}
func remove
{
'flask', 'django'}
func clear
set()
func del
difference function
Returns the difference set of a set , That is, the returned set elements are contained in the first set , But not in the second set
a_set.diference(b_set)
b_set The current set needs to be compared
Return value : Returns the difference between the original set and the comparison set
case:
drivers = ['dewei', 'xiaomu', 'xiaoming', 'xiaoman']
testers = ['xiaomu', 'xiaoman', 'xiaogang', 'xiaotao']
dirver_set = set(drivers)
test_set = set(testers)
sinple_drivers = dirver_set.difference(test_set)
print(sinple_drivers)
Running results :
{
'xiaoming', 'dewei'}
intersection function
Returns the elements contained in two or more collections
a_set.intersection(b_set…)
b_set…: Compared with the current set 1 One or more sets
Return value : Returns the intersection of the original set and the comparison set
case:
a = ['dewei', 'xiaomu', 'xiaohua', 'xiaoguo']
b = ['xiaohua', 'dewei', 'xiaoman', 'xiaolin']
c = ['xiaoguang', 'xiaobai', 'dewei', 'xiaoyuan']
a_set = set(a)
b_set = set(b)
c_set = set(c)
print(a_set, b_set, c_set)
res = a_set.intersection(b_set, c_set)
# Convert a collection to a list
xiaotou = list(res)
print('{} It's a thief '.format(xiaotou[0]))
Running results :
{
'xiaomu', 'dewei', 'xiaohua', 'xiaoguo'} {
'xiaolin', 'dewei', 'xiaoman', 'xiaohua'} {
'dewei', 'xiaoguang', 'xiaoyuan', 'xiaobai'}
dewei It's a thief
union function
Returns the union of sets , That is, it contains all the elements of the set , Repeated elements only appear once
a_set.union(b_set…)
b_set…: Compared with the current set 1 Or sets
case:
a_school = [' Half a day on Friday ', ' Free training on weekends ', ' Friday off ']
b_school = [' School hours start from 6 Point changed to 5 spot ', ' Leave less homework ', ' Change to a comfortable seat ']
c_school = [' Leave less homework ', ' Half a day on Friday ', ' Food improvement ']
a_set = set(a_school)
b_set = set(b_school)
c_set = set(c_school)
print(a_set, b_set, c_set)
help_data = a_set.union(b_set, c_set)
# help_data = a_set.union(b_school, c_school)
# The same function can be achieved by using the annotation section
print(help_data)
Running results :
{
' Half a day on Friday ', ' Friday off ', ' Free training on weekends '} {
' Leave less homework ', ' School hours start from 6 Point changed to 5 spot ', ' Change to a comfortable seat '} {
' Food improvement ', ' Leave less homework ', ' Half a day on Friday '}
{
' Friday off ', ' Change to a comfortable seat ', ' Food improvement ', ' Leave less homework ', ' Half a day on Friday ', ' School hours start from 6 Point changed to 5 spot ', ' Free training on weekends '}
isdisjoint function
function : Determine whether two sets contain the same elements , If there is no return True, Otherwise return to False
usage :a_set.isdisjoint(b_set)
b_set: And the set used by the current set to judge
Returns a Boolean value
case:
# shortcoming , Can not contain
ccompany_not_allow = {
' Woman ', ' Drink ', ' smoking ', ' sleep late '}
one_player = {
' male ', ' running ', ' Vigour ', ' Drink '}
two_player = {
' Woman ', ' routine ', ' Taekwondo '}
three_player = {
' male ', ' Taiji boxing '}
four_player = {
' male ', ' Karate ', ' young '}
print(ccompany_not_allow.isdisjoint(one_player))
print(ccompany_not_allow.isdisjoint(two_player))
print(ccompany_not_allow.isdisjoint(three_player))
print(ccompany_not_allow.isdisjoint(four_player))
Running results :
False
False
True
True
Used 5 A collection of , The main set is ccompany_not_allow, in addition 4 Sets correspond to 4 Personal characteristics
Here is a summary of the article :
This article introduces the creation of collections , The difference between sets and lists , Common functions for adding, deleting and modifying sets and functions for set operations .