flatMap还是在Python 3中绑定? [英] flatMap or bind in Python 3?

查看:146
本文介绍了flatMap还是在Python 3中绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python提供了提供映射/过滤器类型功能的列表解析。我可以用这个做一个flatMap aka绑定操作吗?我已经看到了itertools或其他附加库的解决方案。我可以使用核心Python来做到这一点吗?

 #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>列表理解。


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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