[英]Python specific vaules average of dictionary
I have a dictionary:我有一本字典:
{201001: '-28.3', 201002: '-23.8', 201003: '-24.2', 201004: '-14.1',
201005: '-7.5', 201006: '0.1', 201007: '5.0', 201008: '5.6',
201009: '2.6', 201010: '-5.0', 201011: '-12.1', 201012: '-23.6',
201101: '-23.0', 201102: '-21.7', 201103: '-21.9', 201104: '-19.5',
201105: '-6.0', 201106: '0.7', 201107: '4.8', 201108: '5.2',
201109: '2.3', 201110: '-4.9', 201111: '-18.5', 201112: '-23.8'}
This dictionary represents the temperature of Alaska.这本字典代表阿拉斯加的温度。 201001: '-28.3'
represent temperature of Alaska in 2010 January is -28.3 celsius.) 201001: '-28.3'
表示 2010 年 1 月阿拉斯加的温度为 -28.3 摄氏度。)
I want to get the average temperature of winter year by year which is我想逐年获得冬季的平均温度,即
( January's + February's ) / 2
. ( January's + February's ) / 2
。
For example 2010's winter average temperature is (-28.3 + -23.8) / 2
.例如 2010 年的冬季平均温度为(-28.3 + -23.8) / 2
。
I think seeing and directly typing average year by year isn't elegant because there are more years 2000~2019.我认为逐年查看并直接输入平均值并不优雅,因为 2000~2019 年的年份更多。
(I typed only 2 years 2010,2011 for readability.) How to get average of specific data year by year in dictionary? (为了可读性,我只输入了 2 年 2010,2011。)如何在字典中逐年获取特定数据的平均值?
Just a simple loop should do it, no?只需一个简单的循环就可以了,不是吗?
from typing import Dict
def avg_winter_temps(temps: Dict[int, float]) -> Dict[int, float]:
winter_temps = {}
for year in range(2010, 2019+1):
jan, feb = year*100 + 1, year*100 + 2
winter_temps[year] = (temps[jan] + temps[feb])/2
return winter_temps
Temperature by Year年份温度
by_year = {}
for k, v in d.items():
# k // 100 is year
by_year.setdefault(k //100, []).append(float(v))
Average Temperature in Winter冬季平均气温
avg_winter = {}
for k, months in by_year.items():
# months[:2] to average first two months
avg_winter[k] = sum(months[:2])/len(months[:2])
Output Output
from pprint import pprint
pprint(by_year)
pprint(avg_winter)
by_year
{2010: [-28.3,
-23.8,
-24.2,
-14.1,
-7.5,
0.1,
5.0,
5.6,
2.6,
-5.0,
-12.1,
-23.6],
2011: [-23.0,
-21.7,
-21.9,
-19.5,
-6.0,
0.7,
4.8,
5.2,
2.3,
-4.9,
-18.5,
-23.8]}
avg_winter
{2010: -26.05, 2011: -22.35}
You can use regular expressions to find keys that end in "01" or "02":您可以使用正则表达式查找以“01”或“02”结尾的键:
import re
dictionary = {201001: '-28.3', 201002: '-23.8', 201003: '-24.2', 201004: '-14.1',
201005: '-7.5', 201006: '0.1', 201007: '5.0', 201008: '5.6',
201009: '2.6', 201010: '-5.0', 201011: '-12.1', 201012: '-23.6',
201101: '-23.0', 201102: '-21.7', 201103: '-21.9', 201104: '-19.5',
201105: '-6.0', 201106: '0.7', 201107: '4.8', 201108: '5.2',
201109: '2.3', 201110: '-4.9', 201111: '-18.5', 201112: '-23.8'}
key_list = dictionary.keys()
months = 0
sum = 0
averages = []
for key in key_list:
match = re.search("01$|02$", str(key)) # String ends in '01; or in '02'
if match:
if months == 0:
sum += float(dictionary.get(key))
months += 1
else:
sum += float(dictionary.get(key))
months = 0
averages.append(sum / 2)
sum = 0
print(averages)
Output: Output:
[-26.05, -22.35]
This is not intended as an answer to the question asked, but to supplement the existing answers if you want to take into account that to calculate a time average of temperature over the January-February period the monthly averages should be weighted by the lengths of the months concerned.这不是作为对所提问题的答案,而是为了补充现有答案,如果您想考虑计算 1 月至 2 月期间的时间平均值,则每月平均值应按时间长度加权月有关。 The function calc_jan_feb_average
can be used to calculate this weighted average. function calc_jan_feb_average
可用于计算此加权平均值。 It requires the year as an input parameter because of leap years.由于闰年,它需要年份作为输入参数。
import datetime
def get_length_feb(year):
return (datetime.datetime(year, 3, 1)
- datetime.datetime(year, 2, 1)).total_seconds()
def calc_jan_feb_average(year, jan_temp, feb_temp):
length_jan = 31 * 86400
length_feb = get_length_feb(year)
return (
((jan_temp * length_jan) + (feb_temp * length_feb)) /
(length_jan + length_feb)
)
Is this what you're looking for?这是你要找的吗?
temps = {201001: '-28.3', 201002: '-23.8', 201003: '-24.2', 201004: '-14.1',
201005: '-7.5', 201006: '0.1', 201007: '5.0', 201008: '5.6',
201009: '2.6', 201010: '-5.0', 201011: '-12.1', 201012: '-23.6',
201101: '-23.0', 201102: '-21.7', 201103: '-21.9', 201104: '-19.5',
201105: '-6.0', 201106: '0.7', 201107: '4.8', 201108: '5.2',
201109: '2.3', 201110: '-4.9', 201111: '-18.5', 201112: '-23.8'}
avgs = {}
jan = min(temps.keys()) // 100 * 100 + 1
limit = max(temps.keys())
while jan <= limit:
jt = temps.get(jan)
feb = jan + 1
ft = temps.get(feb)
if jt is not None and ft is not None:
avgs[f'{jan} - {feb}'] = (float(jt) + float(ft)) / 2
jan += 100
for period, avg in avgs.items():
print(f'{period} = {avg}')
Output: Output:
201001 - 201002 = -26.05
201101 - 201102 = -22.35
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.