0

假设我有一个 start_date

start_date = datetime.time(7, 55, 56)

我创建了一个时间表:

timelist = []
for y in range(0, 24, 1):
    for x in range(0, 60, 5):
        start = datetime.time(y, x)
        timelist.append(start)

timelist = [datetime.time(0, 0), 
            datetime.time(0, 10), 
            datetime.time(0, 20), 
            datetime.time(0, 30), 
            datetime.time(0, 40), 
            datetime.time(0, 50), 
            datetime.time(1, 0),
            ...]

x.hour退货[0, 1, 2, ..., 23] x.minute退货[0, 10, 20, 30, 40, 50]

然后我想创建一个字典{ [time: 00:00:00, value: 0], [time: 00:10:00, value=0], ... }

现在时间是 x。并且该值是通过检查开始时间何时创建的:

例如:

start_date = 07:55:56 → at 07:50:00 value = 1
start_date = 08:03:22 → at 08:00:00 value = 1
start_date = 08:18:00 → at 08:10:00 value = 1

否则值为 0

我需要帮助创建用于检查分钟的 if 语句。

到目前为止,我有:

for x in timelist:
    if x.hour == start_date.hour:
        ...
4

1 回答 1

1

有点难以理解在问什么。

您想知道 start_date 属于哪个时间间隔吗?

如果是这样:

timelist = []
for y in range(0, 24, 1):
    for x in range(0, 60, 10):     
        # should be ten? 
        start = datetime.time(y, x)
        timelist.append(start)


timedict = {}
for start in timelist:
    timedict[start] = 0
# is it the dictionary looking for?

start_date = datetime.time(7, 55, 56)

for i in range(len(timelist)):
    if timelist[i] <= start_date:
        if i == len(timelist)-1 or start_date < timelist[i+1]
            timedict[timelist[i]] += 1  # or = 1, depend on your usage
于 2013-03-17T16:58:28.467 回答