[英]python progress of a for loop - juypter
On Jupyter - I have a complex nested for loop and if clauses and a list_pairs:在 Jupyter 上 - 我有一个复杂的嵌套 for 循环和 if 子句和一个 list_pairs:
pair_list = [[1,2], [1,3], ...] --> len(pair_list) about 3.5 million
check = 0
for i, j in pair_list:
print(check) #this creates one extra line for each iteration
check += 1
these outputs:这些输出:
0
1
2
3
4
is it possible to print the check variable each second with the updated number - for example 0 should be replace with 1 and 1 with 2 -- it should print on the same line each time and replace the old with the new one - it can also be outside the for loop是否可以每秒用更新的数字打印检查变量 - 例如 0 应该替换为 1 和 1 替换为 2 - 它应该每次都打印在同一行并用新的替换旧的 - 它也可以在 for 循环之外
What you need is a "progress bar"!你需要的是一个“进度条”!
How to get it?如何得到它? Use tqdm
.使用tqdm
。 For jupyter-notebook
, use this对于jupyter-notebook
,使用这个
from tqdm.notebook import tqdm
for i, j in tqdm(pair_list):
#You won't need check variable
print(check) #this creates one extra line for each iteration
check += 1
# Carry on with the logic
Sample usage:示例用法:
In Python you can change the value of an item in a list using list indexing,在 Python 中,您可以使用列表索引更改列表中项目的值,
pair_list = [[1,2], [1,3], ...]
check = 0
for i in range(len(pair_list)):
pair_list[i][0] = pair_list[i][1]
check += 1
print(check)
par_list[i][0]
would index the 0th element in the Ith list. par_list[i][0]
将索引第 I 个列表中的第 0 个元素。 Then par_list[i][1]
would be indexing the second element in the Ith list.然后par_list[i][1]
将索引 Ith 列表中的第二个元素。 list indexing always starts at 0, so 0 is first, 1 is second item and so on.列表索引总是从 0 开始,所以 0 是第一个,1 是第二个项目,依此类推。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.