简单的python迭代问题 [英] Simple python iteration question
问题描述
我刚开始使用python,并且有一个for循环问题
在c ++(或其他一些语言)中我可以这样做:
for(int i = 0,j = 0; i< i_len,j< j_len; ++ i,++ j){}
如果我在python中有这个:
l = [''''',''b'',''c'']
我想得到的值和迭代器:
for i,v in len(l),l:
print v
打印我
或类似的东西没有在我的循环外声明迭代器...
我该怎么办这个?
谢谢!
Hi,
I just started with python, and have a for loop question
In c++ (or a number of other languages) I can do this:
for (int i=0, j=0; i < i_len, j< j_len; ++i, ++j) {}
If I have this in python:
l = [''a'', ''b'', ''c'']
I want to get the value and also an iterator:
for i,v in len(l), l:
print v
print i
Or something like this without declaring the iterator outside my loop...
How do I do this?
Thanks!
推荐答案
8月14日上午10点22分,Bryan< b ... @ bcc.comwrote:
On Aug 14, 10:22 am, Bryan <b...@bcc.comwrote:
我刚开始使用python,并且有一个for循环问题
在c ++(或许多其他语言)中,我可以这样做:
for(int i = 0,j = 0; i< i_len ,j< j_len; ++ i,++ j){}
如果我在python中有这个:
l = [''a'',''b'',''c'']
我想得到的价值和迭代器:
for i,v in len(l),l:
print v
打印我
这样的事情没有在我的循环外声明迭代器...
我该怎么办?这样做吗?
谢谢!
Hi,
I just started with python, and have a for loop question
In c++ (or a number of other languages) I can do this:
for (int i=0, j=0; i < i_len, j< j_len; ++i, ++j) {}
If I have this in python:
l = [''a'', ''b'', ''c'']
I want to get the value and also an iterator:
for i,v in len(l), l:
print v
print i
Or something like this without declaring the iterator outside my loop...
How do I do this?
Thanks!
这将获得索引处的索引和项目,
for i in range(0,len(l)):
打印i
print l [i]
this will get index and item at index,
for i in range(0, len(l)):
print i
print l[i]
Bryan< b@bcc.comwrote:
Bryan <b@bcc.comwrote:
我该怎么做?
How do I do this?
for i,item in enumerate(l):
print i,item
-
劳伦斯,oluyede.org - neropercaso.it
很难让男人理解
当他的工资取决于不是
理解它 - Upton Sinclair
for i, item in enumerate(l):
print i, item
--
Lawrence, oluyede.org - neropercaso.it
"It is difficult to get a man to understand
something when his salary depends on not
understanding it" - Upton Sinclair
2007年8月14日星期二下午12:22:04 -0400,Bryan写道:
On Tue, Aug 14, 2007 at 12:22:04PM -0400, Bryan wrote:
我刚开始使用python,并且有一个for循环问题
在c ++(或许多其他语言)中我可以这样做:
for(int i = 0,j = 0; i< i_len,j< j_len; ++ i,++ j){}
如果我有这个在python:
l = ['''',''b'',''c'']
我想得到的价值和也是一个迭代器:
for i,v in len(l),l:
print v
print i
或类似的东西,而不是在循环外声明迭代器...
我该怎么做?
I just started with python, and have a for loop question
In c++ (or a number of other languages) I can do this:
for (int i=0, j=0; i < i_len, j< j_len; ++i, ++j) {}
If I have this in python:
l = [''a'', ''b'', ''c'']
I want to get the value and also an iterator:
for i,v in len(l), l:
print v
print i
Or something like this without declaring the iterator outside my loop...
How do I do this?
使用enumerate()内置。
Use the enumerate() builtin.
>> l = ['''',''b'',''c'']
for i,v in enumerate(l):
>>l = [''a'', ''b'', ''c'']
for i, v in enumerate(l):
...打印i,v
...
0 a
1 b
2 c
-
[Will Maier] ---- ------------- [wi ******* @ ml1.net | http://www.lfod.us/]
这篇关于简单的python迭代问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!