brain storm
brain storm

Reputation: 31282

in python picking tuples from two list of tuples if both the list contain common elements

I want to make a new list of tuples from tuples of list1 if elements in list1 are present or common in list2.

list1 = [('We', 'all'), ('all', 'live'), ('live', 'in'), ('in', 'a'),
         ('a', 'yellow'), ('yellow', 'submarine.')]

list2 = [('A', 'live'), ('live', 'yellow'), ('yellow', 'submarine'),
         ('submarine', 'lifeform'), ('lifeform', 'in'), ('in', 'a'),
         ('a', 'sea.')]

expected output = [('live', 'in'), ('in', 'a'), ('a', 'yellow')]

my code is below: It works in this case, but somehow fails in large datasets.

All_elements_set1 = set([item for tuple in list1 for item in tuple])

All_elements_set2 = set([item for tuple in list2 for item in tuple])


common_set = All_elements_set1 & All_elements_set2

new_list = [(i,v) for i,v in list1 if i (in common_set and v in common_set)]

print new_list

Upvotes: 2

Views: 1551

Answers (2)

kiriloff
kiriloff

Reputation: 26333

Basically, you don't need to make a set for elements in list1. All you need if check, for each tuple in list1, whether their elements are in some tuple in list 2...

list1 = [('We', 'all'), ('all', 'live'), ('live', 'in'), ('in', 'a'),
         ('a', 'yellow'), ('yellow', 'submarine.')]

list2 = [('A', 'live'), ('live', 'yellow'), ('yellow', 'submarine'),
         ('submarine', 'lifeform'), ('lifeform', 'in'), ('in', 'a'),
         ('a', 'sea.')]

Elements_set2 = set([item for tuple in list2 for item in tuple])

print [(i,v) for i,v in list1 if (i in Elements_set2 and v in Elements_set2 )]

As you are not giving details about the case where your code fails, cannot check whether this one works on your failing example.

Upvotes: 0

root
root

Reputation: 80456

In [39]: from itertools import chain

In [40]: list1 = [('We', 'all'), ('all', 'live'), ('live', 'in'), ('in', 'a'),
    ...:          ('a', 'yellow'), ('yellow', 'submarine.')]
    ...: 
    ...: list2 = [('A', 'live'), ('live', 'yellow'), ('yellow', 'submarine'),
    ...:          ('submarine', 'lifeform'), ('lifeform', 'in'), ('in', 'a'),
    ...:          ('a', 'sea.')]
    ...: 

In [41]: elems = set(chain.from_iterable(list2))

In [42]: [tup for tup in list1 if elems.issuperset(tup)]
Out[42]: [('live', 'in'), ('in', 'a'), ('a', 'yellow')]

Upvotes: 3

Related Questions