没有索引和项目的Python for循环 [英] Python for-loop without index and item

查看:54
本文介绍了没有索引和项目的Python for循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python中是否可以有一个没有索引和项目的for循环?我有类似以下内容:

Is it possible in python to have a for-loop without index and item? I have something like the following:

list_1 = []
for i in range(5):
    list_1.append(3)

上面的代码可以正常工作,但根据pep8编码准则,效果不佳.它说:未使用的变量'i'".

The code above works fine, but is not nice according to the pep8 coding guidelines. It says: "Unused variable 'i'".

有没有办法在没有索引和项目的情况下进行for循环(没有while循环)?还是应该忽略编码准则?

Is there a way to make a for-loop (no while-loop) without having neither the index nor the item? Or should I ignore the coding guidelines?

推荐答案

您可以将 i 替换为 _ ,以使其成为不可见"变量.

You can replace i with _ to make it an 'invisible' variable.

请参阅相关内容:单个下划线"_"的目的是什么?Python中的变量?.

这篇关于没有索引和项目的Python for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