在lambda功能中弄平列表
我正在尝试构建一个lambda表达式,该表达式以:(a,b [],c)
,其中b
是需要的元素列表被扁平。
例如,表达式的输出应为: (a,b [0],c),(a,b [1],c),(a,b [2],c)...
我尝试研究并寻找与我刚刚描述的情况相似的发电机表达方式,但我无法真正缠绕着这个。
如果有人可以将我指向正确的方向,我会很感激。
I am trying to construct a lambda expression which takes in tuples of the form: (a, b[], c)
, where b
is a list of elements which needs to be flattened.
For example, the output of the expression should be:(a, b[0], c), (a, b[1], c), (a, b[2], c) ...
I've tried researching and looking for generator expressions that are similar to the situation I've just described, but I can't really wrap my head around this.
I would appreciate it if someone could point me in the right direction.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
itertools.repeats.repeats.repeat() /a>使用
zip()
为b
中的每个元素创建所需的元组:此输出:
You can use
itertools.repeat()
withzip()
to create the desired tuple for each element inb
:This outputs:
返回列表的lambda函数理解列表长度的参数。
A lambda function returning a list comprehension parametrized by the length of the list.