Home > Software engineering >  Python: How do I count unique list elements and put that count as dictionary values?
Python: How do I count unique list elements and put that count as dictionary values?

Time:10-04

I'm doing a project for a class. I'm at a stage where I need to count disaster areas. I've defined a function that is supposed to pull the names from the multi-dimensional list, count the # of times each unique element occurs, and then marry those two items as keys & values respectively in a new dictionary. I have managed to do the first part(establish the areas as keys), but I can't figure out how to get the unique counts for how often each area occurs in the list, and assign them as the values.

I've attempted to use a variable to count each one, but the results are all wrong. The counts coming out seem to be random.

# areas affected by each hurricane
areas_affected = [['Central America', 'Mexico', 'Cuba', 'Florida', 'The Bahamas'], ['Lesser Antilles', 'The Bahamas', 'United States East Coast', 'Atlantic Canada'], ['The Bahamas', 'Northeastern United States'], ['Lesser Antilles', 'Jamaica', 'Cayman Islands', 'Cuba', 'The Bahamas', 'Bermuda'], ['The Bahamas', 'Cuba', 'Florida', 'Texas', 'Tamaulipas'], ['Jamaica', 'Yucatn Peninsula'], ['The Bahamas', 'Florida', 'Georgia', 'The Carolinas', 'Virginia'], ['Southeastern United States', 'Northeastern United States', 'Southwestern Quebec'], ['Bermuda', 'New England', 'Atlantic Canada'], ['Lesser Antilles', 'Central America'], ['Texas', 'Louisiana', 'Midwestern United States'], ['Central America'], ['The Caribbean', 'Mexico', 'Texas'], ['Cuba', 'United States Gulf Coast'], ['The Caribbean', 'Central America', 'Mexico', 'United States Gulf Coast'], ['Mexico'], ['The Caribbean', 'United States East coast'], ['The Caribbean', 'Yucatn Peninsula', 'Mexico', 'South Texas'], ['Jamaica', 'Venezuela', 'Central America', 'Hispaniola', 'Mexico'], ['The Caribbean', 'United States East Coast'], ['The Bahamas', 'Florida', 'United States Gulf Coast'], ['Central America', 'Yucatn Peninsula', 'South Florida'], ['Greater Antilles', 'Bahamas', 'Eastern United States', 'Ontario'], ['The Caribbean', 'Venezuela', 'United States Gulf Coast'], ['Windward Islands', 'Jamaica', 'Mexico', 'Texas'], ['Bahamas', 'United States Gulf Coast'], ['Cuba', 'United States Gulf Coast'], ['Greater Antilles', 'Central America', 'Florida'], ['The Caribbean', 'Central America'], ['Nicaragua', 'Honduras'], ['Antilles', 'Venezuela', 'Colombia', 'United States East Coast', 'Atlantic Canada'], ['Cape Verde', 'The Caribbean', 'British Virgin Islands', 'U.S. Virgin Islands', 'Cuba', 'Florida'], ['Lesser Antilles', 'Virgin Islands', 'Puerto Rico', 'Dominican Republic', 'Turks and Caicos Islands'], ['Central America', 'United States Gulf Coast (especially Florida Panhandle)']]



def area_counter(areas_affected):
areas = {}
count = 0
splits = [item[-1].split(",") for item in areas_affected]
flat = [item for l in splits for item in l]
for i in range(len(flat)):
  count  = 1
  areas[flat[i]] = {"Count": [count]}
return areas
print(area_counter(areas_affected))

CodePudding user response:

Try using a defaultdict with a lambda that returns 0 for uninitialized items

from collections import defaultdict
areas = defaultdict(lambda: 0)

and whenever you use that area as a key just simply update it by one

areas[key]  = 1

Let me know if this helps or not

CodePudding user response:

You can use the defaultdict from the collections package.

If you prefer to do it manually, you can do it first by extracting all the keys and then counting the occurrences for each key.

keys = set()
for item in areas_affected:
    keys.update(item)

dic = {}
for k in keys:
    counter = 0
    for item in areas_affected:
        if k in item:
            counter  = 1
        
    dic[k] = counter
  • Related