【发布时间】:2017-12-21 03:46:35
【问题描述】:
我为什么要进入 Python2
>>> map(max,[1,2],[3,1])
[3, 2]
在 Python3 中
>>> map(max,[1,2],[3,1])
<map object at 0x10c2235f8>
?
在 Python3 中应该用什么替换 map(max,[1,2],[3,1])?
我读到应该在 Python3 中使用列表推导,但是
>>> [max(i,j) for i in [1,2] for j in [3,1]]
[3, 1, 3, 2]
没有给出想要的结果,想到的变化也没有。
【问题讨论】:
-
@Dark 从技术上讲,不是生成器,只是一个迭代器。
-
list-comp:
[max(i, j) for i, j in zip([1,2], [3,1])]. -
@ekhumoro:谢谢这很优雅。 python 程序员对这两种解决方案(使用列表还是使用 zip 和理解)哪个更合适有意见?
-
@AlexanderKurz。可能有一些讨论相对性能的问题。否则,这实际上只是个人品味或房屋风格的问题(甚至只是在当前代码上下文中“看起来正确”)。
标签: python python-3.x