根据每个元组中存在的元素在列表中查找互斥元组的优雅方法

Elegant way to find mutually exclusive tuples in the list based on the element present in each tuple

我想将以下两个元组彼此相减以获得所需的结果(也包括在下面)。注意减法只基于(a, b)的a。

# the two tuples
first = [(('the',), 431), (('and',), 367)]
second = [(('the',), 100), (('hello',), 100)]

# the desired result
first = [(('and',), 367)]
second = [(('hello',), 100)]

我试过 map(operation.sub, first, second),没用。试过 b = map(sub, first, second),没用。说 不支持的操作数类型 -: 'tuple' 和 'tuple.

感谢您的帮助和提前时间。

编辑:对我最有帮助的解决方案包括创建两个元组的交集并从每个元组中减去它。

编辑:我想根据共同项目减去。希望澄清它。

这是您要查找的内容 -

# the two tuples
first = [(('the',), 431), (('and',), 367)]
second = [(('the',), 100), (('hello',), 100)]

interim1 = {i[0][0]:i[1] for i in first}

interim2 = {i[0][0]:i[1] for i in second}

op1 = [ ((item,),interim1[item]) for item in interim1 if item not in interim2]
op2 = [ ((item,),interim2[item]) for item in interim2 if item not in interim1]
print(op1)
print(op2)

交叉点(编辑)

intersection = [ ((item,),interim1[item]) for item in interim1 if item in interim2]
print(intersection)

联盟(额外)

union = set().union(*[ op1, op2, intersection])
print(union)

输出

[(('the',), 431)]
[(('and',), 367)]
[(('hello',), 100)]
{(('hello',), 100), (('and',), 367), (('the',), 431)}

您可以将 set 与以下 列表理解 结合使用以获得所需的结果:

>>> first = [(('the',), 431), (('and',), 367)]
>>> second = [(('the',), 100), (('hello',), 100)]

>>> [t for t in first+second if t[0] in set(f[0] for f in first) ^ (set(s[0] for s in second))]
[(('and',), 367), (('hello',), 100)]

解释:

在这里,我使用 set 通过对两个集合执行 XOR (also know as Exclusive OR) 来获取两个列表中的非常用词。例如:

>>> first_words = set(f[0] for f in first)
>>> second_words = set(s[0] for s in second)

>>> first_words ^ second_words
set([('hello',), ('and',)])

然后我在 list comprehension 中迭代两个列表,并检查它们是否存在于上面的 non-common 单词集中元组。如果它们存在,那么我们将其作为新列表的一部分保留为:

>>> result = [t for t in first+second if t[0] in first_words ^ second_words]
# where `result` will hold value `[(('and',), 367), (('hello',), 100)]`

## If your resultant lists contains only two variable, 
## then you may assign them directly to individual variable as:
#     f, s = [t for t in first+second if t[0] in first_words ^ second_words]

## where `f` first required tuple will hold
# >>> f
# (('and',), 367)

## and `s` second required tuple will hold
# >>> s
# (('hello',), 100)

也许follow就是你想要的:

# the two tuples
first = [(('the',), 431), (('and',), 367)]
second = [(('the',), 100), (('hello',), 100)]

first_keys = set(_[0][0] for _ in first)
second_keys = set(_[0][0] for _ in second)

first = [_ for _ in first if _[0][0] not in second_keys]
second = [_ for _ in second if _[0][0] not in first_keys]

multi = [
    [(('the',), 431), (('and',), 367)],
    [(('the',), 100), (('hello',), 100)]
]

def get_key(x):
    return x[0][0]

def occur_counts(set_x):
    cnt = {}
    for x in set_x:
        cnt[get_key(x)] = cnt.get(get_key(x), 0) + 1
    return cnt


def do_one(single, total_cnt):
    single_cnt = occur_counts(single)
    return [_ for _ in single if single_cnt[get_key(_)] == total_cnt[get_key(_)]]


total_cnt = occur_counts(sum(multi, []))

answer = [do_one(_, total_cnt) for _ in multi]