-2

For Python 2.7, what is the logic of below lambda expression, confused by this part int(s), it seems no variable called s.

x = [tuple(map(lambda s: int(s), x.split(':'))) for x in y.split(' ')]

thanks in advance, Lin

Lin Ma
  • 9,739
  • 32
  • 105
  • 175

1 Answers1

4

The lambda function was used with map, so the parameters for the lambda are passed from the second argument of map. Understanding how map works will help you understand better how the lambda takes its parameter:

Apply function to every item of iterable and return a list of the results. If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel

So s represents each item from the iterable x.split(':') and int(s) implies an explicit cast of item s to integer, where int(x) is the return object of the lambda.

You may read more about lambda and map

Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139