Home > Software engineering >  Sum certain values of a list inside a dictionary
Sum certain values of a list inside a dictionary

Time:09-30

I have this dictionary with a list of values on it:

d = { 
    '1751': [70.0, 43.5, 45.3, 56.4, 60.7, 50.7, 66.3, 59.8, 23.5, 23.2, 28.5, 44.0], 
    '1752': [35.0, 50.0, 71.0, 59.3, 59.7, 39.6, 78.4, 29.3, 27.1, 46.6, 37.6, 40.0], 
    '1753': [44.0, 32.0, 45.7, 38.0, 36.0, 31.7, 22.0, 39.0, 28.0, 25.0, 20.0, 6.7], 
    '1754': [0.0, 3.0, 1.7, 13.7, 20.7, 26.7, 18.8, 12.3, 8.2, 24.1, 13.2, 4.2], 
    '1755': [10.2, 11.2, 6.8, 6.5, 0.0, 0.0, 8.6, 3.2, 17.8, 23.7, 6.8, 20.0], 
    '1756': [12.5, 7.1, 5.4, 9.4, 12.5, 12.9, 3.6, 6.4, 11.8, 14.3, 17.0, 9.4], 
    '1757': [14.1, 21.2, 26.2, 30.0, 38.1, 12.8, 25.0, 51.3, 39.7, 32.5, 64.7, 33.5], 
    '1758': [37.6, 52.0, 49.0, 72.3, 46.4, 45.0, 44.0, 38.7, 62.5, 37.7, 43.0, 43.0], 
    '1759': [48.3, 44.0, 46.8, 47.0, 49.0, 50.0, 51.0, 71.3, 77.2, 59.7, 46.3, 57.0]
     }

Every key-value represents a year and each year has a list of twelve values , that represents each month of the year (the first value is the value for January, the second one is the value for February, the third one is the value for March,..).

The user is going to be ask to insert a beginning year a beginning month and a ending year and a ending month:

print("Begining year")
year1 = input()
print("Begining month")
month1 = input()


print("Ending year")
year2 = input()
print("Ending month")
month2 = input()

I wish to get users' input and go through the dictionary in order to sum all the values listed between the period of time determined by the user and print the total value.

CodePudding user response:

You can try this:

d = { 
    '1751': [70.0, 43.5, 45.3, 56.4, 60.7, 50.7, 66.3, 59.8, 23.5, 23.2, 28.5, 44.0], 
    '1752': [35.0, 50.0, 71.0, 59.3, 59.7, 39.6, 78.4, 29.3, 27.1, 46.6, 37.6, 40.0], 
    '1753': [44.0, 32.0, 45.7, 38.0, 36.0, 31.7, 22.0, 39.0, 28.0, 25.0, 20.0, 6.7], 
    '1754': [0.0, 3.0, 1.7, 13.7, 20.7, 26.7, 18.8, 12.3, 8.2, 24.1, 13.2, 4.2], 
    '1755': [10.2, 11.2, 6.8, 6.5, 0.0, 0.0, 8.6, 3.2, 17.8, 23.7, 6.8, 20.0], 
    '1756': [12.5, 7.1, 5.4, 9.4, 12.5, 12.9, 3.6, 6.4, 11.8, 14.3, 17.0, 9.4], 
    '1757': [14.1, 21.2, 26.2, 30.0, 38.1, 12.8, 25.0, 51.3, 39.7, 32.5, 64.7, 33.5], 
    '1758': [37.6, 52.0, 49.0, 72.3, 46.4, 45.0, 44.0, 38.7, 62.5, 37.7, 43.0, 43.0], 
    '1759': [48.3, 44.0, 46.8, 47.0, 49.0, 50.0, 51.0, 71.3, 77.2, 59.7, 46.3, 57.0]
}

year1 = int(input("Beginning Year: "))
month1 = int(input("Beginning Month: "))


year2 = int(input("Ending Year: "))
month2 = int(input("Ending Month: "))

total_sum = 0
for year in range(year1, year2   1):
    list_sum = 0
    if year == year1:
        list_sum = sum(d[str(year)][month1 -1:])
    elif year == year2:
        list_sum = sum(d[str(year)][:month2   1])
    else:
        list_sum = sum(d[str(year)])

    total_sum  = list_sum

