如何为 python 列表中的单个项目添加计时器?
How to add a timer to individual items in a list in python?
我正在编写一个程序,其中有 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
我想不出任何解决方案可以满足我的需要。有什么建议去哪里看吗?
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']
我正在编写一个程序,其中有 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
我想不出任何解决方案可以满足我的需要。有什么建议去哪里看吗?
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']