python的集合运算
集合是python的基本数据结构,这比IDL简单多了
显然,python集合支持的数据类型是很丰富的,标量、多维矢量、字符等等都行!
x=[(1,2),(3,4),(2,3),(3,4)]
y=[(1,2),(2,3),(2,3),(2,4)]
print('x=',x)
print('y=',y)
print('x的集合=',set(x))
print('y的集合=',set(y))
print('交集=',set(x)&set(y))
print('并集=',set(x)|set(y))
print('并集=',set(x)-set(y))
x={'a','b','c'}
y={'b','c','d'}
print('交集=',set(x)&set(y))
print('并集=',set(x)|set(y))
print('并集=',set(x)-set(y))
x={1,2}
y={3,4}
if not x&y:
print('空集')
输出:
x= [(1, 2), (3, 4), (2, 3), (3, 4)]
y= [(1, 2), (2, 3), (2, 3), (2, 4)]
x的集合= {(1, 2), (3, 4), (2, 3)}
y的集合= {(1, 2), (2, 3), (2, 4)}
交集= {(1, 2), (2, 3)}
并集= {(1, 2), (2, 3), (3, 4), (2, 4)}
并集= {(3, 4)}
交集= {'c', 'b'}
并集= {'c', 'a', 'd', 'b'}
并集= {'a'}
空集
|