Jakub S
Jakub S

Reputation: 43

Python 3 - Find strings that appear in more than one list, the list are populated from function arguments

I have a function which accepts any number of arguments:

def lists(*args):

When I execute this function it will be executed with arguments like the following:

lists(['g', 'gh', 'ghj', 'g'], ['j', 'ju', 'gh', 'gk', 'gn'])

These arguments form an multi-dimensional array (at least I think they do). So if I

print(args)

I get the following:

(['g', 'gh', 'ghj', 'g'], ['j', 'ju', 'gh', 'gk', 'gn'])

Which is what I'd expect.

The problem I'm trying to solve is printing out strings from the above lists which appear in more than one list.

In the above example I can convert the lists into one flat list and print out duplicates which is half right, it would print out

'g', 'gh'

Because there they both appear more than once, but what I really want it to print is just

'gh'

Because it's the only that appears in more than one list.

Am I going about this completely the wrong way, or do I just lack the knowledge of how to achieve this.

I've looked online and found a few examples of how to achieve something similar however it's all using integers not strings. I'm not sure if I'm setting up the arrays/lists correctly which means that any filtering I do fails.

Thank you for your time!

Upvotes: 1

Views: 62

Answers (1)

Ma0
Ma0

Reputation: 15204

How about this:

def lists(*args):
  if len(args)>=2:
    res = set(args[0])
  else:
    return set()
  for sub in args[1:]:
    res = res.intersection(sub)
  return res

Tests:

print(lists(['g', 'gh', 'ghj', 'g'], ['j', 'ju', 'gh', 'gk', 'gn']))  # -> {'gh'}

print(lists())  # -> set()

print(lists(['g', 'gh', 'ghj', 'g']))  # -> {'gh'}

print(lists(['g', 'gh', 'gn', 'g'], ['ju', 'gh', 'gk', 'gn'], ['gn', 'gh']))  # -> {'gh', 'gn'}

Note that the above code works with any amount of lists passed to the function.


The problem with your code was the flattening. By flattening you were losing info regarding whether the duplicates were in all lists or multiple times in one.

What my code is doing is (after checking that at least two lists are passed) converting every list to a set and calculating the intersection (common elements) over all. The calculation of the intersection is done in pairs (1rst set with 2nb, the result of the previous intersection with 3rd and so on).

Upvotes: 2

Related Questions