flatMap or bind in Python 3?

廉价感情. 提交于 2019-12-30 09:21:54

问题


Python provides list comprehensions that provide map/filter type functionality. Can I do a flatMap aka bind operation with this? I've seen solutions with itertools or other add-on libraries. Can I do this with core Python?

# this
[[x,10*x] for x in [1,2,3]]
# will result in unflattened [[1, 10], [2, 20], [3, 30]]

回答1:


[y for x in [1, 2, 3] for y in [x, 10*x]]

Just add another for to the list comprehension.



来源:https://stackoverflow.com/questions/21418764/flatmap-or-bind-in-python-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!