Solved

Convert array to a dictionary of counts?

Posted on 2006-07-10
6
1,745 Views
Last Modified: 2012-08-14
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!
0
Comment
Question by:BerkeleyJeff
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 14

Expert Comment

by:RichieHindle
ID: 17071618
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.
0
 
LVL 29

Expert Comment

by:pepr
ID: 17072340
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.
0
 

Author Comment

by:BerkeleyJeff
ID: 17076100
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.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 14

Accepted Solution

by:
RichieHindle earned 300 total points
ID: 17076658
For what it's worth, here's how I'd spell it:

def count_items(seq):
    counts = {}
    for s in seq:
        counts[s] = counts.get(s, 0) + 1
    return counts

if __name__ == '__main__':
    TEST = ['red', 'green', 'blue', 'green', 'pink', 'red', 'green']
    print count_items(TEST)

And also for what it's worth, one of the reasons Python is more elegant than Perl is that you can't express four line's worth of code in one line.  8-)
0
 
LVL 17

Assisted Solution

by:ramrom
ramrom earned 200 total points
ID: 17078565
counts={}
map(lambda x:counts.update({x:counts.get(x,0)+1}),seq)
0
 

Author Comment

by:BerkeleyJeff
ID: 17078591
Thanks! It was the 'get' function that I was looking for.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Python algorithm 20 162
mysql database, schema and table creation 13 89
Pass stdin text to Python subprocess 3 52
Converting .JS to .JSON and Vice-Versa 4 19
A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question