Python提供了提供map/filter类型功能的列表理解。我能用这个做一个flatMap,也就是绑定操作吗?我见过迭代工具或其他附加库的解决方案。我可以用核心Python来完成这个任务吗?
# this
[[x,10*x] for x in [1,2,3]]
# will result in unflattened [[1, 10], [2, 20], [3, 30]]
发布于 2014-01-28 23:01:21
[y for x in [1, 2, 3] for y in [x, 10*x]]
只需在列表理解中添加另一个for
即可。
发布于 2021-01-22 00:35:56
见python list comprehensions; compressing a list of lists?。
from functools import reduce
def flatMap(array: List[List]): List
return reduce(list.__add__, array)
# or return reduce(lambda a,b: a+b, array)
https://stackoverflow.com/questions/21418764
复制相似问题