我有一个列表 A
和一个函数 f
,它接受一个 A
项并返回一个列表。我可以使用一个列表内涵来转换 A
中的所有内容,比如 [f(a) for a in A]
,但这会返回一个列表列表。假设我的输入是 [a1,a2,a3]
,结果是 [[b11,b12],[b21,b22],[b31,b32]]
。
我怎样才能得到 被压扁了列表 [b11,b12,b21,b22,b31,b32]
而不是?换句话说,在 Python 中,如何在函数式编程语言中获得传统上称为 flatmap
的内容,或者在。NET?
(在实际代码中,A
是一个目录列表,而 f
是 os.listdir
。我想建立一个子目录的平面列表。)
另请参阅: 如何从列表列表中制作一个平面列表?,了解在创建列表之后将列表展开的更一般的问题。