- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在编写一个代码来模拟一个棋子在垄断板上转一百万次。我想要一个 tqdm 进度条,每次完成转板时都会更新。
以下是我当前的代码。我正在使用一个 while 循环,当电路板的转数超过所需数量时,它会停止。
import os
from openpyxl import Workbook
from monopolyfct import *
def main(runs, fileOutput):
### EXCEL SETUP ###
theWorkbook = Workbook() # Creates the workbook interface.
defaultSheet = theWorkbook.active # Creates the used worksheet.
currentData = ["Current Table Turn", "Current Tile"] # Makes EXCEL column titles.
defaultSheet.append(currentData) # Appends column titles.
### CONTENT SETUP ###
currentData = [1, 0] # Sets starting position.
defaultSheet.append(currentData) # Appends starting position.
while currentData[0] <= runs:
### ROLLING THE DICES PROCESS ###
dices = twinDiceRoll()
currentData[1] += dices[2] # Updating the current tile
### SURPASSING THE NUMBER OF TILES ONBOARD ###
if currentData[1] > 37: # If more than a table turn is achieved,
currentData[0] += 1 # One more turn is registered
currentData[1] -= 38 # Update the tile to one coresponding to a board tile.
else:
pass
### APPENDING AQUIRED DATA ###
defaultSheet.append(currentData)
### MANAGIING SPECIAL TILES ###
if currentData[1] == 2 or 15 or 31: # Community chess
pass #TODO: Make a mechanic simulating the community chest card draw and it's related action.
elif currentData[1] == 5 or 20 or 34: # Chance
pass #TODO: Make a mechanic simulating the chance card draw and it's related action.
elif currentData[1] == 28: # Go to Jail
pass #TODO: Make a mechanic simulating the entire jail process
### TWIN DICE ROLL EXCEPTION ###
if dices[3] is True: # If the dices roll a double,
pass #TODO: Make a mechanic considering that three doubles sends one to Jail.
### STORING THE ACCUMULATED DATA ###
theWorkbook.save(fileOutput) # Compiles the data in a .xlxs file.
if __name__ == "__main__":
terminalWidth = os.get_terminal_size().columns # Gets current terminal width.
space(3)
print("Python Monopoly Statistics Renderer".upper().center(terminalWidth)) # Prints the title.
print("(PMSR)".center(terminalWidth)) # Prints the acronym.
space(2)
runs = int(request("For how many table turns do you want the simulation to run?")) # Prompts for the desired run ammount
#runs = 1000
fileOutput = request("What should be the name of the file in which statistics are stored?") # Prompts for the desired store filename
#fileOutput = "test"
fileOutput += ".xlsx" # Adds file extension to filename
main(runs, fileOutput)
最佳答案
您可以通过在构造函数中指定 total
参数来在 tqdm
中使用手动控制。来自 manual 的逐字记录:
with tqdm(total=100) as pbar:
for i in range(10):
sleep(0.1)
pbar.update(10)
更新
要在没有上下文管理器的情况下手动控制 tqdm
(又名 with
语句),您需要在完成使用后关闭进度条。这是手册中的另一个示例:
pbar = tqdm(total=100)
for i in range(10):
sleep(0.1)
pbar.update(10)
pbar.close()
为此,您需要知道预期运行的总数。在您的代码中,它可能看起来像
...
pbar = tqdm(total = runs+1)
while currentData[0] <= runs:
### ROLLING THE DICES PROCESS ###
dices = twinDiceRoll()
currentData[1] += dices[2] # Updating the current tile
### SURPASSING THE NUMBER OF TILES ONBOARD ###
if currentData[1] > 37: # If more than a table turn is achieved,
currentData[0] += 1 # One more turn is registered
currentData[1] -= 38 # Update the tile to one coresponding to a board tile.
pbar.update(1)
else:
pass
...
pbar.close()
但是,这段代码并不完美:考虑 currentData[1]
是否总是小于 37 —— 进度条将停止而不更新。如果您尝试在 else:...
部分更新它,您可能会违反 total
上限。这是一个开始 :)
关于python - 在 while 循环中使用 tqdm 进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45808140/
这个问题已经有答案了: What can I do about "ImportError: Cannot import name X" or "AttributeError: ... (most li
我正在使用Python3.6运行以下像素递归神经网络(RNN)代码。但是,出现了一个错误:。有谁知道怎么解这个题吗?
我正在研究 Pytorchlightning,tqdm 的进度条有很多问题,它不断地从短到长来回调整大小,让阅读日志文本变得非常不愉快,我意识到进度条并不是真正必要的,我只想保留当前epoch、当前b
我有一组进度条,我使用 tqdm.write 在它们运行时在它们上面打印日志消息。出于审美原因,我想用空行或类似重复的“=”在视觉上将日志消息与进度条分开,例如: Log message 1
我正在尝试使用 Python 中可用的 tqdm 模块打印优化算法的进度状态,但是,每次我尝试更新它时,它都会在新行中打印进度,有没有办法我只能更新最初正在实例化的原始 tqdm 条?我的代码如下,它
我想在我的脚本中使用 tqdm 但不要求其他人使用它,如果他们还没有安装的话。 我发现了这个: try: import tqdm except ImportError: tqdm =
我在递归目录树时使用 tqdm。我不知道我将使用的路径数,而且我不想在我做工作之前建立该列表只是为了获得准确的总数,我宁愿让它更新进度条为它继续。 我发现我可以很好地使用 'reset(total=n
在从 SQL 数据库读取大型关系到 pandas 数据帧时,最好有一个进度条,因为元组的数量是静态已知的,并且可以估计 I/O 率。它看起来像 tqdm模块有一个函数tqdm_pandas它将报告列上
我正在尝试使用 tqdm 报告从三个链接下载每个文件的进度,我想使用多线程从每个链接同时下载同时更新进度条。 但是当我执行我的脚本时,有多行进度条似乎线程正在同时更新 tqdm 进度条。 我在问我应该
我有一个使用 tqdm 的 python 包进度条。但是,我不希望这成为我的包的用户的硬依赖。如果未安装 tqdm,是否有一些简单的嵌入式解决方案可以轻松充当备用解决方案? 我正在使用 tqdm.tq
我正在尝试编写一个使用多个线程来完成其工作的程序,因此我想通过多个进度条(每个线程一个)来显示进度。我的代码看起来很像这样 import threading def my_function(posit
这个问题在这里已经有了答案: Redirect print command in python script through tqdm.write() (5 个答案) 关闭 5 年前。 我有以下 P
我正在使用 tqdm 库,它没有给我进度条,而是给我如下所示的输出,它只是告诉我迭代: 251it [01:44, 2.39it/s] 知道为什么代码会这样做吗?我想这可能是因为我给它传递了一个发电机
我正在使用 python 开发一个小型命令行游戏,我正在使用 tqdm 模块显示进度条。我使用 msvcrt 模块监听用户输入以中断进度。一旦中断,用户可以通过在命令行提示符中输入“restart”来
我正在循环一个我知道长度的大文件,但是由于它太大而无法放入内存,所以我正在懒惰地处理。我希望能够使用 tqdm 来跟踪我在文件中的进度,但是由于它无法从我正在使用的生成器中获取示例总数,因此它唯一显示
我想在每个进程中使用 4 个 python 进程和 tqdm 进度条: 66%|███████████████████████████▌ | 80/122 [00:44<0
我有一个 tqdm 进度条。我在代码的某些部分使用 set_postfix_str 方法设置后缀字符串。在另一部分,我需要附加到这个字符串。这是一个 MWE。 import numpy as np f
我正在尝试创建一个进度条,只要异步任务完成,它就会更新。 我有以下代码 scan_results = [] tasks = [self.run_scan(i) for i in input_paths
当我使用 bar_format 选项添加颜色时,我不确定为什么我的 TQDM 进度条分成多行。它似乎也与迭代次数有关,因为当我运行相同的代码时只有 10 次迭代而不是 1389 次迭代时它不会 spl
我有多个并行处理的大量 csv 文件。我希望每个文件都有一个进度条。 但是,虽然我显示了 5 个条,但只有最后一个正在更新 - 似乎所有进程都会同时更新。由于我无法将整个 csv 文件读入内存,因此我
我是一名优秀的程序员,十分优秀!