我想使用地图和/或过滤器功能编写此代码。它返回列表中项目的索引,前提是总和达到目标
我已经为此使用列表理解,但是看不到如何将第二个for循环获取到map / filter函数中。我不确定使用的语法如果我为map / filter函数的function参数定义自己的函数
num = [2,5,7,11,6,15,3,4]
tgt= 9
[num.index(x) for x in num for y in num if x + y == tgt]
Run Code Online (Sandbox Code Playgroud)
结果:
[0, 1, 2, 4, 6, 7]
Run Code Online (Sandbox Code Playgroud)