print(total_sum)

Input and Output:

Beginning Year: 1751
Beginning Month: 1
Ending Year: 1759
Ending Month: 12
3505.1999999999994

CodePudding user response:

No attempt here to handle your user input but here's how you could sum the values in the manner you've described:-

d = {
    '1751': [70.0, 43.5, 45.3, 56.4, 60.7, 50.7, 66.3, 59.8, 23.5, 23.2, 28.5, 44.0],
    '1752': [35.0, 50.0, 71.0, 59.3, 59.7, 39.6, 78.4, 29.3, 27.1, 46.6, 37.6, 40.0],
    '1753': [44.0, 32.0, 45.7, 38.0, 36.0, 31.7, 22.0, 39.0, 28.0, 25.0, 20.0, 6.7],
    '1754': [0.0, 3.0, 1.7, 13.7, 20.7, 26.7, 18.8, 12.3, 8.2, 24.1, 13.2, 4.2],
    '1755': [10.2, 11.2, 6.8, 6.5, 0.0, 0.0, 8.6, 3.2, 17.8, 23.7, 6.8, 20.0],
    '1756': [12.5, 7.1, 5.4, 9.4, 12.5, 12.9, 3.6, 6.4, 11.8, 14.3, 17.0, 9.4],
    '1757': [14.1, 21.2, 26.2, 30.0, 38.1, 12.8, 25.0, 51.3, 39.7, 32.5, 64.7, 33.5],
    '1758': [37.6, 52.0, 49.0, 72.3, 46.4, 45.0, 44.0, 38.7, 62.5, 37.7, 43.0, 43.0],
    '1759': [48.3, 44.0, 46.8, 47.0, 49.0, 50.0, 51.0, 71.3, 77.2, 59.7, 46.3, 57.0]
}

# sy -> start year, ey -> end year, sm -> start month, em -> end month
# month values are base 0 - i.e., 0-11
# specified years and months are inclusive


def doSum(sy, ey, sm, em):
    # sort the keys as the dictionary may not be sorted
    ly = sorted(list(d.keys()))
    s = 0
    try:
        for y in ly[ly.index(sy):]:
            if y > ey:
                break
            s  = sum(d[y][sm:em 1])
    except Exception:
        pass
    return s


x = doSum('1752', '1754', 10, 11)
print(f'{x:.1f}')

CodePudding user response:

A one-liner could be like this

total = sum(d[year1][int(month1)-1:] [sum(months) for months in [d[str(year)] for year in range(int(year1)   1, int(year2))]] d[year2][:int(month2)-1])

ofcourse, you have to sanitize your inputs for this.

CodePudding user response:

This might work as a starter. However, you should check if the inputs are legitimate. Also, try using clearer variable names like 'start_year' and 'end_year' instead of the opaque 'year1' and 'year2'.

    import numpy as np
    
    d = { 
        '1751': [70.0, 43.5, 45.3, 56.4, 60.7, 50.7, 66.3, 59.8, 23.5, 23.2, 28.5, 44.0], 
        '1752': [35.0, 50.0, 71.0, 59.3, 59.7, 39.6, 78.4, 29.3, 27.1, 46.6, 37.6, 40.0], 
        '1753': [44.0, 32.0, 45.7, 38.0, 36.0, 31.7, 22.0, 39.0, 28.0, 25.0, 20.0, 6.7], 
        '1754': [0.0, 3.0, 1.7, 13.7, 20.7, 26.7, 18.8, 12.3, 8.2, 24.1, 13.2, 4.2], 
        '1755': [10.2, 11.2, 6.8, 6.5, 0.0, 0.0, 8.6, 3.2, 17.8, 23.7, 6.8, 20.0], 
        '1756': [12.5, 7.1, 5.4, 9.4, 12.5, 12.9, 3.6, 6.4, 11.8, 14.3, 17.0, 9.4], 
        '1757': [14.1, 21.2, 26.2, 30.0, 38.1, 12.8, 25.0, 51.3, 39.7, 32.5, 64.7, 33.5], 
        '1758': [37.6, 52.0, 49.0, 72.3, 46.4, 45.0, 44.0, 38.7, 62.5, 37.7, 43.0, 43.0], 
        '1759': [48.3, 44.0, 46.8, 47.0, 49.0, 50.0, 51.0, 71.3, 77.2, 59.7, 46.3, 57.0]
         }
    
    print("Enter beginning year")
    year1 = input()
    print("Enter beginning month")
    month1 = input()
    
    
    print("Enter ending year")
    year2 = input()
    print("Enter ending month")
    month2 = input()
    
    d[year1] = d[year1][int(month1)::]
    d[year2] = d[year2][::int(month1)]
    l = np.arange(int(year1), int(year2))
    l = [str(i) for i in l]
    dtemp = {k: sum(d[k]) for k in l}
    sum_of_values = sum(dtemp.values())
    print(sum_of_values)

