我无法更新数组中的变量

时间:2019-07-16 08:35:04

标签: python arrays python-3.x

我想更新数组中的变量。像katsayi_adam等,但是我无法更改变量。

control = []
CezaSayac = 0
i=0 
katsayi_adam = 1
katsayi_nadam = 1
katsayi_adamax = 1
katsayilar = [katsayi_adam,katsayi_nadam,katsayi_adamax]
a=0
for i in range(3):
    if(1==1):
        control.append(1)
        CezaSayac += 1
    else:
        control.append(0)
for a in range(3):
    if control[a] == 1:
        katsayilar[a] = katsayilar[a] + 1/67
    else:
        katsayilar[a] = katsayilar[a]- ((CezaSayac)/67)/(67-CezaSayac)
print(control)
print(katsayi_adam)
print(katsayi_nadam)
print(katsayi_adamax)

输出总是这样:

[1, 1, 1]

1

1

1

5 个答案:

答案 0 :(得分:0)

我不知道您想要什么,但是我发现了一个错误,并且已经提到了。如果您想了解有关查询的更多信息,请告诉我您想要的脚本详细信息

control = []
CezaSayac = 0
i=0 
katsayi_adam = 1
katsayi_nadam = 1
katsayi_adamax = 1
katsayilar = [katsayi_adam,katsayi_nadam,katsayi_adamax]
a=0
for i in range(3):
    if(i==1):  # use i instead of one..if(i==1):
        control.append(1)
        CezaSayac += 1
    else:
        control.append(0)
for a in range(3):
    if control[a] == 1:
        katsayilar[a] = katsayilar[a] + 1/67
    else:
        katsayilar[a] = katsayilar[a]- ((CezaSayac)/67)/(67-CezaSayac)
print(control)
print(katsayi_adam)
print(katsayi_nadam)
print(katsayi_adamax)

答案 1 :(得分:0)

您需要将分子或分母设置为数据类型float

所以代替:

katsayilar[a] = katsayilar[a] + 1/67

应该是:

katsayilar[a] = katsayilar[a] + float(1)/67

katsayilar[a] = katsayilar[a] + 1/float(67)

答案 2 :(得分:0)

katsayilar是由值而不是引用传递的前提元素(int)的列表。因此,当您更新katsayilar时,您只会更新列表元素,而不会更新创建列表时分配给它的元素,即,您没有更新katsayi_adam, katsayi_nadam, katsayi_adamax

编辑-计算后将列表分配给变量

control = []
CezaSayac = 0
i=0 
katsayi_adam = 1
katsayi_nadam = 1
katsayi_adamax = 1
katsayilar = [katsayi_adam,katsayi_nadam,katsayi_adamax]
a=0
for i in range(3):
    if(1==1):
        control.append(1)
        CezaSayac += 1
    else:
        control.append(0)

for a in range(3):
    if control[a] == 1:
        katsayilar[a] = katsayilar[a] + 1/67
    else:
        katsayilar[a] = katsayilar[a]- ((CezaSayac)/67)/(67-CezaSayac)
print(control)
print(katsayi_adam)
print(katsayi_nadam)
print(katsayi_adamax)
katsayi_adam,katsayi_nadam,katsayi_adamax = katsayilar
print(katsayi_adam)
print(katsayi_nadam)
print(katsayi_adamax)

输出-

[1, 1, 1]
1
1
1
1.0149253731343284
1.0149253731343284
1.0149253731343284

答案 3 :(得分:0)

control = []
CezaSayac = 0

for i in range(3):
    if(1==1):
        control.append(1)
        CezaSayac += 1
    else:
        control.append(0)

katsayi_adam = 1
katsayi_nadam = 1
katsayi_adamax = 1
katsayilar = [1, 1, 1]


for a in range(3):
    if control[a] == 1:
        katsayilar[a] = katsayilar[a] + 1/67
    else:
        katsayilar[a] = katsayilar[a]- ((CezaSayac)/67)/(67-CezaSayac)
katsayi_adam = katsayilar[0]
katsayi_nadam =  katsayilar[1]
katsayi_adamax = katsayilar[2]

print(control)
print(katsayi_adam)
print(katsayi_nadam)
print(katsayi_adamax)

输出为

[1, 1, 1]
1.0149253731343284
1.0149253731343284
1.0149253731343284

使用katsayilar = [katsayi_adam,katsayi_nadam,katsayi_adamax]时  它不会像katsayilar[0] = katsayi_adam一样工作,katsayilar[0]始终等于一个值,您的变量永远不能更新

答案 4 :(得分:0)

在第一个for循环代码中输入错误。您的代码包含:

if (1==1):

表示控制值始终为[1,1,1]。

因此第一个for循环是没有意义的。

第二部分。您要添加值,然后再次删除它们。我想您是故意这样做的,并且您总是想以[1,1,1]结尾,而这实际上是某种难题代码!您要添加1/67,然后在第二个循环中再次将其删除。