status |
草稿 |
清风; 100% |
概述
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开始。
例如:
0,"a" 1,"b" 2,"c"
map
行者:以一个小例子,展示map的威力,将数组中每一个数乘以2
1 map(lambda x:x*2,[1,2,3,4,5])
行者:同时循环两个一样长的数组
行者:过滤掉,数组中小于3的数
1 filter(lambda x:x>3,[1,2,3,4,5])