CodePudding user response:

d = { 
    '1751': [70.0, 43.5, 45.3, 56.4, 60.7, 50.7, 66.3, 59.8, 23.5, 23.2, 28.5, 44.0], 
    '1752': [35.0, 50.0, 71.0, 59.3, 59.7, 39.6, 78.4, 29.3, 27.1, 46.6, 37.6, 40.0], 
    '1753': [44.0, 32.0, 45.7, 38.0, 36.0, 31.7, 22.0, 39.0, 28.0, 25.0, 20.0, 6.7], 
    '1754': [0.0, 3.0, 1.7, 13.7, 20.7, 26.7, 18.8, 12.3, 8.2, 24.1, 13.2, 4.2], 
    '1755': [10.2, 11.2, 6.8, 6.5, 0.0, 0.0, 8.6, 3.2, 17.8, 23.7, 6.8, 20.0], 
    '1756': [12.5, 7.1, 5.4, 9.4, 12.5, 12.9, 3.6, 6.4, 11.8, 14.3, 17.0, 9.4], 
    '1757': [14.1, 21.2, 26.2, 30.0, 38.1, 12.8, 25.0, 51.3, 39.7, 32.5, 64.7, 33.5], 
    '1758': [37.6, 52.0, 49.0, 72.3, 46.4, 45.0, 44.0, 38.7, 62.5, 37.7, 43.0, 43.0], 
    '1759': [48.3, 44.0, 46.8, 47.0, 49.0, 50.0, 51.0, 71.3, 77.2, 59.7, 46.3, 57.0]
     }
while 1:
    year1 = int(input("Please enter a Begining year (1751 to 1759): "))
    if str(year1) not in d.keys():
        print("Sorry, your response was not loud enough.")
    else:
        #we're happy with the value given.
        #we're ready to exit the loop.
        break
while 1:
    year2 = int(input("Please enter a Ending year(" str(year1) " to 1759): "))
    if str(year2) not in d.keys():
        print("Sorry, your response was not loud enough.")
    else:
        break
while 1:
    month1 = int(input("Please enter a Begining month (1 to 12): "))
    if month1 not in range(1,13):
        print("Sorry, your response was not loud enough.")
    else:
        break
while 1:
    month2 = int(input("Please enter a Ending month (1 to 12): "))
    if month2 not in range(1,13):
        print("Sorry, your response was not loud enough.")
    else:
        break

data_list=[]
for year in range(int(year1),int(year2) 1):
    if year==year1:
        start_month =month1
        end_month=12
    elif year==year2:
        start_month=1
        end_month=month2
    else:
        start_month=1
        end_month=12
    for i in range(start_month-1,end_month):
        data_list.append(d[str(year)][i])
print("Sum :",sum(data_list))

** answer is **

Please enter a Begining year (1751 to 1759): 1752

Please enter a Ending year(1752 to 1759): 1753

Please enter a Begining month (1 to 12): 1

Please enter a Ending month (1 to 12): 3

Sum : 695.3000000000001

CodePudding user response:

Dict of {str: list} might not be the best data structure for your task as you need to convert the years to ints to compare them, and there are a lot of errors you might encounter. Otherwise, you can use something like this

from itertools import chain
flat = list(chain.from_iterable(d[str(year)] for year in range(year1, year2 1)))
#assuming month index starts from 1, not 0
result = sum(flat[month1 -1 : -12   month2 or None])

Personally I'd use a numpy array or pandas dataframe in place of the dict.

  • Related