If I have a list ts of tuples in python:
ts = [(702,703), (703,704), (803,805), (803,806), (901,903), (902,903)]
How do I obtain a list containing common elements between 2 or more such tuples?
Assume that both the tuples in ts and the the elements in tuples are already numerically sorted.
For this example, the intended output should be:
ts_output = [703, 803, 903]
Below is my working so far:
ts = [(702,703), (703,704), (803,805), (803,806), (901,903), (902,903)]
ts = set(ts)
t1 = set(w for w,x in ts for y,z in ts if w == y) # t1 should only contain 803
print("t1: ", t1)
t2 = set(y for w,x in ts for y,z in ts if x == y) # t2 should only contain 703
print("t2: ", t2)
t3 = set(x for w,x in ts for y,z in ts if x == z) # t3 should only contain 903
print("t3: ", t3)
And this is the corresponding output:
t1: {803, 901, 902, 702, 703}
t2: {703}
t3: {704, 805, 806, 903, 703}
From above, only t2 gave the intended output, but I'm not sure what happened to t1 and t3.
You may use this alternative input to test your code, and it should give the exact same output:
ts = [(701,703), (702,703), (703,704), (803,805), (803,806), (901,903), (902,903), (903,904)]
import collections
ts = [(702,703), (703,704), (803,805), (803,806), (901,903), (902,903)]
flat_list = [item for sublist in ts for item in sublist]
duplicates = [item for item, count in collections.Counter(flat_list).items() if count > 1]
print(duplicates)
Given your input, you first need to flat your list.
#1 Simple and pythonic
flat_list = [item
for sublist in ts
for item in sublist]
#2 More efficient.
import itertools
flat_list = itertools.chain.from_iterable(ts)
In case of method #1 your flat_list will be list object in in case of method #2 it will be generator object. Both will behave same for iteration.
Now you can count the elements in your flat_list. If they are greater than 1 they are duplicates.
for item, count in collections.Counter(flat_list).items():
if count > 1:
print(item)
or you can use more pythonic list comprehension.
duplicates = [item
for item, count in collections.Counter(flat_list).items()
if count > 1]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With