Dave
Dave

Reputation: 13

Python - Count occurrences of an item as one loops through an unsorted array

I am having difficulty making a loop that counts the occurrences of a word in an array and when looping through said array makes another one with the current occurrence count of the word. I am able to get the total occurrences of the individual words using collections counter() but I dont necessarily need to know the total I need it incremental.

I have attempted using the dictionary made by counter() and looping back through the array but that generates an array which does half the job but the order of the original output is gone.

LOOP

for key, value in cnt.most_common():
    x = value
    y = 0
    for index, word in enumerate(reversed(outputList)):
        if key == word:
            if x > 0:
                outputFontSizeList.append(word + str(x-y))
                if y <= x:
                    y += 1
                else:
                    y = 0

OUTPUT

[5,4,3,2,1,3,2,1,4,3,2,1,1,1] 

The wanted result is something detailed below, if I had list like

['cat','dog','neck','book','neck','bottle','apple','orange','cat','dog','cat','apple','neck','cat','dog']

I would after looping have an array that has counters like so corresponding the the current number of occurrences of said word matching the other array

[1,1,1,1,2,1,1,1,2,2,3,2,3,4,3] 

Upvotes: 1

Views: 2137

Answers (4)

Scott Boston
Scott Boston

Reputation: 153460

You can do this pretty easily with Pandas:

import pandas as pd
l = ['cat','dog','neck','book','neck','bottle','apple','orange','cat','dog','cat','apple','neck','cat','dog']
s = pd.Series(l)
s.groupby(s).cumcount().add(1).tolist()

Output:

[1, 1, 1, 1, 2, 1, 1, 1, 2, 2, 3, 2, 3, 4, 3]

Upvotes: 1

Franz Forstmayr
Franz Forstmayr

Reputation: 1300

Updated my answer

class my_cnt:

def __init__(self):
    self.data = dict()

def count(self, val):
    if not val in self.data.keys():
        self.data.update({val : 1})
    else: 
        self.data[val] = self.data[val] + 1

    return self.data[val]

lst = ['cat','dog','neck','book','neck','bottle','apple','orange','cat','dog','cat','apple','neck','cat','dog']
cnt = my_cnt()

output = [cnt.count(e) for e in lst]
print(output)

Upvotes: 0

David A
David A

Reputation: 413

I would recommend just counting as you go through rather than using collections.Counter. Using collections.defaultdict(int) might be helpful though:

import collections
arr = ['cat','dog','neck','book','neck','bottle','apple','orange','cat','dog','cat','apple','neck','cat','dog']
c = collections.defaultdict(int)
output = []
for word in arr:
    c[word] += 1
    output.append(c[word])
print(output)

Upvotes: 1

Owen Campbell
Owen Campbell

Reputation: 632

def get_occurences( input ):
    output = []
    occurences_dict = {}

    for word in input:
        if word in occurences_dict:
            occurences_dict[ word ] += 1
        else:
            occurences_dict[ word ] = 1
        output.append( occurences_dict[ word ] )
    return output

Running with:

input = [
    "cat",
    "dog",
    "neck",
    "book",
    "neck",
    "bottle",
    "apple",
    "orange",
    "cat",
    "dog",
    "cat",
    "apple",
    "neck",
    "cat",
    "dog"
]

print( get_occurences( input ) )

Gives [1, 1, 1, 1, 2, 1, 1, 1, 2, 2, 3, 2, 3, 4, 3]

Basically, use a dictionary to keep a running count of each word you encounter and append it to your output array. If you need the total count of each word, return the dictionary used as well.

Upvotes: 0

Related Questions