Differences between revisions 3 and 6 (spanning 3 versions)
Revision 3 as of 2008-06-24 07:38:20
Size: 1077
Editor: qingfeng
Comment:
Revision 6 as of 2008-08-26 13:58:53
Size: 2639
Editor: lizzie
Comment:
Deletions are marked like this. Additions are marked like this.
Line 8: Line 8:
== enumerate ==
小白:Python中的循环,想知道现在循环了几次好麻烦,总要在循环外定义一个变量,然后每次循环再加1,像这样:
== 概述 ==
Python除了语言简洁,容易上手等优点,还有一个重要的优点,就是存在大量的内置函数,方便编程.这个章节将介绍这些常用函数.让您更好的了解Python的诱人之处.

== 使用 ==
=== enumerate ===
enumerate是python 2.3中新增的内置函数,它的英文说明为:
Line 11: Line 15:
i=0
for obj in objlist:
    i+=1
    print i
enumerate(iterable)
    Return an enumerate object. iterable must be a sequence, an iterator, or some other object which supports iteration. The next() method of the iterator returned by enumerate() returns a tuple containing a count (from zero) and the corresponding value obtained from iterating over iterable. enumerate() is useful for obtaining an indexed series: (0, seq[0]), (1, seq[1]), (2, seq[2]), .... New in version 2.3.
Line 17: Line 19:
行者:..........请尝试enumerate 它特别适合用于一个for循环时,当我们同时需要计数和元素时可以使用这个函数。举个简单的例子,有一个字符串数组,需要一行一行打印出来,同时每行前面加上计数,从1开始。

例如:
Line 24: Line 28:
0,"a"
1,"b"
2,"c"
0,a
1,b
2,c
Line 29: Line 33:
=== map ===
函数说明:map(function, sequence[, sequence, ...]) -> list,两个参数一个是函数名,另一个是列表或元组,返回一个列表。
例子:将数组中每一个数乘以2
{{{#!python
print "map(lambda x:x*2,[1,2,3,4,5]) -> ",map(lambda x:x*2,[1,2,3,4,5])
}}}
{{{
map(lambda x:x*2,[1,2,3,4,5]) -> [2, 4, 6, 8, 10]
}}}
Line 30: Line 43:
== 其他内置函数 ==
 * 小白:enumerate很方便,那Python还有什么内置函数吗?
 * 行者:map,zip,filter,等等,很多.

=== map ===
行者:以一个小例子,展示map的威力,将数组中每一个数乘以2
=== zip ===
函数说明:zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)],可以同时循环两个一样长的数组,返回一个包含每个参数元组对应元素的元组。各参数元组长度最好一致,若不一致,采取截断使得返回的结果元组的长度为各参数元组长度最小的。
Line 37: Line 46:
map(lambda x:x*2,[1,2,3,4,5])
}}}
行者:同时循环两个一样长的数组
{{{#!python
print "zip([1,2,3],[4,5,6]):"
Line 44: Line 50:
行者:过滤掉,数组中小于3的数
{{{#!python
filter(lambda x:x>3,[1,2,3,4,5])
{{{
zip([1,2,3],[4,5,6]):
x,y 1 4
x,y 2 5
x,y 3 6
Line 49: Line 57:
== 练习 ==
=== filter ===
函数说明: filter(function or None, sequence) -> list, tuple, or string,包括两个参数,分别是function和list。该函数根据function参数返回的结果是否为真来过滤list参数中的项,最后返回一个新列表
例子:过滤掉数组中小于3的数
{{{#!python
print "filter(lambda x:x>3,[1,2,3,4,5]) -> ",filter(lambda x:x>3,[1,2,3,4,5])
}}}
{{{
filter(lambda x:x>3,[1,2,3,4,5]) -> [4, 5]
}}}

status

草稿

清风; 100%

TableOfContents

概述

Python除了语言简洁,容易上手等优点,还有一个重要的优点,就是存在大量的内置函数,方便编程.这个章节将介绍这些常用函数.让您更好的了解Python的诱人之处.

使用

enumerate

enumerate是python 2.3中新增的内置函数,它的英文说明为:

enumerate(iterable)
    Return an enumerate object. iterable must be a sequence, an iterator, or some other object which supports iteration. The next() method of the iterator returned by enumerate() returns a tuple containing a count (from zero) and the corresponding value obtained from iterating over iterable. enumerate() is useful for obtaining an indexed series: (0, seq[0]), (1, seq[1]), (2, seq[2]), .... New in version 2.3. 

它特别适合用于一个for循环时,当我们同时需要计数和元素时可以使用这个函数。举个简单的例子,有一个字符串数组,需要一行一行打印出来,同时每行前面加上计数,从1开始。

例如:

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

0,a
1,b
2,c

map

函数说明:map(function, sequence[, sequence, ...]) -> list,两个参数一个是函数名,另一个是列表或元组,返回一个列表。 例子:将数组中每一个数乘以2

   1 print "map(lambda x:x*2,[1,2,3,4,5]) -> ",map(lambda x:x*2,[1,2,3,4,5])

map(lambda x:x*2,[1,2,3,4,5]) ->  [2, 4, 6, 8, 10]

zip

函数说明:zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)],可以同时循环两个一样长的数组,返回一个包含每个参数元组对应元素的元组。各参数元组长度最好一致,若不一致,采取截断使得返回的结果元组的长度为各参数元组长度最小的。

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

zip([1,2,3],[4,5,6]):
x,y 1 4
x,y 2 5
x,y 3 6

filter

函数说明: filter(function or None, sequence) -> list, tuple, or string,包括两个参数,分别是function和list。该函数根据function参数返回的结果是否为真来过滤list参数中的项,最后返回一个新列表 例子:过滤掉数组中小于3的数

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

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

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

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