Anadactothe
Anadactothe

Reputation: 170

How to count the occurence and number of duplicates in a list?

I am trying to count the duplicate characters of strings in a list; additionally I increment a variable depending on whether it is a duplicate 2 or 3 times.

However, out of my 250 string test list, it is returning a total count of 641, so I'm sure something's wrong.

Here is my code:

def findDupesNum(listy):
    a = []      # the current string's character list
    b = 0       # the 2-dupe counter
    c = 0       # the 3-dupe counter
    for i in listy:
        a.clear()
        for x in i:
            if x in a and a.count(x) == 1 and x != '':
                b += 1
            elif x in a and a.count(x) == 2 and x != '':
                c += 1
                b -= 1
            a.append(x)
        print('b = ' + str(b) + ' and c = ' + str(c))

Upvotes: 2

Views: 276

Answers (3)

ic3b3rg
ic3b3rg

Reputation: 14937

It would be a bit easier to count occurrences using a dict:

def findDupesNum(listy):
    for i in listy:
        counts = {}
        for x in i:
        if x:
            counts[x] = counts.get(x, 0) + 1
        doubles = 0
        triples = 0
        for i in counts:
            if counts[i] == 2:
                doubles = doubles + 1
            elif counts[i] == 3:
                triples = triples + 1

        print('{} doubles and {} triples'.format(doubles, triples))

Upvotes: 2

Ralf
Ralf

Reputation: 16515

This looks like a good use case for Counter (a subclass of dict).

from collections import Counter

def find_duplicates(string_list):
    total_counter = Counter()

    for s in string_list:
        s_counter = Counter(s)
        total_counter.update(s_counter)    # also update total counter

        print()
        print('this string:', s)
        print(
            'number of chars that appear 2 times in this string:',
            len([c for c, n in s_counter.items() if n == 2]))
        print(
            'number of chars that appear 3 times in this string:',
            len([c for c, n in s_counter.items() if n == 3]))

    # print()
    # print(
    #     'number of chars that appear 2 times in ALL strings:',
    #     len([c for c, n in total_counter.items() if n == 2]))
    # print(
    #     'number of chars that appear 3 times in ALL strings:',
    #     len([c for c, n in total_counter.items() if n == 3]))

The output could look like this:

>>> s_list = ['alfa', 'beta', 'gamma', 'aaabbcc']
>>> find_duplicates(s_list)

this string: alfa
number of chars that appear 2 times in this string: 1
number of chars that appear 3 times in this string: 0

this string: beta
number of chars that appear 2 times in this string: 0
number of chars that appear 3 times in this string: 0

this string: gamma
number of chars that appear 2 times in this string: 2
number of chars that appear 3 times in this string: 0

this string: aaabbcc
number of chars that appear 2 times in this string: 2
number of chars that appear 3 times in this string: 1

Upvotes: 2

ere
ere

Reputation: 56

This should be working:

l = ["bob",'bob', "thomas", "rémi", "marc", 'rémi']

dictElt = {}

for string in l:
    if string in dictElt.keys(): #if the key of the dictionnary exists
        dictElt[string] += 1
    else:
        dictElt[string] = 1

print(dictElt)
# to get your results 
for string, nbr in dictElt.items():
    if nbr == 1:
         print(string, "number of occurences =", nbr)
    elif nbr ==2:
         print(string, "number of occurences =",nbr)
    elif nbr == 3:
         print(string, "number of occurences =",nbr)

Upvotes: 0

Related Questions