for empty iterable上的语句 [英] for statement on empty iterable

查看:101
本文介绍了for empty iterable上的语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我需要做一些循环迭代,这可能是空的

有时,例如


a_list = []

$ a b $ b for a in a_list:

#do something


是这个安全?或者我应该先用if语句对它进行测试?


谢谢

james



I need to do some for loop on iterables which could be empty
sometimes, for example

a_list = []

for i in a_list:
#do something

is this safe? or should I put a if statement to test it first?

Thanks
james

推荐答案

james_027< ca ******** @ gmail.comwrites:
james_027 <ca********@gmail.comwrites:

for i in []:

#do something

这样安全吗?或者我应该先使用if语句对其进行测试?
for i in []:
#do something
is this safe? or should I put a if statement to test it first?



那不会崩溃或类似的东西,但它也没有设置索引变量
,这可能导致在某些情况下会产生混淆。

That doesn''t crash or anything like that, but it also doesn''t
set the index variable, which can cause confusion in some situations.


嗨Paul,
hi Paul,

>

那个不会崩溃或类似的东西,但它也没有设置索引变量,这可能会在某些情况下造成混乱。
>
That doesn''t crash or anything like that, but it also doesn''t
set the index variable, which can cause confusion in some situations.



感谢您的快速回答......其实我在想我如何在for语句中访问
索引?你能帮忙吗?


谢谢

james

Thanks for your quick answer ... Actually I was thinking how do I
access the index inside a for statement? Can you help

Thanks
james


james_027< ca ****** **@gmail.comwrites:
james_027 <ca********@gmail.comwrites:

感谢您的快速回答......其实我在想我怎么用

访问里面的索引一个声明?你能帮助
Thanks for your quick answer ... Actually I was thinking how do I
access the index inside a for statement? Can you help



听起来你刚刚开始学习这门语言......有你吗?b $ b阅读在线教程?这是一个非常简单的介绍。


请参阅: http: //python.org/doc/


无论如何,你可以说


for i in(1,2,3 ):

打印i * 5

例如,在单独的行上打印5,10和15。

It sounds like you''re just starting to learn the language... have you
read the online tutorial yet? That is a pretty easy introduction.

See: http://python.org/doc/

Anyway, you can say

for i in (1,2,3):
print i*5

to print 5, 10, and 15 on separate lines, for example.


这篇关于for empty iterable上的语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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