Trying to use a variable as an index but it won’t work.

huangapple go评论51阅读模式
英文:

Trying to use a variable as an index but it won't work

问题

在Python中,我正在尝试让'number'指向与'x'在列表中完全相同的数字(这可能听起来很愚蠢,但这是我尝试实现的简化版本)。

但是,由于某种原因,索引假定'x'仍然是1,而不是2(在'x += 1'之后它确实是2)。
英文:

In Python I'm trying to make it so that 'number' points to the exact same number in the list as x (this probably sounds stupid but its a simplified version of what I'm trying to accomplish).

But, for some reason the index assumes x is still 1 instead of 2 (which it is after x += 1)

list = [0, 1, 2, 3, 4]

x = 1

number = list[x]

print(number)
x += 1

print(number)
print(x)

Anyone know what I'm doing wrong?

I'm pretty new to all of this.

I expect an output of 1 2 2 but I got 1 1 2

答案1

得分: 1

在这个示例中,我们确实增加了变量 x 的值,但变量 number 仍然包含1。我们必须重新分配它以获得值2。

代码:

listOfNumbers = [0, 1, 2, 3, 4]

value = 1

matchingNumber = listOfNumbers[value]

print(matchingNumber)
value += 1
matchingNumber = listOfNumbers[value]
print(matchingNumber)
print(value)

输出:

1
2
2

注意:请不要将 list 作为变量名,因为它会遮蔽内置名称 list

英文:

In this, we do increment the value of x, but the variable number still have 1 in it. We have to reassign it again to get the value 2.

Code :

listOfNumbers = [0, 1, 2, 3, 4]

value = 1

matchingNumber = listOfNumbers[value]

print(matchingNumber)
value += 1
matchingNumber = listOfNumbers[value]
print(matchingNumber)
print(value)

Output :

1
2
2

NB: Please do not use list as a variable name, because it overshadows the built-in name list.

答案2

得分: 1

lst = [0, 1, 2, 3, 4] # 不要使用"list"作为变量名

x = 1

number = lst[x] # number从lst[1]获取了值

print(number)

x += 1 # x增加了1

number = lst[x] # 添加这行代码以改变number,现在它是lst[2]

print(number)
print(x)

#输出
1
2
2

英文:
lst = [0, 1, 2, 3, 4]  # do not use list as a variable name

x = 1

number = lst[x]   # number got the value 1 from lst[1]

print(number)

x += 1           # x is incremented by 1

number = lst[x]  # add this line to change number, now it is lst[2]

print(number)
print(x)

#output
1
2
2

答案3

得分: 1

以下是翻译的内容:

正如其他人指出的,重新分配 x 不会自动重新分配 number

一般来说,拥有需要同时重新分配的两个变量是迷惑自己的简单方法 - 如果您在更新一个后忘记更新另一个,您很可能在程序中出现错误,然后需要四处寻找以确定这两者何时分离。

通常更好的方法是拥有一个单一的值(单一的“真实来源”),然后使用一个可以自动计算相关值的函数。在面向对象编程上下文中,您通常会通过定义一个 @property 来实现这一点,但您也可以使用一个普通的函数来做到这一点:

numbers = [0, 1, 2, 3, 4]

x = 1
def number():
    return numbers[x]

print(number())
x += 1

print(number())
print(x)

打印:

1
2
2

请注意,由于 number 是一个函数,您需要调用它(即 number())以获取所需的值。调用函数会导致重新评估 numbers[x],从而为您提供了更新后的 number 值。

英文:

As others have pointed out, reassigning x will not automatically reassign number.

In general, having two variables that need to be reassigned at the same time is an easy way to confuse yourself -- if you ever forget to update one after updating the other, you're likely to have a bug in your program, and then you need to hunt all over the place to figure out at what point the two drifted apart.

A better approach is usually to have a single value (a single "source of truth") and then use a function that will automatically calculate the associated value from it. In an OOP context you'd frequently do this by defining a @property, but you can also do it with a plain old function:

numbers = [0, 1, 2, 3, 4]

x = 1
def number():
    return numbers[x]

print(number())
x += 1

print(number())
print(x)

prints:

1
2
2

Note that since number is a function, you need to call it (i.e. number()) to get the desired value. Calling the function is what causes numbers[x] to be re-evaluated, giving you the updated value for number.

答案4

得分: 0

更改 x 在分配 number 后不会改变 number。 为了得到预期的结果,您需要在 x 更改后重新分配 number

list_num = [0, 1, 2, 3, 4]
x = 1
number = list_num[x]
print(number)
x += 1
number = list_num[x]
print(number)
print(x)

编辑:您不应该将 list 用作变量名。

英文:

Changing x after assigning number won't change number. For the intended result, you would have to reassign number after the x change:

list_num = [0, 1, 2, 3, 4] 
x = 1
number = list_num[x] 
print(number) 
x += 1 
number = list_num[x]
print(number) 
print(x)

EDIT: you should not use list as a variable name

答案5

得分: 0

这是代码:

lst = [0, 1, 2, 3, 4]

x = 1

number = lst[x]  # 将索引为1的值(1)分配给number
print(number)  # 输出:1

x += 1  # 将x的值增加1

number = lst[x]  # 使用更新后的索引2(2)重新分配number
print(number)  # 输出:2

print(x)  # 输出:2(x已增加)

希望这对你有所帮助。

英文:

here is the code:

lst = [0, 1, 2, 3, 4]

x = 1

number = lst[x]  # Assign the value at index 1 (1) to number
print(number)  # Output: 1

x += 1  # Increment the value of x by 1

number = lst[x]  # Reassign number with the updated value at index 2 (2)
print(number)  # Output: 2

print(x)  # Output: 2 (x has been incremented)

huangapple
  • 本文由 发表于 2023年5月21日 22:24:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76300380.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定