Python可迭代地图,范围等

时间:2011-03-07 19:38:18

标签: python

为什么python没有__future__库替换map imaprange替换xrange等?是否可以写一个?

3 个答案:

答案 0 :(得分:8)

自Python 2.6以来有future_builtins个模块:

from future_builtins import filter, map, zip

答案 1 :(得分:4)

无需__future__导入:

from itertools import imap as map
range = xrange

修改:如果你想在一行中完成其中几个,你可以

from itertools import imap as map, ifilter as filter, izip as zip
range = xrange; input = raw_input

答案 2 :(得分:2)

如果您想这样做,为什么不将以下内容添加到PYTHONSTARTUP文件中?

import itertools
filter = itertools.ifilter
map    = itertools.imap
slice  = itertools.islice
zip    = itertools.izip
range  = xrange
input  = raw_input