Python内置map返回的是列表,而six.moves.map返回的是iter。
>>> map(lambda a: a*2, [1, 2, 3])
[2, 4, 6]
>>> m = six.moves.map(lambda a: a*2, [1, 2, 3])
>>> type(m)
<type 'itertools.imap'>
>>> next(m)
2
>>> next(m)
4
>>> m.next()
6
本文探讨了Python内置函数map与six.moves.map之间的不同之处。前者返回列表类型,而后者返回迭代器类型。通过具体实例展示了两种函数的使用方式及其返回类型的特点。
Python内置map返回的是列表,而six.moves.map返回的是iter。
>>> map(lambda a: a*2, [1, 2, 3])
[2, 4, 6]
>>> m = six.moves.map(lambda a: a*2, [1, 2, 3])
>>> type(m)
<type 'itertools.imap'>
>>> next(m)
2
>>> next(m)
4
>>> m.next()
6
2594

被折叠的 条评论
为什么被折叠?