Search code examples
pythonlistinfinite-loopcountdownsublist

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


My question is how to create a python globalList and, inside an infinite while True loop, to create an infinite countdown (for example of 5 seconds) during which it will create a subList that will store some data (here current time, as example) along the whole period of 5 seconds, after which append the subList to globalList and restart the countdown with a new_subList to store other data until the break of while True loop...

That is, something like:

    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...
    ]

Here what I have tried:

    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

But this code gives an output like:

    [
      [[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]]
    ]
    
    ...

that is, as you can see, I obtain something like:

    [
      [[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]],
      ...
    ]

Solution

  • Solved

    here the code:

        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