python循环 [英] python loops
问题描述
在C和C ++和Java中,''for''语句是制作非常简洁循环的快捷方式。在python中,''for''迭代序列中的元素。
有没有办法在python中执行此操作,比''更简洁'?'
C:
for(i = 0; i< length; i ++)
python:
而i< ;长度:
i + = 1
Putty写道:
在C和C ++和Java中,''for''语句是制作非常简洁循环的快捷方式。在python中,''for''迭代序列中的元素。
有没有办法在python中执行此操作,比''更简洁'?'
C:
for(i = 0; i< length; i ++)
python:
而我<长度:
i + = 1
我的范围(长度):
print i
-
-
我永远不会有几件事:
*我永远不会被吸引到女性。
*我永远不会喜欢别人的陪伴。
这些现实对我的敌人来说究竟是怎样的,不是我的关注。
>
AlbaClause写道:
for i in range(length):
print i
通常更好:
$ x $ b for x in xrange(长度):
...
再见,
bearophile
Putty写道:
< blockquote class =post_quotes>
在C和C ++和Java中,''for''语句是制作非常简洁循环的快捷方式。在python中,''for''迭代序列中的元素。
有没有办法在python中执行此操作,比''更简洁'?'
C:
for(i = 0; i< length; i ++)
python:
而我<长度:
i + = 1
正如AlbaClause已经证明你可以迭代索引以及
使用for-statement和range()函数。但你也可以使用
builtin enumerate()类型同时迭代元素和索引:
for i,item in enumerate((s1,s2)):
print i,item
0 s1
1 s2
In C and C++ and Java, the ''for'' statement is a shortcut to make very
concise loops. In python, ''for'' iterates over elements in a sequence.
Is there a way to do this in python that''s more concise than ''while''?
C:
for(i=0; i<length; i++)
python:
while i < length:
i += 1
Putty wrote:
In C and C++ and Java, the ''for'' statement is a shortcut to make very
concise loops. In python, ''for'' iterates over elements in a sequence.
Is there a way to do this in python that''s more concise than ''while''?
C:
for(i=0; i<length; i++)
python:
while i < length:
i += 1for i in range(length):
print i
--
--
There are several things that I will never be:
* I will never be attracted to females.
* I will never enjoy the company of others.
Exactly how these realities bode for my enemy, is not of my concern.
AlbaClause wrote:
for i in range(length):
print iOr usually better:
for ii in xrange(length):
...
Bye,
bearophile
Putty wrote:In C and C++ and Java, the ''for'' statement is a shortcut to make very
concise loops. In python, ''for'' iterates over elements in a sequence.
Is there a way to do this in python that''s more concise than ''while''?
C:
for(i=0; i<length; i++)
python:
while i < length:
i += 1As AlbaClause had demonstrated you can iterate over indices as well
using the for-statement and the range() function. But you can also
combine iterating over elements and indices at the same time using the
builtin enumerate() type:
for i, item in enumerate(("s1","s2")):
print i,item
0 s1
1 s2
这篇关于python循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!