假设我有几个列表数据,例如4个:
list1=['2','5','10']
list2=['3','6','8']
list3=['7','6','9']
list4=['5','4','3']
我如何找到列表中在其他的列表中含有的数据?重复的数据,比如说6在list2和list3都含有。
就打印出来6在list3和list2都包含。
求集合的交集:>>> set2 = set(list2)
>>> set3 = set(list3)
>>> print set2 & set 3
set(['6'])
其他操作:>>> x = set('abcde')
>>> y = set('bdxyz')
>>> x
set(['a', 'c', 'b', 'e', 'd']) # display format
>>> 'e' in x # Membership
True
>>> x – y # Difference
set(['a', 'c', 'e'])
>>> x | y # Union
set(['a', 'c', 'b', 'e', 'd', 'y', 'x', 'z'])
>>> x & y # Intersection
set(['b', 'd'])
>>> x ^ y # Symmetric difference (XOR)
set(['a', 'c', 'e', 'y', 'x', 'z'])
>>> x > y, x < y # Superset, subset
(False, False)
玩蛇网文章,转载请注明出处和文章网址:https://www.iplaypy.com/wenda/wd20177.html
相关文章 Recommend