简体   繁体   English

基于每个元组中存在的元素在列表中查找互斥元组的优雅方式

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

I want to subtract the two following tuples from each other to get the desired result (also included below). 我想从彼此中减去以下两个元组以获得所需的结果(也包含在下面)。 Note that the subtraction is based on only the a of the (a, b). 请注意,减法仅基于(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)]

I tried map(operation.sub, first, second) , didn't work. 我试过map(operation.sub, first, second) ,没用。 Tried b = map(sub, first, second) , didn't work. 试过b = map(sub, first, second) ,没用。 Says unsupported operand type(s) for -: 'tuple' and 'tuple . 表示不支持的操作数类型 - :'tuple'和'tuple

Thanks for your help and time in advance. 感谢您的帮助和提前的时间。

Edit : A solution that would help me most would include creating an intersection of the two tuples and subtracting that from each of the tuples. 编辑 :最能帮助我的解决方案包括创建两个元组的交集并从每个元组中减去它。

Edit: I want to subtract based on common items. 编辑:我想基于常见项目减去。 Hope that clarifies it. 希望澄清一下。

Here is what you are looking for - 这是你在找什么 -

# 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 (Edit) 交叉点(编辑)

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

Union (Extra) 联盟(额外)

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

Output 产量

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

You can use set with below list comprehension to get the desired result as: 您可以使用带有以下列表理解的 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)]

Explanation: 说明:

Here, I am using set to get the non-common words in the two list by performing XOR (also know as Exclusive OR) on two sets. 在这里,我使用set通过在两个集合上执行XOR(也称为异或)来获取两个列表中的非公共单词。 For example: 例如:

>>> 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',)])

Then I am iterating on both the list within list comprehension and checking whether they are present in above set of non-common words tuples. 然后,我在列表理解中的列表上进行迭代,并检查它们是否存在于上面的非常用单词元组中。 If they are present, then we are keeping it as the part of new list as: 如果它们存在,那么我们将它作为新列表的一部分保存为:

>>> 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)

Maybe follow is what you want: 也许以下就是你想要的:

# 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]

暂无
暂无

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

相关问题 优雅的方法从元组列表中提取元组,具有最小的元素值 - Elegant way to extract a tuple from list of tuples with minimum value of element 在元组列表中查找一个元组,如果不存在则添加 - Find a tuple in a list of tuples and add if not present 在元组的元组列表中通过内部元组查找元素 - Find an element by inner tuple in a list of a tuple of tuples 如何修改元组列表中元组的每个元素 - How to modify each element of a tuple in a list of tuples 从元组列表中删除元素,然后根据每个元组中的元素计算总价 - Deleting an element from a list of tuples and then calculating a total price based on elements within each tuple 从Python中的元组列表中获取每个元组的第n个元素的最佳方法 - Best way to get the nth element of each tuple from a list of tuples in Python 在 Python 中找到元组列表的平均值的最快方法是什么,每个元组包含一对命名元组? - What is the fastest way to find the average for a list of tuples in Python, each tuple containing a pair of namedtuples? 用python中元组列表中元组的第一个元素索引元素的最快方法 - Fastest way to index an element by the first element of a tuple in a list of tuples in python 按每个元组的第一个元素对元组列表进行排序-Python 2.7 - Sorting list of tuples by first element of each tuple - Python 2.7 基于每个元组中的值的元组分区列表 - Partition list of tuples based on a value within each tuple
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM