无限倒计时生成新的子列表以附加到 python 中的主列表



我的问题是如何创建一个pythonglobalList,并在无限while True循环中创建一个无限倒计时(例如5秒),在此期间它将创建一个subList,该将在整个5秒的时间段内存储一些数据(例如,此处current time),之后附加subListglobalList重新启动倒计时,new_subList存储其他数据,直到breakwhile True循环...

也就是说,类似于:

globalList = [
[[1], [2], [3], [4], [5], [6]],  # "subList_1" created and appended to "globalList" in the first 5 seconds
[[7], [8], [9], [10], [11], [12]],  # "subList_2" created and appended to "globalList" in the next 5 seconds
[[13], [14], [15], [16], [17], [18]],  # "subList_3" created and appended to "globalList" in the next 5 seconds
[[19], [20], [21], [22], [23], [24]],  # "subList_4" created and appended to "globalList" in the next 5 seconds
[[25], [26], [27], [28], [29], [30]],  # "subList_5" created and appended to "globalList" in the next 5 seconds
[[31], [32], [33], [34], [35], [36]],  # "subList_6" created and appended to "globalList" in the next 5 seconds
# ... and so on, until the end of a "while True" loop with that "countdown" loop with a duration of 5 seconds (for example) that restarts after the end of first 5 seconds...
]

这是我尝试过的:

from time import time

globalList = []

while True:
t_end = time() + 5  # 5 seconds
counter = 1
globals()['subList_{}'.format(counter)] = []
while time() < t_end:

globals()['timeList_{}'.format(counter)] = []
globals()['timeList_{}'.format(counter)].append(time())
globals()['subList_{}'.format(counter)].append(globals()['timeList_{}'.format(counter)])
globalList.append(globals()['subList_{}'.format(counter)])
print(globalList)
t_end = time() + 5  # restart loop

counter += 1

但是这段代码给出了如下输出:

[
[[1643349543.012217]]
]

[
[[1643349543.012217], [1643349543.0122268]],
[[1643349543.012217], [1643349543.0122268]]
]

[
[[1643349543.012217], [1643349543.0122268], [1643349543.012234]],
[[1643349543.012217], [1643349543.0122268], [1643349543.012234]],
[[1643349543.012217], [1643349543.0122268], [1643349543.012234]]
]

[
[[1643349543.012217], [1643349543.0122268], [1643349543.012234], [1643349543.0122433]],
[[1643349543.012217], [1643349543.0122268], [1643349543.012234], [1643349543.0122433]],
[[1643349543.012217], [1643349543.0122268], [1643349543.012234], [1643349543.0122433]],
[[1643349543.012217], [1643349543.0122268], [1643349543.012234], [1643349543.0122433]]
]

...

也就是说,如您所见,我得到类似的东西:

[
[[1], [2], [3], [4], [5], [6]],
[[1], [2], [3], [4], [5], [6]],
[[1], [2], [3], [4], [5], [6]],
[[1], [2], [3], [4], [5], [6]],
[[1], [2], [3], [4], [5], [6]],
[[1], [2], [3], [4], [5], [6]],
...
]

已解决

这里的代码:

import time
from datetime import datetime

subList_counter = 1
globals()['subList_{}'.format(subList_counter)] = []
globalList = []
timeList_counter = 1
startTime = time.time()

while True:
globals()['timeList_{}'.format(timeList_counter)] = [time.time()]
globals()['subList_{}'.format(subList_counter)].append(globals()['timeList_{}'.format(timeList_counter)])

if len(globals()['subList_{}'.format(subList_counter)]) == 10:
globalList.append(globals()['subList_{}'.format(subList_counter)])

# print("Updated subList_{} at time {}:n{}".format(subList_counter, time.strftime('%H:%M:%S'), globals()['subList_{}'.format(subList_counter)]))
print("Updated subList_{} at time {}:n{}".format(subList_counter, datetime.now().strftime('%d/%m/%y %H:%M:%S.%f'), globals()['subList_{}'.format(subList_counter)]))

subList_counter += 1
globals()['subList_{}'.format(subList_counter)] = []

# if len(globalList) > 0:
#     print("Updated globalList at time {}:n{}n".format(time.strftime('%H:%M:%S'), globalList))

timeList_counter += 1
time.sleep(5 - ((time.time() - startTime) % 5))  # repeat every 5 seconds

最新更新