如何在以小时为单位的给定时间范围内捕获数据?
How to capture data within a given time frame based on an hour basis?
我还在学习 Python,有时我的编码效率不是很高。我正在寻找一种更有效的方法来按“小时”对数据输入进行充分分组。这是为了衡量每小时的产量,而不是每小时的平均产量(是的,我知道这会简单得多。举个例子,如果我在 4:30-5:30 和 [=] 之间做了 32 次某事=28=]-6:30 我做了 58 次,当天就这样了,这两个时间范围不是每小时生产的平均值,而是保持静态,这正是我需要的。它必须适合的时间范围是(4:30-5:30, 5:30-6:30, 6:30-7:30, 7:30-8:30, 8:30-9:30, 9:30-10:30, 11:00-12:00, 12:00-1:00, 1:00-2:00, 2:00-3:00, 3:00-4:00, 4:00-5:00, 5:00-6:00, 6:00-7:00, 7:30-8:30, 8:30-9:30, 9:30-10:30, 10:30-11:30, 11:30-12:30, 12:30-1:30)
。我已经有用于将数据输入 excel 的 GUI。我有两列标记为 *"Time" and "Date"
、Time 是我操纵小时和分钟的地方。
a_list = [2,3,4,5,6,7,8,9,10,11]
b_list = [11,12,13,14,15,16,17,18,19]
c_list = [19,20,21,22,23,0]
date = dt.datetime.now()
#hm = float(date.strftime('%H.%M'))
#hour = float(date.strftime('%H'))
hour = float(5) #testing
hm = float(5.30) #testing
for i in a_list:
if hour in a_list:
if hour < 5:
hour = 5
a ="{:.2f}".format(hour)
print(f'Test 1:{a}')
break
if hour+.5 >= 5.5 and hour <10:
if hm >= hour +.3:
a ="{:.2f}".format(hour+1)
print(f'Test 2: {a}')
else:
a ="{:.2f}".format(hour)
print(f'Test 3: {a}')
if hour >= 10:
hour = 10
a ="{:.2f}".format(hour)
print(f'Test 4: {a}')
break
我还没有为另一个列表添加任何代码。我正在寻找比我在这里更好的想法。我考虑过日期范围,但可能 运行 会导致并发症。这是其中两列的参考图片:
使用 datetime 模块,当然可以避免繁琐的 if-else 沙拉。
要在这些 windows 中创建时间 windows 并注册 activity,让我们考虑以下内容:
import datetime
def calculate_slot(point_in_time, base, windows, length = datetime.timedelta(hours = 1)):
'''Calculates the slot index of a given time relative to a base time and a list of time windows with a certain length.'''
slot = -1
# check if the time is further in the future than the end of the day.
if (base + windows[-1] + length) <= point_in_time:
return slot
# increase the slot index, as long as the time is still bigger than the window time.
while (slot+1) < len(windows) and (base + windows[slot+1]) < point_in_time:
slot += 1
return slot
def register_activity(activity_log, base, windows, point_in_time):
'''Stores an entry in a slot depending on the point in time given.'''
# in which window does the given time fit?
slot = calculate_slot(point_in_time, base, windows)
# only add if slot is valid.
if -1 < slot:
activity_log[slot].append(point_in_time)
def main():
# when does the first time window start?
base = datetime.datetime(2021, 1, 6)
# what are the starting points of each window?
windows = [
datetime.timedelta(hours = 4, minutes = 30),
datetime.timedelta(hours = 5, minutes = 30),
datetime.timedelta(hours = 6, minutes = 30),
datetime.timedelta(hours = 7, minutes = 30),
datetime.timedelta(hours = 8, minutes = 30),
datetime.timedelta(hours = 9, minutes = 30),
datetime.timedelta(hours = 11, minutes = 00),
datetime.timedelta(hours = 12, minutes = 00),
datetime.timedelta(hours = 13, minutes = 00),
datetime.timedelta(hours = 14, minutes = 00),
datetime.timedelta(hours = 15, minutes = 00),
datetime.timedelta(hours = 16, minutes = 00),
datetime.timedelta(hours = 17, minutes = 00),
datetime.timedelta(hours = 18, minutes = 00),
datetime.timedelta(hours = 19, minutes = 30),
datetime.timedelta(hours = 20, minutes = 30),
datetime.timedelta(hours = 21, minutes = 30),
datetime.timedelta(hours = 22, minutes = 30),
datetime.timedelta(hours = 23, minutes = 30),
datetime.timedelta(hours = 24, minutes = 30),
]
# prepare the activity log with enought slots.
activity_log = []
for _ in range(0, len(windows)):
activity_log.append([])
# point in time to test against.
search_time0 = datetime.datetime(2021, 1, 6, 8, 15)
search_time1 = datetime.datetime(2021, 1, 6, 23, 42)
search_time2 = datetime.datetime(2021, 1, 7, 1, 11)
# register activity on specific times.
register_activity(activity_log, base, windows, search_time0)
register_activity(activity_log, base, windows, search_time1)
register_activity(activity_log, base, windows, search_time2)
# show activity log.
print(activity_log)
if __name__ == '__main__':
main()
概念是,定义一个基准时间(考虑日期)并为每个 window 创建一个开始时间列表。使用 timedelta a window 由相对于开始时间的时间距离定义。
为了弄清楚某个时间的activity属于哪个window / slot,从早到晚检查windows。
对于activity日志,使用列表的列表(内部列表的数量等于windows/槽的数量)。要注册一个activity,计算给定时间的槽,如果找到槽,则将时间插入到内部列表中。
希望这能成为您申请的灵感来源。
我还在学习 Python,有时我的编码效率不是很高。我正在寻找一种更有效的方法来按“小时”对数据输入进行充分分组。这是为了衡量每小时的产量,而不是每小时的平均产量(是的,我知道这会简单得多。举个例子,如果我在 4:30-5:30 和 [=] 之间做了 32 次某事=28=]-6:30 我做了 58 次,当天就这样了,这两个时间范围不是每小时生产的平均值,而是保持静态,这正是我需要的。它必须适合的时间范围是(4:30-5:30, 5:30-6:30, 6:30-7:30, 7:30-8:30, 8:30-9:30, 9:30-10:30, 11:00-12:00, 12:00-1:00, 1:00-2:00, 2:00-3:00, 3:00-4:00, 4:00-5:00, 5:00-6:00, 6:00-7:00, 7:30-8:30, 8:30-9:30, 9:30-10:30, 10:30-11:30, 11:30-12:30, 12:30-1:30)
。我已经有用于将数据输入 excel 的 GUI。我有两列标记为 *"Time" and "Date"
、Time 是我操纵小时和分钟的地方。
a_list = [2,3,4,5,6,7,8,9,10,11]
b_list = [11,12,13,14,15,16,17,18,19]
c_list = [19,20,21,22,23,0]
date = dt.datetime.now()
#hm = float(date.strftime('%H.%M'))
#hour = float(date.strftime('%H'))
hour = float(5) #testing
hm = float(5.30) #testing
for i in a_list:
if hour in a_list:
if hour < 5:
hour = 5
a ="{:.2f}".format(hour)
print(f'Test 1:{a}')
break
if hour+.5 >= 5.5 and hour <10:
if hm >= hour +.3:
a ="{:.2f}".format(hour+1)
print(f'Test 2: {a}')
else:
a ="{:.2f}".format(hour)
print(f'Test 3: {a}')
if hour >= 10:
hour = 10
a ="{:.2f}".format(hour)
print(f'Test 4: {a}')
break
我还没有为另一个列表添加任何代码。我正在寻找比我在这里更好的想法。我考虑过日期范围,但可能 运行 会导致并发症。这是其中两列的参考图片:
使用 datetime 模块,当然可以避免繁琐的 if-else 沙拉。 要在这些 windows 中创建时间 windows 并注册 activity,让我们考虑以下内容:
import datetime
def calculate_slot(point_in_time, base, windows, length = datetime.timedelta(hours = 1)):
'''Calculates the slot index of a given time relative to a base time and a list of time windows with a certain length.'''
slot = -1
# check if the time is further in the future than the end of the day.
if (base + windows[-1] + length) <= point_in_time:
return slot
# increase the slot index, as long as the time is still bigger than the window time.
while (slot+1) < len(windows) and (base + windows[slot+1]) < point_in_time:
slot += 1
return slot
def register_activity(activity_log, base, windows, point_in_time):
'''Stores an entry in a slot depending on the point in time given.'''
# in which window does the given time fit?
slot = calculate_slot(point_in_time, base, windows)
# only add if slot is valid.
if -1 < slot:
activity_log[slot].append(point_in_time)
def main():
# when does the first time window start?
base = datetime.datetime(2021, 1, 6)
# what are the starting points of each window?
windows = [
datetime.timedelta(hours = 4, minutes = 30),
datetime.timedelta(hours = 5, minutes = 30),
datetime.timedelta(hours = 6, minutes = 30),
datetime.timedelta(hours = 7, minutes = 30),
datetime.timedelta(hours = 8, minutes = 30),
datetime.timedelta(hours = 9, minutes = 30),
datetime.timedelta(hours = 11, minutes = 00),
datetime.timedelta(hours = 12, minutes = 00),
datetime.timedelta(hours = 13, minutes = 00),
datetime.timedelta(hours = 14, minutes = 00),
datetime.timedelta(hours = 15, minutes = 00),
datetime.timedelta(hours = 16, minutes = 00),
datetime.timedelta(hours = 17, minutes = 00),
datetime.timedelta(hours = 18, minutes = 00),
datetime.timedelta(hours = 19, minutes = 30),
datetime.timedelta(hours = 20, minutes = 30),
datetime.timedelta(hours = 21, minutes = 30),
datetime.timedelta(hours = 22, minutes = 30),
datetime.timedelta(hours = 23, minutes = 30),
datetime.timedelta(hours = 24, minutes = 30),
]
# prepare the activity log with enought slots.
activity_log = []
for _ in range(0, len(windows)):
activity_log.append([])
# point in time to test against.
search_time0 = datetime.datetime(2021, 1, 6, 8, 15)
search_time1 = datetime.datetime(2021, 1, 6, 23, 42)
search_time2 = datetime.datetime(2021, 1, 7, 1, 11)
# register activity on specific times.
register_activity(activity_log, base, windows, search_time0)
register_activity(activity_log, base, windows, search_time1)
register_activity(activity_log, base, windows, search_time2)
# show activity log.
print(activity_log)
if __name__ == '__main__':
main()
概念是,定义一个基准时间(考虑日期)并为每个 window 创建一个开始时间列表。使用 timedelta a window 由相对于开始时间的时间距离定义。
为了弄清楚某个时间的activity属于哪个window / slot,从早到晚检查windows。
对于activity日志,使用列表的列表(内部列表的数量等于windows/槽的数量)。要注册一个activity,计算给定时间的槽,如果找到槽,则将时间插入到内部列表中。
希望这能成为您申请的灵感来源。