Python(带append的重复值)

Python(带append的重复值),python,append,equation,definition,Python,Append,Equation,Definition,我有下面的公式 Q=[(2*C*D)/H]的平方根 我想输入100150180,所以我想要的输出是18,22,24 所以我的代码是 import math c=50 h=30 value=[] def equation(a1,b1,c1): for i in (a1,b1,c1): value.append(int(math.sqrt(2*c*a1/h))) value.append(int(math.sqrt(2*c*b1/h))) va

我有下面的公式 Q=[(2*C*D)/H]的平方根 我想输入100150180,所以我想要的输出是18,22,24

所以我的代码是

import math
c=50
h=30
value=[]
def equation(a1,b1,c1):
    for i in (a1,b1,c1):
        value.append(int(math.sqrt(2*c*a1/h)))
        value.append(int(math.sqrt(2*c*b1/h)))
        value.append(int(math.sqrt(2*c*c1/h)))
        print (value)
当我输入方程式(100150180)时,为什么输出如下

[18, 12, 24]
[18, 12, 24, 18, 12, 24]
[18, 12, 24, 18, 12, 24, 18, 12, 24]
我如何更改代码,以便只获得

[18, 12, 24]

为什么使用
for
循环

import math
c=50
h=30
def equation(a1,b1,c1):
    value=[]
    value.append(int(math.sqrt(2*c*a1/h)))
    value.append(int(math.sqrt(2*c*b1/h)))
    value.append(int(math.sqrt(2*c*c1/h)))
    print (value)

为什么使用
for
循环

import math
c=50
h=30
def equation(a1,b1,c1):
    value=[]
    value.append(int(math.sqrt(2*c*a1/h)))
    value.append(int(math.sqrt(2*c*b1/h)))
    value.append(int(math.sqrt(2*c*c1/h)))
    print (value)

for循环在这里似乎有点不必要。如果您只是希望函数返回三个数字的列表,可以使用:

import math

c = 50
h = 30


def equation(a1, b1, c1):
    value = []
    value.append(int(math.sqrt(2 * c * a1 / h)))
    value.append(int(math.sqrt(2 * c * b1 / h)))
    value.append(int(math.sqrt(2 * c * c1 / h)))
    print(value)


equation(100, 150, 180)

for循环在这里似乎有点不必要。如果您只是希望函数返回三个数字的列表,可以使用:

import math

c = 50
h = 30


def equation(a1, b1, c1):
    value = []
    value.append(int(math.sqrt(2 * c * a1 / h)))
    value.append(int(math.sqrt(2 * c * b1 / h)))
    value.append(int(math.sqrt(2 * c * c1 / h)))
    print(value)


equation(100, 150, 180)

循环不需要

import math
c=50
h=30
value=[]
def equation(a1,b1,c1):
     value.append(int(math.sqrt(2*c*a1/h)))
     value.append(int(math.sqrt(2*c*b1/h)))
     value.append(int(math.sqrt(2*c*c1/h)))
     print (value)

循环不需要

import math
c=50
h=30
value=[]
def equation(a1,b1,c1):
     value.append(int(math.sqrt(2*c*a1/h)))
     value.append(int(math.sqrt(2*c*b1/h)))
     value.append(int(math.sqrt(2*c*c1/h)))
     print (value)

循环值仅应用相同的公式,在列表理解中,也不打印结果,只返回结果(如果需要,在调用者中打印):

结果:

[18, 22, 24]
(这将保存此类循环/在何处定义返回值错误和大量复制/粘贴)

带有变量参数的变量(相同的调用语法,保存参数打包和解包,因为所有参数都得到相同的处理):


循环值仅应用相同的公式,在列表理解中,也不打印结果,只返回结果(如果需要,在调用者中打印):

结果:

[18, 22, 24]
(这将保存此类循环/在何处定义返回值错误和大量复制/粘贴)

带有变量参数的变量(相同的调用语法,保存参数打包和解包,因为所有参数都得到相同的处理):


在我看来,这就是你所追求的:

import math

c = 50
h = 30

def equation(values):
    return [int(math.sqrt(2*c*i/h)) for i in values]

input = [100, 150, 180]

print(equation(input))
输出:

[18, 22, 24]

在我看来,这就是你所追求的:

import math

c = 50
h = 30

def equation(values):
    return [int(math.sqrt(2*c*i/h)) for i in values]

input = [100, 150, 180]

print(equation(input))
输出:

[18, 22, 24]

我完全不明白你为什么在循环。仅仅删除
for
行,你就不能得到正确的答案吗?我完全不明白你为什么要循环。仅仅删除
行的
,您就不会得到正确的答案吗?