-1

I have 5 set of lists and some of strings in those lists are repetitive

now! I wanna know the number of repetition! for example the word "A" is in all of my lists by "B" is just in "3" or "C" is in 4 of them.

How can I sort this problem, by using remove() I faced to wrong answer

Thank you in advance!

Erfan
  • 1
  • 3

1 Answers1

-2

Take a look at Counter

from collections import Counter
a = ['a','a','b','b']
b = ['b','b','c','d']
c = a+b
cnt = Counter()
for x in c:
     cnt[x] +=1

print(cnt)
    Counter({'a': 2, 'b': 4, 'c': 1, 'd': 1})

The above will get you counts of each but it seems like you're more concerned at a list level.

from collections import defaultdict
f = defaultdict(list)
a = ['a','a','b','b']
b = ['b','b','c','d']
c = ['e','f','g','h']
d = a + b + c
 for i in d:
     f[i] = 0
     if i in b:
         f[i] += 1
     if i in c:
         f[i] +=1
     if i in a:
         f[i] +=1
print (f)

defaultdict(list,
                {'a': 1, 'b': 2, 'c': 1, 'd': 1, 'e': 1, 'f': 1, 'g': 1, 'h': 1})
W Stokvis
  • 1,409
  • 8
  • 15