首页 > 解决方案 > 如何计算字典中出现的连续小时数

问题描述

我对 Python 还很陌生,我正在尝试使用天气数据来计算白粉病在农作物中发生的可能性。

1.白粉病的第一个指标是计算温度在 70F 和 85F 之间的日期范围内的天数。

我能够从 weather.json 构建一个字典,它只有 70F 和 85F 的值,但现在我需要弄清楚白粉病计算的第二部分

import json
import operator
import time
import datetime

min_hourly_temperature = 70
max_hourly_temperature = 85
hours_in_range = {}


for temperature, value in hourly_temperature_hora.items():
    if min_hourly_temperature <= temperature <= max_hourly_temperature:
        hours_in_range[temperature] = value

sorted_temp_in_range = sorted(hours_in_range.items(), key=operator.itemgetter(1))

date_hour_temp = dict(sorted_temp_in_range)

这是排序的温度字典以及温度发生的日期和时间。

date_hour_temp = {84.5: '2021-06-02 22:00:00', 72.9: '2021-06-05 01:00:00', 76.9: '2021-06-08 15:00:00', 80.4: '2021-06-09 18:00:00', 71.7: '2021-06-12 10:00:00', 82.8: '2021-06-12 17:00:00', 79.9: '2021-06-13 22:00:00', 75.9: '2021-06-14 02:00:00', 73.5: '2021-06-15 10:00:00', 75.4: '2021-06-15 11:00:00', 82.7: '2021-06-16 19:00:00', 76.5: '2021-06-17 11:00:00', 76.7: '2021-06-18 10:00:00', 76.2: '2021-06-19 01:00:00', 77.3: '2021-06-19 03:00:00', 74.8: '2021-06-19 04:00:00', 81.1: '2021-06-19 09:00:00', 82.4: '2021-06-19 23:00:00', 84.9: '2021-06-22 02:00:00', 76.8: '2021-06-22 08:00:00', 80.5: '2021-06-23 12:00:00', 75.2: '2021-06-25 03:00:00', 77.9: '2021-06-25 08:00:00', 71.2: '2021-06-27 03:00:00', 79.0: '2021-06-27 09:00:00', 79.6: '2021-06-30 02:00:00', 75.7: '2021-06-30 06:00:00', 82.6: '2021-06-30 08:00:00', 77.8: '2021-07-01 02:00:00', 78.1: '2021-07-01 04:00:00', 72.6: '2021-07-01 05:00:00', 83.3: '2021-07-01 10:00:00', 81.2: '2021-07-02 01:00:00', 77.5: '2021-07-02 02:00:00', 75.0: '2021-07-02 03:00:00', 70.9: '2021-07-02 04:00:00', 83.0: '2021-07-02 10:00:00', 72.4: '2021-07-03 07:00:00', 84.6: '2021-07-03 10:00:00', 73.0: '2021-07-04 06:00:00', 71.9: '2021-07-04 07:00:00', 84.1: '2021-07-05 02:00:00', 72.1: '2021-07-05 05:00:00', 70.0: '2021-07-05 07:00:00', 72.0: '2021-07-05 08:00:00', 77.1: '2021-07-05 09:00:00', 77.6: '2021-07-06 02:00:00', 71.3: '2021-07-06 06:00:00', 75.6: '2021-07-07 04:00:00', 83.6: '2021-07-07 23:00:00', 81.7: '2021-07-08 00:00:00', 79.1: '2021-07-08 02:00:00', 76.3: '2021-07-08 03:00:00', 74.1: '2021-07-08 04:00:00', 74.6: '2021-07-08 07:00:00', 80.7: '2021-07-08 22:00:00', 82.2: '2021-07-09 00:00:00', 81.0: '2021-07-09 01:00:00', 78.5: '2021-07-09 02:00:00', 74.0: '2021-07-09 06:00:00', 71.4: '2021-07-09 08:00:00', 74.9: '2021-07-09 09:00:00', 70.3: '2021-07-10 09:00:00', 75.5: '2021-07-10 10:00:00', 83.7: '2021-07-11 01:00:00', 84.8: '2021-07-11 02:00:00', 79.3: '2021-07-11 06:00:00', 82.0: '2021-07-11 09:00:00', 84.7: '2021-07-12 00:00:00', 78.4: '2021-07-12 02:00:00', 71.8: '2021-07-12 04:00:00', 74.2: '2021-07-12 05:00:00', 74.4: '2021-07-12 06:00:00', 76.1: '2021-07-12 09:00:00', 82.5: '2021-07-12 10:00:00', 84.0: '2021-07-13 02:00:00', 79.5: '2021-07-13 06:00:00', 78.8: '2021-07-13 07:00:00', 81.4: '2021-07-13 09:00:00', 83.4: '2021-07-13 10:00:00', 78.0: '2021-07-14 00:00:00', 73.4: '2021-07-14 03:00:00', 78.6: '2021-07-14 10:00:00', 83.9: '2021-07-14 11:00:00', 82.1: '2021-07-14 22:00:00', 78.9: '2021-07-14 23:00:00', 73.8: '2021-07-15 00:00:00', 72.5: '2021-07-15 03:00:00', 70.1: '2021-07-15 07:00:00', 80.8: '2021-07-15 10:00:00', 83.5: '2021-07-16 00:00:00', 79.2: '2021-07-16 03:00:00', 77.0: '2021-07-16 04:00:00', 75.8: '2021-07-16 05:00:00', 76.6: '2021-07-16 09:00:00', 81.8: '2021-07-16 10:00:00', 82.3: '2021-07-16 22:00:00', 81.3: '2021-07-17 00:00:00', 77.4: '2021-07-17 02:00:00', 75.1: '2021-07-17 03:00:00', 74.3: '2021-07-17 04:00:00', 70.4: '2021-07-17 06:00:00', 70.2: '2021-07-17 07:00:00', 72.3: '2021-07-17 08:00:00', 74.5: '2021-07-17 09:00:00', 79.4: '2021-07-17 10:00:00', 81.6: '2021-07-17 11:00:00', 80.3: '2021-07-17 22:00:00', 78.2: '2021-07-17 23:00:00', 71.1: '2021-07-18 00:00:00', 70.5: '2021-07-18 01:00:00', 70.6: '2021-07-18 02:00:00', 70.7: '2021-07-18 03:00:00', 73.1: '2021-07-18 05:00:00', 72.2: '2021-07-18 06:00:00', 73.3: '2021-07-18 09:00:00', 80.1: '2021-07-18 10:00:00', 84.3: '2021-07-18 11:00:00', 83.2: '2021-07-18 22:00:00', 79.7: '2021-07-18 23:00:00', 73.7: '2021-07-19 01:00:00', 80.9: '2021-07-19 02:00:00', 75.3: '2021-07-19 03:00:00', 73.9: '2021-07-19 04:00:00', 72.8: '2021-07-19 05:00:00', 72.7: '2021-07-19 06:00:00', 71.6: '2021-07-19 07:00:00', 74.7: '2021-07-19 08:00:00', 83.8: '2021-07-19 22:00:00', 79.8: '2021-07-20 00:00:00', 78.7: '2021-07-20 01:00:00', 76.4: '2021-07-20 02:00:00', 77.7: '2021-07-20 03:00:00', 73.6: '2021-07-20 05:00:00', 70.8: '2021-07-20 06:00:00', 71.0: '2021-07-20 08:00:00', 73.2: '2021-07-20 09:00:00', 77.2: '2021-07-20 10:00:00', 80.2: '2021-07-20 11:00:00', 82.9: '2021-07-20 12:00:00', 85.0: '2021-07-20 21:00:00', 81.9: '2021-07-20 22:00:00', 80.0: '2021-07-20 23:00:00'}

