获取迭代次数 [英] Get number of iteration

查看:51
本文介绍了获取迭代次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!

如果我遍历一个列表,有没有办法可以获得

迭代次数:第一,第二,第三,......


l = [" three",four,five,six]

for x in l

print x

print x.iteration()#< - 这就是我要找的!

print" next"


打印:




1

next

4

2

next

fixe

3

next

6

4

next


Thx,

Florian

Hi!
If I iterate through a list, is there a way I can get the number of the
iteration: first, second, third, ...

l = ["three", "four", "five", "six"]
for x in l
print x
print x.iteration() # <- That''s what I''m looking for!
print "next"

prints that:

three
1
next
four
2
next
fixe
3
next
six
4
next

Thx,
Florian

推荐答案

>如果我遍历一个列表,有没有办法可以得到
> If I iterate through a list, is there a way I can get the number of the
迭代的次数:第一,第二,第三,......

l = ["三个,四个,五个,六个]
for x
print x
print x.iteration()#< - 那是'我在找什么!
打印下一个
iteration: first, second, third, ...

l = ["three", "four", "five", "six"]
for x in l
print x
print x.iteration() # <- That''s what I''m looking for!
print "next"




不,这不行 - x是列表元素的值,而不是c ++ - 比如

iterator-object(在访问实际的

值之前必须取消引用)。


所以通常x不会有迭代方法。但当然你也可以这样做:
这个:

$ x $ b for x in xrange(len(l)):

x = l [i]

打印x

打印i

打印下一页


或 - 如果len()由于某种原因不能应用于你的序列,因为它是例如.b $ b一个可迭代的对象,你当然可以跟踪自己的

计数器:


i = 0

for some in some_iterable_thingy:

打印x

打印i

i + = 1

打印下一页


Diez



No, this won''t work - x is the value of the list element, not an c++-like
iterator-object (that has to be dereferenced before accessing the actual
value).

So usually x won''t have an iteration-method. But of course you can alwas do
this:

for i in xrange(len(l)):
x = l[i]
print x
print i
print "next"

Or - if len() can''t be applied to your sequence for whatever reason, as it
is e.g. a iterable object, you can of course keep track with your own
counter:

i = 0
for x in some_iterable_thingy:
print x
print i
i += 1
print "next"

Regards,

Diez


Florian Lindner写道:
Florian Lindner wrote:
如果我遍历一个列表,有没有办法可以得到
迭代次数:第一,第二,第三,......

l = [" three" ,四,五,六,对于x中的x
打印x
打印x.iteration()#< - 这就是我的意思''我正在寻找!
打印" next"
If I iterate through a list, is there a way I can get the number of the
iteration: first, second, third, ...

l = ["three", "four", "five", "six"]
for x in l
print x
print x.iteration() # <- That''s what I''m looking for!
print "next"


sample =" ;树为五.split()
为索引,项目为枚举(示例):
sample = "tree for five".split()
for index, item in enumerate(sample):



.... print ind ex,item

....

0 tree

1 for

2 5


您正在寻找enumerate()。不过,计数从0开始。


Peter


.... print index, item
....
0 tree
1 for
2 five

You are looking for enumerate(). Counting starts at 0, though.

Peter


python 2.3中有一个名为enumerate的函数。
< br>
there is a function in python 2.3 called enumerate.

l = range(0,51,5)
e = enumerate(range(0,51) ,5))
[(0,0),(1,5),(2,10),(3,15),(4,20),(5,25),(6,30) ,(7,35),(8,

40),(9,45),(10,50)] e =枚举(范围(0,51,5))
对于索引,e中的值:
.... print"值%i的索引是%i %(价值,指数)

....

0的索引是0

5的索引是1 />
10的索引是2

..

..

..

如果你使用的是旧版本的python,它没有枚举

你可以这样做l = range(0,51,5)
zip(范围(len(l)),l)
l = range(0,51,5)
e = enumerate(range(0,51,5))
e <enumerate object at 0x0119BBC0> list(e) [(0, 0), (1, 5), (2, 10), (3, 15), (4, 20), (5, 25), (6, 30), (7, 35), (8,
40), (9, 45), (10, 50)] e = enumerate(range(0,51,5))
for index, value in e: .... print "The index of value %i is %i" % (value, index)
....
The index of value 0 is 0
The index of value 5 is 1
The index of value 10 is 2
..
..
..

if you are using an older version of python which doesn''t have enumerate
you could do this l = range(0,51,5)
zip(range(len(l)), l)



[(0,0),(1,5),(2,10),(3 ,15),(4,20),(5,25),(6,30),(7,35),(8,

40),(9,45),(10 ,50)]


[(0, 0), (1, 5), (2, 10), (3, 15), (4, 20), (5, 25), (6, 30), (7, 35), (8,
40), (9, 45), (10, 50)]


这篇关于获取迭代次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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