Differences between revisions 6 and 15 (spanning 9 versions)
Revision 6 as of 2008-08-26 13:58:53
Size: 2639
Editor: lizzie
Comment:
Revision 15 as of 2009-08-27 05:54:02
Size: 3782
Editor: ZoomQuiet
Comment:
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:
##OBP项目图书reST通用文章模板
###format rst
||status|| 草稿 ||清风; 100%||
#acl WoodpeckerAdminGroup:read,write,delete,revert LovPyBookGroup:read,write,delete,revert All:
#pragma section-numbers off
||status|| 草稿 ||清风 & liz; 100%||
Line 8: Line 8:
##startInc
= PCS205 内建函式(enumerate) =
Line 9: Line 11:
Python除了语言简洁,容易上手等优点,还有一个重要的优点,就是存在大量的内置函,方便编程.这个章节将介绍这些常用函数.更好的了解Python的诱人之处. Python除了语言简洁,容易上手等优点,还有一个重要的优点,就是存在大量的内置函,方便编程.这个章节将介绍这些常用函式,我们更好的了解Python的诱人之处.
Line 13: Line 15:
enumerate是python 2.3中新增的内置函数,它的英文说明为 enumerate是Python 2.3中新增的内置函式,它的英文说明为:
Line 18: Line 20:

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

例如
:
它特别适合用于一个for循环时,当我们同时需要计数和元素时可以使用这个函式.
比如,
有一个字符串数组,需要一行一行打印出来,同时每行前面加上计数,从1开始:
Line 26: Line 26:
}}} }}}输出结果为:
Line 28: Line 28:
0,a
1,b
2,c
0 a
1 b
2 c
Line 34: Line 34:
说明:map(function, sequence[, sequence, ...]) -> list两个参数一个是函另一个是列表或元组返回一个列表
例子:将数组中每一个数乘以2
说明:map(function, sequence[, sequence, ...]) -> list,两个参数一个是函,另一个是列表或元组,返回一个列表.
比如,将数组中每一个数乘以2:
Line 38: Line 38:
}}} }}}输出结果为:
Line 44: Line 44:
说明:zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]可以同时循环两个一样长的数组返回一个包含每个参数元组对应元素的元组各参数元组长度最好一致若不一致采取截断使得返回的结果元组的长度为各参数元组长度最小的 说明:zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)],可以同时循环两个一样长的数组,返回一个包含每个参数元组对应元素的元组.各参数元组长度最好一致,若不一致,采取截断使得返回的结果元组的长度为各参数元组长度最小的.
比如:
Line 49: Line 50:
}}} }}}输出结果为:
Line 58: Line 59:
说明: filter(function or None, sequence) -> list, tuple, or string,包括两个参数,分别是function和list。该函根据function参数返回的结果是否为真来过滤list参数中的项,最后返回一个新列表
例子:过滤掉数组中小于3的数
说明: filter(function or None, sequence) -> list, tuple, or string,包括两个参数,分别是function和list。该函根据function参数返回的结果是否为真来过滤list参数中的项,最后返回一个新列表.
比如,过滤掉数组中小于3的数:
Line 62: Line 63:
}}} }}}输出结果为:
Line 67: Line 68:
-- 清风 [[DateTime(2008-04-25T14:33:00Z)]]
=== dir ===
函式说明:列出一个变量所有方法和属性

动态语言经常遇到的问题就是,当前的变量究竟有哪些可用的方法和属性?通过dir()函式就可以很容易的解决了:
{{{#!python
s="Hello Python"
print dir(s)
}}}
运行结果:
{{{
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__str__', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
}}}
##endInc

status

草稿

清风 & liz; 100%

TableOfContents

PCS205 内建函式(enumerate)

概述

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]

dir

函式说明:列出一个变量所有方法和属性

动态语言经常遇到的问题就是,当前的变量究竟有哪些可用的方法和属性?通过dir()函式就可以很容易的解决了:

   1 s="Hello Python"
   2 print dir(s)

运行结果:

['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__str__', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

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