一个小故事
三年前,我在一篇博客 里不无自豪的记录了python编写的小函数,当时感觉python真强大,11行代码就写出了一个配置文件的解析器。
1
2
3
4
5
6
7
8
9
10
11
12
13
def loadUserInfo ( fileName ):
userinfo = {}
file = open ( fileName , "r" )
while file :
line = file . readline ()
if len ( line ) == 0 :
break
if line . startswith ( '#' ):
continue
key , value = line . split ( "=" )
userinfo [ key . strip ()] = value . strip ()
return userinfo
最近正在跟同事学习python在数据挖掘中的应用
,又专门学习了一下python本身,然后用list comprehension
简化了以下上面的代码:
1
2
3
4
def loadUserInfo ( file ):
return dict ([ line . strip () . split ( "=" )
for line in open ( file , "r" )
if len ( line ) > 0 and not line . startswith ( "#" )])
这个函数和上面的函数的功能一样,都是读取一个指定的key=value
格式的文件,然后构建出来一个映射
(当然,在Python中叫做字典
)对象,该函数还会跳过空行和#
开头的行。
比如,我想要查看一下.wgetrc
配置文件:
1
2
if __name__ == "__main__" :
print ( loadUserInfo ( "/Users/jtqiu/.wgetrc" ))
假设我的.wgetrc
文件配置如下:
1
2
3
4
http - proxy = 10.18 . 0.254 : 3128
ftp - proxy = 10.18 . 0.254 : 3128
#http_proxy=10.1.1.28:3128
use_proxy = yes
则上面的函数会产生这样的输出:
1
{ 'use_proxy' : 'yes' , 'ftp-proxy' : '10.18.0.254:3128' , 'http-proxy' : '10.18.0.254:3128' }
list comprehension(列表推导式)
在python中,list comprehension
(或译为列表推导式)可以很容易的从一个列表生成另外一个列表,从而完成诸如map
, filter
等的动作,比如:
要把一个字符串数组中的每个字符串都变成大写:
1
2
3
4
5
names = [ "john" , "jack" , "sean" ]
result = []
for name in names :
result . append ( name . upper ())
如果用列表推导式,只需要一行:
1
[ name . upper () for name in names ]
结果都是一样:
1
[ 'JOHN' , 'JACK' , 'SEAN' ]
另外一个例子,如果想要过滤出一个数字列表中的所有偶数:
1
2
3
4
5
6
numbers = [ 1 , 2 , 3 , 4 , 5 , 6 ]
result = []
for number in numbers :
if number % 2 == 0 :
result . append ( number )
如果写成列表推导式:
1
[ x for x in numbers if x % 2 == 0 ]
结果也是一样:
显然,列表推导更加短小,也更加表意。
迭代器
在了解generator
之前,我们先来看一个迭代器
的概念。有时候我们不需要将整个列表都放在内存中,特别是当列表的尺寸比较大的时候。
比如我们定义一个函数,它会返回一个连续的整数的列表:
1
2
3
4
5
6
def myrange ( n ):
num , nums = 0 , []
while num < n :
nums . append ( num )
num += 1
return nums
当我们计算诸如myrange(50)
或者myrange(100)
时,不会有任何问题,但是当获取诸如myrange(10000000000)
的时候,由于这个函数的内部会将数字保存在一个临时的列表中,因此会有很多的内存占用。
因此在python有了迭代器的概念:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class myrange ( object ):
def __init__ ( self , n ):
self . i = 0
self . n = n
def __iter__ ( self ):
return self
# for python 3
def __next__ ( self ):
return self . next ()
def next ( self ):
if self . i < self . n :
i = self . i
self . i += 1
return i
else :
raise StopIteration ()
这个对象其实实现了两个特殊的方法:__iter__
(对于python3来说,是__next__
)和next
方法。其中next
每次只返回一个值,如果迭代已经结束,就抛出一个StopIteration
的异常。实现了这两个方法的类都可以算作是一个迭代器,他们可以被用于可迭代
的上下文中,比如:
1
2
3
4
5
6
7
8
>>> from myrange import myrange
>>> x = myrange ( 10 )
>>> x . next ()
0
>>> x . next ()
1
>>> x . next ()
2
但是可以看到这个函数中有很多的样板代码,因此我们有了生成器表达式来简化这个过程:
1
2
3
4
5
def myrange ( n ):
num = 0
while num < n :
yield num
num += 1
注意此处的yield
关键字,每次使用next
来调用这个函数时都会求值一次num并返回,具体的细节可以参考这里 。
区别
简单来说,两者都可以在迭代器上下文中使用,看起来几乎是一样的。不同的地方是generator
可以节省内存空间,从而提高执行速度。generator
更适合一次性的列表处理,比如只是需要一个中间列表作为转换。而列表推导则更适合要将列表
保存下来,以备后续使用的场景。
这里也有一些讨论 ,可以一并参看。
参考
Iterators & Generators
Generators Wiki
网友评论已有0条评论, 我也要评论