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

Infinite countdown generating new sublists to append to a main list in python

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

也就是说,类似于:

    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