【问题标题】:Print if remainder of division with certain number is zero - Python [closed]如果具有特定数字的除法余数为零,则打印 - Python [关闭]
【发布时间】:2020-12-30 21:16:08
【问题描述】:
我正在运行一个大循环,我想看看进度。我希望代码在 1e4 的余数为零时打印迭代的索引。到目前为止我尝试过的看起来是这样的:
print(i if (i%1e4==0))
但它似乎不起作用。我不能使用诸如 qdtm 之类的任何功能,因为我也在使用 numba 并且它不接受它。有什么帮助吗?
【问题讨论】:
标签:
python
pandas
loops
for-loop
progress-bar
【解决方案1】:
条件运算符需要else 值。如果条件为假时不想打印任何内容,则需要使用普通的if 语句,而不是条件表达式。
if i % 1e4 == 0:
print(i)
如果确实必须是单个语句,您可以使用print() 的end 参数不打印任何内容,然后在打印的输出中包含换行符。
print(str(i) + '\n' if i % 1e4 == 0 else '', end='')
【解决方案2】:
for i in range(int(1e5)):
if (i%1e4==0):
print(i)
输出:
0
10000
20000
30000
40000
50000
60000
70000
80000
90000