我该怎么做呢:
(("and", "dog"), ("a", "dog"))
进入这个:
("and", "dog", "a")
这意味着只需要使用一次公共元素"dog"
。
答案 0 :(得分:3)
>>> tuple(set(("and", "dog")) | set(("a", "dog")))
('and', 'a', 'dog')
或者,通常:
import operator
tuple(reduce(operator.__or__, map(set, mytuples)))
答案 1 :(得分:0)
您可以将其转换为set。
diff = (set(("a","dog")) - set(("and","dog")))
result = list(("and","dog")) + list((diff))
print(result) #['and', 'dog', 'a']
答案 2 :(得分:0)
如果订单无关紧要:
>>> tuple(set.union(*map(set, tuples)))
('and', 'a', 'dog')
如果订单确实重要:
>>> seen = set()
>>> tuple(elem for tupl in tuples for elem in tupl
if elem not in seen and not seen.add(elem))
('and', 'dog', 'a')