简体   繁体   中英

How to sum of dict values into list of tuples?

Consider the following function:

I need to determine the sum for each year in the range of years taken as input.

So for example, sumarry_statistics(data, 2015, 2020)

I need to find the sum of col[9] for each tuple in a list of tuples that has [0] in that range of years.

So I have this much, which works to create my first list of tuples that counts each year, and how many time that year occurred:

def summary_statistics(data, year_start, year_end):
    earthquake_count_by_year = []
    total_damages_by_year = []
    casualties_by_year = []
    year_count = {}
    dmg_sum = {}
    
    years = []
    year_start = int(year_start)
    year_end = int(year_end)
    
    if year_end >= year_start:
        
        #store year range into list
        years = list(range(year_start, year_end+1))
        
        for index, tuple in enumerate(data):
            #values to be summed for each year in dmg_sum
            yr = tuple[0]
            dmgs = tuple[9]
            
            #if year in range of years (year_start - year_end)
            if tuple[0] in years:
                #if year does not exist in dict, set it to 0
                if tuple[0] not in year_count:
                    year_count[tuple[0]] = 0
                #count number of occurences for each year
                year_count[tuple[0]] += 1
       earthquake_count_by_year = list(year_count.items())

Output of print(earthquake_count_by_year):

[(2015, 48), (2016, 52), (2017, 64), (2018, 65), (2019, 60), (2020, 24)]

I need to write something similar, but instead of counting the number of occurrences, i need to find the sum of of col[9] for each year taken as input.

I don't know what to do after this:

for index, tuple in enumerate(data):
    #values to be summed for each year in dmg_sum
    yr = tuple[0]
    dmgs = tuple[9]
    
    #if year in range of years (year_start - year_end)
    if tuple[0] in years:
        #if year does not exist in dict, set it to 0
        if tuple[0] not in year_count:
            year_count[tuple[0]] = 0
        #count number of occurences for each year
        year_count[tuple[0]] += 1
        
        if tuple[0] not in dmg_sum:
            dmg_sum[tuple[0]] += sum(dmg_sum.values())

that last line is especially problematic for me. It does not work. I don't think I was even very clearly able to describe what I need to do here. tuple[0] is a csv column with a list of years. tuple[9] is a list of integers representing number of deaths. I need to find number of deaths for each year taken as input and store in same fashion, list of tuples like:

[(year, sum of [9]), (year2, sum of [9]), etc. 

you can do

if tuple[0] not in dmg_sum:
    dmg_sum[tuple[0]] = dmgs
else:
    dmg_sum[tuple[0]] += dmgs

EDIT: Adding a little bit more information regarding the answer and the context You can't add a value to a variable that is not yet assigned, in:

if tuple[0] not in dmg_sum:
        dmg_sum[tuple[0]] += dmgs

And you iterate over data which contains a specific year tuple[0] and a number representing number of deaths tuple[9] so for each iteration of the for loop, if your conditions are respected, if the key representing the dictionary where you want to assign the data doesn't exist you add it and assign the corresponding data, ie dmgs , else you add that data to it

Be careful with the variable name you take to iterate over data in:

for index, tuple in enumerate(data):

tuple is a python builtin for the tuple object, so when you create that variable with that name you override the tuple python builtin with the same name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM