flatMap还是在Python 3中绑定? [英] flatMap or bind in Python 3?
本文介绍了flatMap还是在Python 3中绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#this
[[x,10 * x] for x in [ 1,2,3]]
#将导致unflattened [[1,10],[2,20],[3,30]]
解决方案
[y for x in [1,2,3] for y in [x ,10 * x]]
只需为$ c $添加另一个 c>列表理解。
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]]
解决方案
[y for x in [1, 2, 3] for y in [x, 10*x]]
Just add another for
to the list comprehension.
这篇关于flatMap还是在Python 3中绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文