PHP前端开发

python-set集合类方法的详细介绍

百变鹏仔 3小时前 #Python
文章标签 详细介绍

s1=set([11,22,33,44,'tom','tony',11,77,2.5,])返回的是{11,22,33,44,‘tom’,‘tony’,77,2.5}(注意:返回的并不是一个字典,只是告诉你这个集合中含有这些元素,所以每次返回的结果元素的顺序可能是不一样的)

s2=set([11,22,33,44,'Tom','tony',11,55,66,])返回的是{11,22,33,44,‘Tom’,‘tony’,55,66}(注意:返回的并不是一个字典,只是告诉你这个集合中含有这些元素,所以每次返回的结果元素的顺序可能是不一样的)

add(...)
Add an element to a set.
This has no effect if the element is already present.(如果元素已存在,则添加无效。就是说明set是一个没有重复元素的集合。)

例如:s1=set([11,22,33,44,'Tom','tony',11,77,2.5,])

  s1.add('jimi')

立即学习“Python免费学习笔记(深入)”;

 print(s1)

结果:{33, 'tony', 2.5, 'jimi', 11, 44, 77, 22, 'Tom'}(注意:set是一个无序的集合,可能每次运行的结果的顺序不一样。)

difference(...)
Return the difference of two or more sets as a new set.(求主差集,并会生成一个新的集合)
(i.e. all elements that are in this set but not the others.)(注意:s1.difference(s2)表示的是找出s1中有而s2中没有的元素,并生成一个新的集合。s2.difference(s1)表示的是找出s2中有而s1中没有的元素,并生成一个新的集合。)

例如:s1=set([11,22,33,44,'Tom','tony',11,77,2.5,])

 s2=set([11,22,33,44,'Tom','tony',11,55,66,])

 s3=s1.difference(s2)

 s4=s2.difference(s1)

 print(s3)

 print(s4)

结果:{2.5, 77}

 {66, 55}

difference_update(...)
Remove all elements of another set from this set.(从这个集合中移除另一组的所有元素。注意:s1.difference_update(s2)表示的是移除s1中两者共有的元素,保留不共有的元素,是对s1的修改而不是生成一个新列表。s2.difference_update(s1)表示的是移除s2中两者共有的元素,保留不共有的元素,是对s2的修改而不是生成一个新列表。)

例如:s1=set([11,22,33,44,'Tom','tony',11,77,2.5,])

 s2=set([11,22,33,44,'Tom','tony',11,55,66,])

 s1.difference_update(s2)

 print(s1)

结果:{2.5, 77}

discard(...)
Remove an element from a set if it is a member.(移除集合中的成员(元素))
If the element is not a member, do nothing.(如果集合中没有这个成员,则不进行任何操作,也不会报错,这与Remove有区别。)

例如:s1=set([11,22,33,44,'Tom','tony',11,77,2.5,])

 s1.discard(2.5)

结果:{33, 'Tom', 11, 44, 77, 'tony', 22}

intersection(...)
Return the intersection of two sets as a new set.(生成两个集合的交集,并生成一个新的列表)
(i.e. all elements that are in both sets.)

例如:s1=set([11,22,33,44,'Tom','tony',11,77,2.5,])

 s2=set([11,22,33,44,'Tom','tony',11,55,66,])

 s5=s1.intersection(s2)

 s6=s2.intersection(s1)

 print(s5)

 print(s6)

结果:{33, 'Tom', 11, 44, 22, 'tony'}

 {33, 11, 44, 'tony', 'Tom', 22}

intersection_update(...)
Update a set with the intersection of itself and another.(功能和intersection(...)是一样的,但是s1.intersection(s2)在执行的时候是对原来的集合s1的修改,并不会生成一个新的集合)

例如:s1=set([11,22,33,44,'Tom','tony',11,77,2.5,])

 s2=set([11,22,33,44,'Tom','tony',11,55,66,])

 s1.intersection(s2)

 s2.intersection(s1)

 print(s1)

 print(s2)

结果:{33, 'Tom', 11, 44, 22, 'tony'}

 {33, 11, 44, 'tony', 'Tom', 22}

isdisjoint(...)
Return True if two sets have a null intersection.(判断两个集合是否有交集,如果有则返回False,如果没有则返回True)

