问题
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