简体   繁体   English

python 3 中不支持子列表参数

[英]sublist parameters are not supported in python 3

Sublist parameter is not supported.不支持子列表参数。 and here is the code这是代码

def calcEuclideanDist((x1, y1),(x2,y2)):
    dist = float(((x2-x1)**2 + (y2-y1)**2)**0.5)
    return dist

You can't define function's arguments as elements of tuple.您不能将函数的 arguments 定义为元组的元素。

Please try instead:请尝试:

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))" remove these nested circular brackets "((x1, y1),(x2,y2))" 删除这些嵌套的圆括号

to simulate this, you can either have 4 params and use * unpacking when calling the function,为了模拟这一点,您可以有 4 个参数并在调用 function 时使用* unpacking
or accept 2 tuples and unpack them at the start of the function.或者接受 2 个元组并在 function 的开头将它们解包。
(both options don't look as nice as your original code...) (这两个选项看起来都不如您的原始代码那么好......)

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: Output:

5.0
5.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM