简体   繁体   English

如何将元组列表中的唯一元素作为另一个元组返回

[英]How to return unique elements in list of tuple as another tuple

Assume I have list of tuples a=[(0,1),(2,0),(1,4)] and I want to return unique element of each two tuples as a new tuple.假设我有元组列表a=[(0,1),(2,0),(1,4)]并且我想将每两个元组的唯一元素作为新元组返回。 For example (0,1) and (2,0) returns (1,2) .例如(0,1)(2,0)返回(1,2) Also (0,1) and (1,4) return (0,4) . (0,1)(1,4)也返回(0,4)

Therefore, output is unique=[(1,2),(0,4)]因此,output 是unique=[(1,2),(0,4)]

I tried code below but it seems I am not in correct path:我尝试了下面的代码,但似乎我不在正确的路径中:

from itertools import combinations
a=[(0,1),(2,0),(1,4)]

b=list(combinations(a,2))
def myComp(pair1, pair2):
    if any(x == y for x, y in zip(pair1, pair2)):
        return(pair1,pair2)

d=[]
for i in range(len(b)):
    c=myComp(b[i][0],b[i][1])
    d.append(c) 

First, convert all the tuples in a to sets.首先,将a所有元组转换为集合。

a2 = [set(i) for i in a]

Then, take combinations of two elements of a2 .然后,取a2的两个元素的组合。

b2 = itertools.combinations(a2, 2)
# print(list(b2)) gives:
# [({0, 1}, {0, 2}), ({0, 1}, {1, 4}), ({0, 2}, {1, 4})]

Then, for every pair in b2 , find the symmetric difference.然后,对于b2中的每一对,找到对称差。

answer = [tuple(x ^ y) for x, y in b2]
# answer: [(1, 2), (0, 4), (0, 1, 2, 4)]

Like Ironkey mentions in the comments, you get (0, 1, 2, 4) in the last element because you're comparing (0, 2) and (1, 4) , and all elements in these tuples are mutually exclusive.就像 Ironkey 在评论中提到的那样,您在最后一个元素中得到(0, 1, 2, 4) ,因为您正在比较(0, 2)(1, 4) ,并且这些元组中的所有元素都是互斥的。

To filter out the four-element tuples, you can simply add that condition:要过滤掉四元素元组,您可以简单地添加该条件:

answer_f = [x for x in answer if len(x) == 2]
# answer_f: [(1, 2), (0, 4)]

crude answer without any list comps:没有任何列表组合的粗略答案:

from itertools import combinations

a = [(0,1),(0,2),(0,3),(1,2),(1,3),(1,4),(2,3)]

uniques = []
for x, y in combinations(a,2):
    z = []
    for i in x:
        if i not in y:
           z.append(i)
    for i in y:
        if i not in x:
           z.append(i)
    if len(z) == 2:
        uniques.append(tuple(z))

print(list(set(uniques)))
[(0, 1), (2, 4), (1, 2), (0, 4), (3, 4), (0, 3), (2, 3), (0, 2), (1, 3)]

This doesn't use packages.这不使用包。

The conditions if i.= x and a.index(i)<a.index(x) make sure we aren't comparing identical tuples and also we are comparing the same pair of tuples once if i.= x and a.index(i)<a.index(x)的条件确保我们没有比较相同的元组,而且我们只比较同一对元组一次

Then we just grab the element of they are not in the other tuple然后我们只抓取它们不在其他元组中的元素

The condition if len(m) == 2 and sorted(m) in uniq makes sure that the list is only 2 elements long, and also solves your comment about having (2,1) & (1,2) if len(m) == 2 and sorted(m) in uniq的条件确保列表只有 2 个元素长,并且还解决了您关于具有(2,1) & (1,2)的评论

a=[(0,1),(2,0),(1,4),(0,2)]

uniq = []
for i in a:
  for x in a:
    if i != x and a.index(i)<a.index(x):
      m = [y for y in i if y not in x] + [z for z in x if z not in i]
      if len(m) == 2 and tuple(sorted(m)) not in uniq:
        uniq.append(tuple(m))

print(uniq)

>>> [(1, 2), (0, 4)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM