python之集合关系的交,差,并集
作者:互联网
1.python_1=['yy','lyf','wyb','xz','lx']
linux_2=['gql','ch','wyb','dc','xz']
ps=set(python_1)
ls=set(linux_2)
print(ps.intersection(ls)) //输出结果为{'xz', 'wyb'},求集合交集
print(ps&ls) //输出结果为{'xz', 'wyb'}
2.python_1=['yy','lyf','wyb','xz','lx']
linux_2=['gql','ch','wyb','dc','xz']
ps=set(python_1)
ls=set(linux_2)
print(ps.union(ls)) //输出结果为{'ch', 'lyf', 'lx', 'wyb', 'gql', 'xz', 'dc', 'yy'},求集合并集
print(ps|ls) //输出结果为{'ch', 'lyf', 'lx', 'wyb', 'gql', 'xz', 'dc', 'yy'}
3.python_1=['yy','lyf','wyb','xz','lx']
linux_2=['gql','ch','wyb','dc','xz']
ps=set(python_1)
ls=set(linux_2)
print('差集:',ps-ls) //输出结果为差集: {'yy', 'lx', 'lyf'},求集合差集
print('差集:',ps.difference(ls)) //输出结果为差集: {'lx', 'lyf', 'yy'}
print('差集:',ls-ps) //输出结果为差集: {'dc', 'ch', 'gql'}
标签:ps,并集,python,差集,xz,lyf,ls,集合,wyb 来源: https://www.cnblogs.com/cxydnxs/p/12229750.html