Home > other >  Duplicates in list of lists
Duplicates in list of lists

Time:10-23

I have a values list in the below format. For every item in the list of lists, I am trying to find out whether it is present in any other sub-lists - if so, I would like to get a count of it.

[
    ['37772'],
    ['38119', '38120'],
    ['38103-2807', '38103-2897', '38103', '38104'],
    ['38138-3904', '38138'],
    ['37421'],
    ['37772'],
    ['37067'],
    ['37203'],
    ['38115'],
    ['38305'],
    ['37916'],
    ['37356'],
    ['38119']
]

Result:

37772 - 2
38119 - 2
38120 - 1
38103-2807 - 1
....

Any leads on how to achieve this using built-in functions?

CodePudding user response:

You can use the built-in modules itertools.chain and collections.Counter

lst = [['37772'],
    ['38119', '38120'],
    ['38103-2807', '38103-2897', '38103', '38104'],
    ['38138-3904', '38138'],
    ['37421'],
    ['37772'],
    ['37067'],
    ['37203'],
    ['38115'],
    ['38305'],
    ['37916'],
    ['37356'],
    ['38119']]

from itertools import chain
from collections import Counter
items = Counter(chain.from_iterable(lst))

for k, v in items.items():
     print(k, v) 


>>> 37772 2
>>> 38119 2
>>> 38120 1
>>> 38103-2807 1
>>> ...

CodePudding user response:

li=[
    ['37772'],
    ['38119', '38120'],
    ['38103-2807', '38103-2897', '38103', '38104'],
    ['38138-3904', '38138'],
    ['37421'],
    ['37772'],
    ['37067'],
    ['37203'],
    ['38115'],
    ['38305'],
    ['37916'],
    ['37356'],
    ['38119']
]
for i in li:
    print(str(i) str(li.count(i)))
  • Related