Differences between revisions 2 and 3
Revision 2 as of 2008-06-17 12:59:10
Size: 585
Editor: qingfeng
Comment:
Revision 3 as of 2008-06-24 07:38:20
Size: 1077
Editor: qingfeng
Comment:
Deletions are marked like this. Additions are marked like this.
Line 29: Line 29:

== 其他内置函数 ==
 * 小白:enumerate很方便,那Python还有什么内置函数吗?
 * 行者:map,zip,filter,等等,很多.

=== map ===
行者:以一个小例子,展示map的威力,将数组中每一个数乘以2
{{{#!python
map(lambda x:x*2,[1,2,3,4,5])
}}}
行者:同时循环两个一样长的数组
{{{#!python
for x,y in zip([1,2,3],[4,5,6]):
    print "x,y",x,y
}}}
行者:过滤掉,数组中小于3的数
{{{#!python
filter(lambda x:x>3,[1,2,3,4,5])
}}}

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"

其他内置函数

  • 小白:enumerate很方便,那Python还有什么内置函数吗?
  • 行者:map,zip,filter,等等,很多.

map

行者:以一个小例子,展示map的威力,将数组中每一个数乘以2

   1 map(lambda x:x*2,[1,2,3,4,5])

行者:同时循环两个一样长的数组

   1 for x,y in zip([1,2,3],[4,5,6]):
   2     print "x,y",x,y

行者:过滤掉,数组中小于3的数

   1 filter(lambda x:x>3,[1,2,3,4,5])

练习

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

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