简体   繁体   English

如何更新python中的变量列表?

[英]How to update a list of variables in python?

This is an aggravating issue that I've come into whilst programming in python.这是我在使用 python 编程时遇到的一个严重问题。 The user can append various variables to a list however, this list when accessed later on will still display the same results as when first assigned.用户可以将各种变量附加到列表中,但是,稍后访问此列表时仍将显示与首次分配时相同的结果。 eg below例如下面

a=1
b=2
list = [a,b] #user defined order
print list # 1,2
a=2
print list # prints 1,2

I need list to print out 2,2.我需要列表打印出 2,2。 However i cannot find out a way to DYNAMICALLY update the list to accommodate ANY order of the variables (many ways are hard coding which i've seen online such as assigning list = [a,b] when needed to update however i dont know if its b,a or a,b)但是我找不到动态更新列表以适应任何变量顺序的方法(许多方法是硬编码,我在网上看到过,例如在需要更新时分配 list = [a,b] 但是我不知道是否它的 b,a 或 a,b)

Help would be much appreciated.帮助将不胜感激。 Thank you谢谢

Edit : My question is different due to being about varaibles that need to be dynamically updated in a list rather than simply changing a list item.编辑:我的问题是不同的,因为关于需要在列表中动态更新而不是简单地更改列表项的变量。

you need to update the list and not the variable:您需要更新列表而不是变量:

a = 1
b = 2
lst = [a, b]  # the list now stores the references a and b point to
              # changing a later on has no effect on this!  
lst[0] = 2

and please do not use list as variable name!并且请不要使用list作为变量名! this is a keyword built-in type in python and overwriting it is a very bad idea!这是python中的 关键字 内置类型,覆盖它是一个非常糟糕的主意!

if you only know the value of the element in the list (and this value is unique) you could do this:如果您只知道列表中元素的值(并且该值是唯一的),您可以这样做:

old_val = 2
new_val = 3

lst[lst.index(old_val)] = new_val

It's not possible if you store immutable items in a list, which Python integers are, so add a level of indirection and use mutable lists:如果将不可变项存储在列表中是不可能的,Python 整数就是这样,因此添加一个间接级别并使用可变列表:

a,b,c,d = [1],[2],[3],[4]
L = [d,b,a,c] # user-defined order
print L
a[0] = 5
print L

Output:输出:

[[4], [2], [1], [3]]
[[4], [2], [5], [3]]

This has the feel of an XY Problem , however.然而,这有一种XY Problem的感觉。 Describing the problem you are solving with this solution may elicit better solutions.用这个解决方案描述你正在解决的问题可能会得到更好的解决方案。

Why don't you use dictionary??为什么不用字典??

With dictionary:带字典:

_dict = {}
_dict["a"] = 1
_dict["b"] = 2
print _dict["a"] # prints 1

Now if you want to set and get value of variable "a":现在,如果您想设置并获取变量“a”的值:

_dict["a"] = 2
print _dict["a"] # prints 2

At the assignment 'a=1' an integer object is created with the value 1. That object is added to your list.在赋值 'a=1' 处创建一个值为 1 的整数对象。该对象被添加到您的列表中。 At the second assignment (a=2) a new object is created, but the old one still is in the list, its value unchanged.在第二次赋值 (a=2) 处创建了一个对象,但旧对象仍在列表中,其值不变。 That is what you see.这就是你所看到的。

To change this behaviour, the value of the object must be changed.要更改此行为,必须更改对象的值。 Ints are immutable in Python, meaning that exactly this, ie changing the value, is not possible. Ints 在 Python 中是不可变的,这意味着完全不可能,即更改值。 You'll have to define your own and then change its value.您必须定义自己的值,然后更改其值。 For example:例如:

class MyInt(object):

    def __init__(self, value):
        self.setValue(value)

    def setValue(self, value):
        self.value = value

    def __repr__(self):
        return str(self.value)

a=MyInt(1)
print(dir(a))
b=2
list = [a,b] #user defined order
print (list) # 1,2
a.setValue(2)
print (list) # prints 2,2

It is not clean but a work around trick , you can assign the value of a is a single element list它不干净,而是一种解决方法,您可以将 a 的值分配为单个元素列表

a = [1]
b = 2

So when you assign them to a list:因此,当您将它们分配给列表时:

lst = [a,b]

and change the value of a by calling its 0th element:并通过调用其第 0 个元素来更改 a 的值:

a[0] = 2

it will also change the referenced list that is a它还将更改引用列表

print(lst)
#[[2],2]

likewise if you place the value in the list:同样,如果您将值放在列表中:

lst[0][0] = 2
print(a[0])
#2

You can define a function:\您可以定义一个函数:\

a, b = 1, 2

def list_():
   return [a, b]

list_() # [1, 2]

a = 2
list_() # [2, 2]

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

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