You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何根据输入的起止年月计算字典中对应时间段的数值总和

Calculate Sum of Values in Date Range from Year-Month Dictionary

Problem Overview

You have a dictionary where each key represents a year (as a string), and the corresponding value is a list of 12 numerical values representing each month of that year (index 0 = January, index 1 = February, ..., index 11 = December). Your goal is to calculate the sum of all values within a user-specified date range, defined by a start year/month and end year/month.

Complete Solution Code

# Define the given dictionary of year-to-monthly values
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]
}

# Get and parse user input
print("Beginning year")
year1 = int(input())
print("Beginning month")
month1 = int(input())
print("Ending year")
year2 = int(input())
print("Ending month")
month2 = int(input())

total_sum = 0.0

# Iterate through each year in the dictionary
for year_str, monthly_values in d.items():
    current_year = int(year_str)
    
    # Check if current year falls within the specified range
    if year1 <= current_year <= year2:
        # Determine which months to sum for this year
        if current_year == year1 and current_year == year2:
            # Same start and end year: use exact month range
            start_idx = month1 - 1
            end_idx = month2 - 1
        elif current_year == year1:
            # Start year: sum from start month to December
            start_idx = month1 - 1
            end_idx = 11
        elif current_year == year2:
            # End year: sum from January to end month
            start_idx = 0
            end_idx = month2 - 1
        else:
            # Middle year: sum all 12 months
            start_idx = 0
            end_idx = 11
        
        # Add the sum of relevant months to total
        total_sum += sum(monthly_values[start_idx:end_idx+1])

# Output the final result
print(f"Total sum of values in the specified range: {total_sum}")

How This Works

Let’s walk through the key parts of the code:

  1. Input Parsing: We convert user input strings to integers to make numerical comparisons easier.
  2. Year Range Check: For each year in the dictionary, we first check if it’s within the user-specified start and end years.
  3. Month Range Logic:
    • If the start and end year are the same, we only sum values between the start and end months.
    • For the start year (but not the end year), we sum from the input start month through December.
    • For the end year (but not the start year), we sum from January through the input end month.
    • For any years between the start and end, we sum all 12 months.
  4. Summing Values: We use Python’s built-in sum() function on the slice of the monthly values list that matches our calculated indices, then add that to the total sum.

内容的提问来源于stack exchange,提问作者user15200538

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 15:17:37