这是给我带来麻烦的部分。

  1. 计算的第二部分是计算这个温度在连续六个小时内出现了多少次。

对于一天中连续六个小时中的每一个小时,模型为 powdery_mildew_calculator 分配 20 分。

所以我试图遍历我的字典并计算一天中连续出现 6 个小时的次数。如果字典中没有出现 print("字典中没有出现连续 6 小时")

# Define Powdery Mildew Calculator
powdery_mildew_calculator = 0
powdery_mildew_positive = 20
powdery_mildew_negative = -10
powdery_mildew_calculator_max = 100
powdery_mildew_calculator_min = 0

get_start_date = list(date_hour_temp.values())[0]
get_end_date = list(date_hour_temp.values())[-1]

date_format = '%Y-%m-%d %H:%M:%S'
start = datetime.datetime.strptime(get_start_date, date_format)
end = datetime.datetime.strptime(get_end_date, date_format)

pm_hourly_calculator = 0
while start <= end:
    for i in date_hour_temp.values():
       if #THIS IS WHERE I'M STUCK IF HOURS CONSECUTIVE THEN pm_hourly_calculator +=1

我一直在阅读 DateTime python 文档https://docs.python.org/3/library/datetime.html并尝试了几个循环,但没有得到我想要的结果。

我想从输出中得到的是,对于一天中连续六个小时的每次出现,计算器给出 20 分。

标签: pythonloopsdatedatetimecount

解决方案


您可以使用defaultdict按日期对日期时间进行分组,然后使用日期时间范围查找连续的小时数

from collections import defaultdict
from datetime import datetime, timedelta

data = defaultdict(list)
for date in date_hour_temp.values():
  data[date.split()[0]].append(date)

for date_lst in data.values():
  if len(date_lst) > 5:
    for date in date_lst:
      start_date = datetime.strptime(date, '%Y-%m-%d %H:%M:%S')
      date_set = set((start_date + timedelta(hours=i)).strftime('%Y-%m-%d %H:%M:%S') for i in range(1, 7))
      if len(date_set.intersection(date_lst)) >= 6:
        print(date_lst)
        # do something
        break

输出:

['2021-07-19 01:00:00', '2021-07-19 02:00:00', '2021-07-19 03:00:00', '2021-07-19 04:00:00', '2021-07-19 05:00:00', '2021-07-19 06:00:00', '2021-07-19 07:00:00', '2021-07-19 08:00:00', '2021-07-19 22:00:00']


推荐阅读