如何使这个lambda在Python3中正常工作?

时间:2017-08-13 14:43:09

标签: python lambda syntax-error python-3.6

为什么这段代码:

import math

def nearest_location(locations, my_location):
    return min(enumerate(locations), key=lambda (_, (x, y)): math.hypot(x - my_location[0], y - my_location[1]))

locations = [(41.56569, 60.60677), (41.561865, 60.602895), (41.566474, 60.605544), (41.55561, 60.63101), (41.564171, 60.604020)]
my_location = (41.565550, 60.607537)

print(nearest_location(locations, my_location))

抛出错误:

  

python 3

中不支持元组参数解包

  

SyntaxError:语法无效

当我在Python 3.6上运行它时?

我自己试图修复它,但我仍然没有得到它...有人可以帮忙解决它吗?

1 个答案:

答案 0 :(得分:1)

您无法在python-3.x中解包bit in_range; @(posedge clk iff req); fork begin in_range = 0; @(posedge clk iff ack) if (in_range) $display("passes"); else $display("fails"); end begin repeat(3) @(posedge clk) ; in_range <='1; wait(0); end begin repeat(5) @(posedge clk); $display("fails"); end join_any disable fork; 的参数。虽然他们仍然可以接受多个参数(即lambda),但您不能再解包一个参数(即lambda x, y: x+y)。

最简单的解决方案是仅仅索引&#34;一个参数&#34;而不是使用解包:

lambda (x, y): x+y
相关问题