Python添加两组并删除重复元素

标签 python python-2.7

如何添加两个集合并删除重复项

>>> a = set(['a', 'b', 'c'])
>>> b = set(['c', 'd', 'e'])
>>> c = a + b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'set' and 'set'
>>>

Expected output:
c = set(['a','b','c', 'd', 'e']) 

最佳答案

试试这个:

>>> a = set(['a', 'b', 'c'])
>>> b = set(['c', 'd', 'e'])
>>> c = a.union(b)

结果:

set(['a','b','c', 'd', 'e'])

关于Python添加两组并删除重复元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49227864/

相关文章:

python - 定义我的轴的限制,但使用 matplotlib 在两个方向上具有相同的比例

python - 如何根据其他字段值的条件逻辑设置模型 bool 字段

python - 强制在所有继承类中实现一个方法

python - 对于维度 1 的数组,轴 1 超出范围

php - Python 对 PHP 生成并存储在 Mysql 中的盐进行乱码

python-2.7 - Apache Airflow 1.9 : Dataflow exception at the job's end

Python.Net 无法导入 dll

子进程中的python getoutput()等价物

python - Matplotlib python 在颜色图中更改单一颜色

python - 设置 python.venvPath 无效