[英]sublist parameters are not supported in python 3
不支持子列表参数。 这是代码
def calcEuclideanDist((x1, y1),(x2,y2)):
dist = float(((x2-x1)**2 + (y2-y1)**2)**0.5)
return dist
您不能将函数的 arguments 定义为元组的元素。
请尝试:
def calcEuclideanDist(p1,p2):
# p1, p2 are tuples with at least 2 elements each
dist = float(((p2[0]-p1[0])**2 + (p2[1]-p1[1])**2)**0.5)
return dist
print(calcEuclideanDist((1,2),(3,4))) # output: 2.8284271247461903
def calcEuclideanDist(x1, y1,x2,y2):
dist = float(((x2-x1)**2 + (y2-y1)**2)**0.5)
return dist
x=calcEuclideanDist(1,2,2,3)
print(x)
"((x1, y1),(x2,y2))" 删除这些嵌套的圆括号
为了模拟这一点,您可以有 4 个参数并在调用 function 时使用* unpacking
,
或者接受 2 个元组并在 function 的开头将它们解包。
(这两个选项看起来都不如您的原始代码那么好......)
def calcEuclideanDist4(x1, y1,x2,y2):
dist = float(((x2-x1)**2 + (y2-y1)**2)**0.5)
return dist
def calcEuclideanDist2(p1, p2):
x1, y1 = p1
x2, y2 = p2
dist = float(((x2-x1)**2 + (y2-y1)**2)**0.5)
return dist
p1 = (0,0)
p2 = (3,4)
print(calcEuclideanDist4(*p1,*p2))
print(calcEuclideanDist2(p1,p2))
Output:
5.0
5.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.