Link to home
Start Free TrialLog in
Avatar of BerkeleyJeff
BerkeleyJeff

asked on

Convert array to a dictionary of counts?

Is there a built-in function that converts an array of strings into a dictionary, where each key is a unique element in the array, and each value is the number of times that the corresponding key appeared in the array? I wrote such a function (below), but I would prefer to use either a built-in function, or at least a function from a common library.

def setToMap(array):
    dict = {}
    for element in array:
        if element in dict:
            dict[element] += 1
        else:
            dict[element] = 1
    return dict


Thanks!
Avatar of RichieHindle
RichieHindle

No, I don't believe there's a built-in function to do this.

Most people build up their own module of such functions, and import that module into each of their projects.
Avatar of pepr
In fact, you are implementing the multi-set (or bag). Using the dictionary type, which is very efficient in Python, is fine for that. Even the Sets module--the predecessor of built-in set type--implemented the sets  using the dictionary type. The comment says "This module implements sets using dictionaries whose values are ignored."

I personally would call the function bagFromSeq(seq), because you can iterate through any sequence of whatever the same way. I would also not use the identifier dict for the dictionary, because you are masking the same name of a built-in type.

You can even think about building your class Bag(dict):  if it makes sense in your case. Otherwise, you code looks fine.
Avatar of BerkeleyJeff

ASKER

Thanks for the suggestions. In response, I've rewritten the code and moved it to a seperate module:

def accumulate(hash, key, value):
    if key in hash:
        hash[key] += value
    else:
        hash[key] = value

def multisetCounts( multiset ):
    counts = {}
    for element in multiset:
        accumulate(counts, element, 1)
    return counts


Is there anything further that could be improved? I'm pretty new to Python, coming from Perl. Btw/in Perl this type of routine could be written in one short line:

map {counts{$_}++} multiset;
 

I'm surprised Python (a successor to Perl, known for its elegance) would require 5x as much code.
ASKER CERTIFIED SOLUTION
Avatar of RichieHindle
RichieHindle

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks! It was the 'get' function that I was looking for.