Index similar entries in Python
Question: I have a column of data (easily imported from Google Docs thanks to
[gspread](http://burnash.github.com/gspread/userguide.html#getting-the-entire-
row-or-column)) that I'd like to intelligently align. I ingest entries into a
dictionary. Input can include email, twitter handle or a blog URL. For
example:

`mike.j@gmail.com  
@mikej45  
j.mike@world.eu  
_http://tumblr.com/mikej45`

Right now, the "dumb" version is:

    
    
    def NomineeCount(spreadsheet):
        worksheet = spreadsheet.sheet1
        nominees = worksheet.col_values(6) # F = 6
        unique_nominees = {}
        for c in nominees:
            pattern = re.compile(r'\s+')
            c = re.sub(pattern, '', c)
            if unique_nominees.has_key(c) == True: # If we already have the name
                unique_nominees[c] += 1
            else:
                unique_nominees[c] = 1
    
            # Print out the alphabetical list of nominees with leading vote count
            for w in sorted(unique_nominees.keys()):
            print string.rjust(str(unique_nominees[w]), 2)+ "  " + w
    
            return nominees
    

What's an efficient(-ish) way to add in some smarts during the if process?


Answer: You can try with defaultdict:

    
    
    from collections import defaultdict
    unique_nominees = defaultdict(lambda: 0)
    unique_nominees[c] += 1
    

