Differences between revisions 1 and 2
Revision 1 as of 2008-04-25 06:50:36
Size: 494
Editor: qingfeng
Comment:
Revision 2 as of 2008-06-17 12:59:10
Size: 585
Editor: qingfeng
Comment:
Deletions are marked like this. Additions are marked like this.
Line 9: Line 9:
小白:Python中的循环,想知道现在循环了几次好麻烦,总要在循环外定义一个变量,然后每次循环再加1,像这样:
{{{
i=0
for obj in objlist:
    i+=1
    print i
}}}
Line 10: Line 17:
由于Python没有像
{{{
for(int i=0;i<10;i++)
}}}
这样的循环方式,所以我们要拿到循环的索引就需要用到enumerate:
行者:..........请尝试enumerate
Line 29: Line 31:
 

status

草稿

清风; 100%

TableOfContents

enumerate

小白:Python中的循环,想知道现在循环了几次好麻烦,总要在循环外定义一个变量,然后每次循环再加1,像这样:

i=0
for obj in objlist:
    i+=1
    print i

行者:..........请尝试enumerate

   1 mylist=["a","b","c"]
   2 for index,obj in enumerate(mylist):
   3     print index,obj

0,"a"
1,"b"
2,"c"

练习

-- 清风 DateTime(2008-04-25T14:33:00Z)

ObpLovelyPython/PCS205 (last edited 2009-12-25 07:15:54 by localhost)