【问题标题】:How to add a timer to individual items in a list in python?如何为python列表中的单个项目添加计时器?
【发布时间】:2022-01-03 04:08:08
【问题描述】:

我正在编写一个有 2 个列表的程序。当满足某个条件时,我想将一个项目从“活动列表”移动到“非活动列表”。然后,我想为移动到非活动列表的项目设置一个计时器 4 小时。一旦时间到期,我想将该项目移回活动列表。

我可以让一些项目进入非活动列表,每个项目都需要自己的 4 小时计时器。

一些简单的代码:

games_active = []
games_not_active = []

while True:
    for x in games_active:
        game = games_active.pop()
        try:
            bot.click_autos()
            sleep(random.uniform(4.8, 5.9))
        except TypeError:
            games_not_active.append(game)
            error += 1        

我想不出任何可以满足我需求的解决方案。有什么建议去哪里看?

【问题讨论】:

    标签: python list timer


    【解决方案1】:
    My idea...
    
    * Create a dictionary of all of your games
    * For each game - capture the time before it switches back to active (for active games, set to 0)
    * Decrement this time in your loop
    * Generate your list on the fly whenever you need it.
    
    So its something like this...
    
    games = dict()
    games['game1'] = dict()
    games['game1']['timetoActive'] = 0
    games['game1']['status'] = 'active'
    ....
    
    while True:
    
    for game in games:
        if game['timetoActive'] > 0:
           game['timetoActive'] = game['timetoActive'] - 1
           # Decrease the time
    
        else:
          games['game1']['status'] = 'inactive'
    
    .....
    
    Then if you need to get all of the active games you could do something like
    
    games = [game for game in games if game['Status'] = 'Active']
    

    【讨论】:

    • 这种方法看起来很有趣。我想我需要修改以捕获它变为非活动状态的时间,然后与当前时间进行比较,如果超过 4 小时变为活动状态。不过谢谢你的想法!
    • 听起来不错 - 如果这符合您的需求,请随时接受答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-17
    • 2021-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多