X次运行流程的更多Pythonic方式 [英] More Pythonic Way to Run a Process X Times
本文介绍了X次运行流程的更多Pythonic方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
哪个更pythonic?
Which is more pythonic?
循环时:
count = 0
while count < 50:
print "Some thing"
count = count + 1
For循环:
for i in range(50):
print "Some thing"
不重复,因为这可以确定哪个更清晰,而不是如何在不使用"i"的情况下运行范围-即使最终以最优雅的方式出现
not duplicate because this has answers to determine which is clearer, vs. how to run a range without 'i' -- even though that ended up being the most elegant
推荐答案
个人:
for _ in range(50):
print "Some thing"
如果您不需要 i
.如果您使用Python<3,并且您想重复很多次循环,请使用 xrange
,因为不需要事先生成整个列表.
if you don't need i
. If you use Python < 3 and you want to repeat the loop a lot of times, use xrange
as there is no need to generate the whole list beforehand.
这篇关于X次运行流程的更多Pythonic方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文