当您不需要python中的索引时,如何制作For循环? [英] how do you make a For loop when you don't need index in python?
问题描述
如果我需要在python中进行for循环
if i need a for loop in python
for i in range(1,42):
print "spam"
但是对于任何pylint抱怨未使用的变量,请不要使用 i。我该如何处理?我知道您可以执行以下操作:
but don't use the "i" for anything pylint complains about the unused variable. How should i handle this? I know you can do this:
for dummy_index in range(1,42):
print "spam"
但是这样做对我来说似乎很奇怪,有更好的方法吗?
but doing this seems quite strange to me, is there a better way?
我对python很陌生,所以如果我缺少明显的东西,请原谅我。
I'm quite new at python so forgive me if I'm missing something obvious.
推荐答案
在Python中,没有没有计数器变量的 n 次循环是没有自然方式的,因此,您不应该使用丑陋的黑客程序来使代码分析器静音。
There is no "natural" way to loop n times without a counter variable in Python, and you should not resort to ugly hacks just to silence code analyzers.
在您的情况下,我建议以下其中一项:
In your case I would suggest one of the following:
- 只需忽略PyLint警告(或过滤报告的警告之一) -字符变量)
- 配置PyLint忽略名为
i
的变量,这些变量通常仅用于for
仍会循环。 - 使用前缀标记未使用的变量,可能使用默认的
_
(比dummy
)
- Just ignore the PyLint warning (or filter reported warnings for one-character variables)
- Configure PyLint to ignore variables named
i
, that are usually only used infor
loops anyway. - Mark unused variables using a prefix, probably using the default
_
(it's less distracting thandummy
)
这篇关于当您不需要python中的索引时,如何制作For循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!