Home > Backend Development > Python Tutorial > Give an example to illustrate the function of python3 set method

Give an example to illustrate the function of python3 set method

高洛峰
Release: 2017-03-21 14:25:11
Original
1653 people have browsed it

add(add elements)

name = set(['Tom','Lucy','Ben'])
name.add('Juny')print(name)#输出:{'Lucy', 'Juny', 'Ben', 'Tom'}
Copy after login

clear(clear all elements)

name = set(['Tom','Lucy','Ben'])
name.clear()print(name)#输出:set()
Copy after login

copy(copy set collection)

name = set(['Tom','Lucy','Ben'])
new_name = name.copy()print(new_name)#输出:{'Tom', 'Lucy', 'Ben'}
Copy after login

difference(returns different elements in two or more sets and generates a new set)

A = set([2,3,4,5= set([3,4= set([2=
Copy after login

difference_update(Delete elements in A set that exist in B set . )

A = set([2,3,4,5])
B = set([4,5])
A.difference_update(B)print(A)#输出:{2, 3}
Copy after login

discard(remove elements)

n = set([2,3,4])
n.discard(3)print(n)#输出:{2, 4}
Copy after login

intersection(take the intersection and generate a new set)

n1 = set([2,3,4,5])
n2 = set([4,5,6,7])
n = n1.intersection(n2)print(n)#输出:{4, 5}
Copy after login

intersection_update(take the intersection and modify the original set)

n1 = set([2,3,4,5])
n2 = set([4,5,6,7])
n1.intersection_update(n2)print(n1)#输出:{4, 5}
Copy after login

isdisjoint(Judge intersection, return False if yes, True if no)

n1 = set([2,3,4,5])
n2 = set([4,5,6,7])print(n1.isdisjoint(n2))#输出:False
Copy after login

issubset(Judge subset)

A = set([2,3])
B = set([2,3,4,5])print(A.issubset(B))#输出:True#A是B的子集
Copy after login

issuperset(Judge parent set)

A = set([2,3])
B = set([2,3,4,5])print(B.issuperset(A))# 输出:True#B是A的父集
Copy after login

pop(randomly remove an element)

n = set([2,3,4,5])
n1 = n.pop()print(n,n1)# 输出:{3, 4, 5} 2
Copy after login

remove(remove the specified element)

n = set([2,3,4,5])
n.remove(2)print(n)# 输出:{3, 4, 5}
Copy after login

symmetric_difference(take the intersection and generate a new set)

A = set([2,3,4,5])
B = set([4,5,6,7])print(A.symmetric_difference(B))# 输出:{2, 3, 6, 7}
Copy after login

symmetric_difference_update (take the intersection, change the original set)

A = set([2,3,4,5])
B = set([4,5,6,7])
A.symmetric_difference_update(B)print(A)# 输出:{2, 3, 6, 7}
Copy after login

union (take the union, and generate a new set)

A = set([2,3,4,5])
B = set([4,5,6,7])print(A.union(B))# 输出:{2, 3, 4, 5, 6, 7}
Copy after login

update (take the union, change the original set) Collection)

A = set([2,3,4,5])
B = set([4,5,6,7])
A.update(B)print(A)# 输出:{2, 3, 4, 5, 6, 7}
Copy after login

The above is the detailed content of Give an example to illustrate the function of python3 set method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template