Python处理数组集合运算代码示例
发布时间
阅读量:
阅读量
本文重点阐述了Python数组中并集、交集与补集的相关代码实例,通过具体的示例代码进行详尽说明,对于学习者或从业者在实际应用中具有一定的参考价值,有需要的读者可加以借鉴。
并集
a = ["a", "b", "c", "d"]
b = ["b", "e"]
c = ["a", "b", "c", "d", "e"]
# 并
# 合并数组
a.extend(b)
# 去重
array = list(set(a))
print(array)
# 第二种方法
array = list(set(a)|set(b))
print(array)
打印结果:
['c', 'a', 'b', 'd', 'e']
['c', 'a', 'b', 'd', 'e']
交叉部分
a = ["a", "b", "c", "d"]
b = ["b", "
全部评论 (0)
还没有任何评论哟~
