开发者

what is this saying in python

开发者 https://www.devze.com 2023-04-01 11:32 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消