例如:s1=set([11,22,33,44,'Tom','tony',11,77,2.5,])

 s2=set([100,50,500,])

 print(s1.isdisjoint(s2))

结果:True

issubset(...)
Report whether another set contains this set.(判断一个集合中的所有元素是否在另个集合中,s1.issubset(s2)表示的是s1中的每个元素都在s2中,注意与s1.issuperset(s2)的区别。  )

例如:s1=set([11,22,33,44,'Tom','tony',11,77,2.5,])

 s2=set([11,22,33,44,'Tom','tony',11,])

 s3=([11,22,33,44,'Tom','tony',11,55,66])

 s1.issubset(s2)

 s1.issubset(s3)

 print(s1.issubset(s2))

 print(s1.issubset(s3))

结果:True

 False

issuperset(...)
Report whether this set contains another set.(判断一个集合中的所有元素是否在另个集合中,s1.issubset(s2)表示的是s2中的每个元素都在s1中,注意与s1.issubset(s2)的区别。  )

例如:s1=set([11,22,33,44,'Tom','tony',11,77,2.5,])

 s2=set([11,22,33,44,'Tom','tony',11,])

 s3=([11,22,33,44,'Tom','tony',11,55,66])

 s1.issuperset(s2)

 s1.issuperset(s3)

 print(s1.issuperset(s2))

 print(s1.issuperset(s3))

结果:True

 False

pop(...)
Remove and return an arbitrary set element.(随机删除一个元素)
Raises KeyError if the set is empty.(如果集合为空,执行pop时会提醒KeyError)

例如:s1=set([11,22,33,44,'Tom','tony',11,77,2.5,])

 s1.pop()

结果:{33, 'Tom', 2.5, 11, 44, 77, 22}

remove(...)
Remove an element from a set; it must be a member.(移除集合中的成员(元素),这个元素必须在这个集合中)
If the element is not a member, raise a KeyError.(如果集合中没有这个成员,则会提示keyError,这与discard有区别。)

例如:s1=set([11,22,33,44,'Tom','tony',11,77,2.5,])

 s1.remove(2.5)

 s1.remove('jimi')

结果:{33, 'Tom', 'tony', 11, 44, 77, 22}

 KeyError: 'jimi'

symmetric_difference(...)
Return the symmetric difference of two sets as a new set.(生成一个新的列表,这个列表是两个列表中不重复元素的集合,s1.symmetric_difference(s2)表示s1中有s2中没有的元素和s2中有s1中没有的元素的集合)

(i.e. all elements that are in exactly one of the sets.)

例如:s1=set([11,22,33,44,'Tom','tony',11,77,2.5,])

 s2=set([11,22,33,44,'Tom','tony',11,55,66,])

 s4=s2.symmetric_difference(s1)

 print(s4)

结果:{2.5, 66, 77, 55}

symmetric_difference_update(...)
Update a set with the symmetric difference of itself and another.(对一个列表进行修改,两个列表中不重复元素的集合,s1.symmetric_difference(s2)表示s1中有s2中没有的元素和s2中有s1中没有的元素的集合)

例如:s1=set([11,22,33,44,'Tom','tony',11,77,2.5,])

 s2=set([11,22,33,44,'Tom','tony',11,55,66,])

 s1.symmetric_difference_update(s2)

 print(s1)

结果:{2.5, 66, 77, 55}

union(...)
Return the union of sets as a new set.(生成一个新集合,改列表是两个列表的所以成员(元素)的集合,s1.union(s2)表示的是包含s1,s2所有元素的新集合)
(i.e. all elements that are in either set.)

例如:s1=set([11,22,33,44,'Tom','tony',11,77,2.5,])

 s2=set([11,22,33,44,'Tom','tony',11,55,66,])

 s3=s1.union(s2)

 print(s3)

结果:{33, 2.5, 66, 'Tom', 11, 44, 77, 55, 22, 'tony'}

update(...)
Update a set with the union of itself and others.(将一个集合更新到另一个集合,s1.update(s2)表示将s2中的所有元素放到s1中,完成对s1的更新修改)

例如:s1=set([100,50,])

s2=set([11,22,33,44,'Tom','tony',11,55,66,])

 s1.update(s2)

 print(s1)

结果:{'tony', 33, 66, 100, 'Tom', 11, 44, 50, 22, 55}