python lambda映射嵌套列表
我最近在 Hackerrank 中收到了以下问题,我无法回答,并且在其他地方也没有找到正确的答案。
完成 lambda 映射函数: 给定一个数组,例如 [[1,2,3,-1,2],[2,4,-3]] ,取两个数组中所有非负数的平方。输出应该是:
[1,4,9,4]
[4,16]
此外,数组还将在数组的开头包含一个附加数字,即数组的长度。因此,在上面的示例中,长度为 2,因此将通过函数传递的最终数组将为 [2, [1,2,3,-1,2],[2,4,-3]]。 (我们不想取初始数字的平方,在本例中为 2)
def lambdamap(arr):
final = map(
#write your lambda function here
, arr)
return final
在映射中创建一个 lambda 函数来创建所需的输出。
我尝试了多种不同的方法,例如
map(lambda x: x**2, filter(lambda x: x>0, arr))
但这不适用于嵌套列表,例如通过函数传递的数组。 如果我可以提供任何信息来帮助澄清问题,请告诉我。
I recently received the following question in a Hackerrank that I wasn't able to answer and I haven't found the correct answer anywhere else.
Complete the lambda map function:
given a array such as [[1,2,3,-1,2],[2,4,-3]] take the square of all the non-negative numbers in the two arrays. The output is suppose to be:
[1,4,9,4]
[4,16]
Additionally, the array will also contain an additional number at the beginning of the array which is the length of the array. So in the above example, the length is 2 so the final array that will be passed through the function will be [2, [1,2,3,-1,2],[2,4,-3]]. (We don't want to take the square of the initial number, in this case 2)
def lambdamap(arr):
final = map(
#write your lambda function here
, arr)
return final
create a lambda function within the map that creates the desired output.
I have tried a number of different ways such as
map(lambda x: x**2, filter(lambda x: x>0, arr))
But this does not work on nested lists like the array that is passed through the function.
Please let me know if there is any information I can provide to help clarify the question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
虽然我不确定这一点,但听起来输入的长度可能包含在开头,并且需要不受影响地传递。如果是这种情况,这里有一个 lambda 函数,它将接受输入并产生所需的输出:
这部分对非负数进行平方:
[n**2 for n in i if n >; 0]
但前提是该值是一个列表:
if isinstance(i, list)
否则传递该值:
else i
这意味着这个输入
[2, [1, 2, 3, -1, 2], [2, 4, -3]]
返回此输出[2, [1, 4, 9, 4], [ 4, 16]]
While I'm not certain of this, it sounds like the input might have the length included in the beginning and it needs to be passed through untouched. If that's the case, here's a single lambda function that will take the input and produce the desired output:
This portion does the squaring on the non-negatives:
[n**2 for n in i if n > 0]
But only if the value is a list:
if isinstance(i, list)
Otherwise pass the value through:
else i
That means this input
[2, [1, 2, 3, -1, 2], [2, 4, -3]]
returns this output[2, [1, 4, 9, 4], [4, 16]]
您必须在 lambda 函数内使用 lambda 函数。结果不是那么漂亮,但它可以工作:
对于给定的输入情况,输出:
如果你不能使用列表切片,你可以使用
filter(lambda x: isinstance(x, list), arr)
而不是arr[1:]
。让我们从最里面的 lambda 函数开始,然后向外进行。为了简化事情,我们首先考虑如何在名为
x
的单个列表上执行此操作:filter(lambda z: z > 0, x)
为我们提供了所有x
中的正元素。map
对所有这些元素进行平方:map(lambda y: y ** 2, ...)
map(lambda y: y ** 2、过滤器(lambda z: z > 0, x))
。这为我们提供了适用于单个列表的东西。我们如何扩展它以处理列表列表?好吧,每个列表的操作定义方式都是相同的,所以再次使用
map
! (切掉第一个元素,因为它不是列表,并将地图对象转换为列表以匹配所需的输出)。这最终给了我们:
如最初指定的那样。
You have to use a lambda function within a lambda function. The result isn't that pretty, but it works:
With the given input case, this outputs:
If you can't use list slicing, you could use
filter(lambda x: isinstance(x, list), arr)
rather thanarr[1:]
.Let's start at the innermost lambda functions and work our way outwards. To simplify things, we start by considering how to perform this operation on a single list, called
x
:filter(lambda z: z > 0, x)
gives us all the positive elements inx
.map
:map(lambda y: y ** 2, ...)
map(lambda y: y ** 2, filter(lambda z: z > 0, x))
.This gives us something that works for a single list. How do we extend it to work with a list of lists? Well, the operation is defined in the same way for each list, so use
map
again! (slicing off the first element because it isn't a list, and transforming the map objects into lists to match the desired output).This finally gives us:
as specified originally.
为了更好的可读性,我们还可以这样做:
结果
显然,这里的列表理解只是为了查看以列表形式打印的结果。如果结果实际上需要是一个列表,我们可以将前两个
map()
函数包装在lambdamap()
内的list()
函数中>For better readability we can also do:
Outcome
Obviously, the list comprehension here is just to see the outcome printed out as a list. If the outcome needs to actually be a list, we can wrap the first two
map()
functions in alist()
function insidelambdamap()