what is this saying in python
map(tuple, map(lambda row: [float(row[0]), int(row[1]), parse(row[2])], res))
Can someone help me with the syntax here? I'm trying to understand specifically开发者_开发技巧 what tuple
and lambda
is referring to.
If it's easier to follow you could rewrite this a few times, from
map(tuple, map(lambda row:
[float(row[0]), int(row[1]), parse(row[2])], res))
to
map(lambda row: (float(row[0]), int(row[1]), parse(row[2])), res)
to
[(float(row[0]), int(row[1]), parse(row[2])) for row in res]
That doesn't really answer your question, but I thought it was easier to read ;)
tuple()
is a constructor for a "tuple" object, that can convert a list (or other sequence object) to a tuple.
For example:
>>> a = [1, 2, 3]
>>> a
[1, 2, 3]
>>> tuple(a)
(1, 2, 3)
When used in your example, it converts the result of each lambda
expression from a list to a tuple. It seems a bit redundant, because the following should be equivalent:
map(lambda row: (float(row[0], int(row[1], parse(row[2])), res)
Note the use of ()
parentheses instead of []
square brackets which creates a tuple rather than a list.
精彩评论