Common list elements: Difference between revisions
Content deleted Content added
m →{{header|Phix}}: added syntax colouring the hard way |
A Python implementation |
||
Line 208: | Line 208: | ||
{3,9,6} |
{3,9,6} |
||
{2,3,6} |
{2,3,6} |
||
</pre> |
|||
=={{header|Python}}== |
|||
===Without Duplicates=== |
|||
<lang python>"""Find distinct common list elements using set.intersection.""" |
|||
def common_list_elements(*lists): |
|||
return list(set.intersection(*(set(list_) for list_ in lists))) |
|||
if __name__ == "__main__": |
|||
test_cases = [ |
|||
([2, 5, 1, 3, 8, 9, 4, 6], [3, 5, 6, 2, 9, 8, 4], [1, 3, 7, 6, 9]), |
|||
([2, 2, 1, 3, 8, 9, 4, 6], [3, 5, 6, 2, 2, 2, 4], [2, 3, 7, 6, 2]), |
|||
] |
|||
for case in test_cases: |
|||
result = common_list_elements(*case) |
|||
print(f"Intersection of {case} is {result}") |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
intersection of ([2, 5, 1, 3, 8, 9, 4, 6], [3, 5, 6, 2, 9, 8, 4], [1, 3, 7, 6, 9]) is [9, 3, 6] |
|||
intersection of ([2, 2, 1, 3, 8, 9, 4, 6], [3, 5, 6, 2, 2, 2, 4], [2, 3, 7, 6, 2]) is [2, 3, 6] |
|||
</pre> |
|||
===With Duplicates=== |
|||
<lang python>"""Find common list elements using collections.Counter (multiset).""" |
|||
from collections import Counter |
|||
from functools import reduce |
|||
from itertools import chain |
|||
def common_list_elements(*lists): |
|||
counts = (Counter(list_) for list_ in lists) |
|||
intersection = reduce(lambda x, y: x & y, counts) |
|||
return list(chain.from_iterable([elem] * cnt for elem, cnt in intersection.items())) |
|||
if __name__ == "__main__": |
|||
test_cases = [ |
|||
([2, 5, 1, 3, 8, 9, 4, 6], [3, 5, 6, 2, 9, 8, 4], [1, 3, 7, 6, 9]), |
|||
([2, 2, 1, 3, 8, 9, 4, 6], [3, 5, 6, 2, 2, 2, 4], [2, 3, 7, 6, 2]), |
|||
] |
|||
for case in test_cases: |
|||
result = common_list_elements(*case) |
|||
print(f"Intersection of {case} is {result}") |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
intersection of ([2, 5, 1, 3, 8, 9, 4, 6], [3, 5, 6, 2, 9, 8, 4], [1, 3, 7, 6, 9]) is [9, 3, 6] |
|||
intersection of ([2, 2, 1, 3, 8, 9, 4, 6], [3, 5, 6, 2, 2, 2, 4], [2, 3, 7, 6, 2]) is [2, 2, 3, 6] |
|||
</pre> |
</pre> |
||