[英]Pyhton 3.9 How to update a list from an imported python file
list.py列表.py
value1 = "a"
value2 = "b"
value3 = "c"
list_of_values = [value1, value2, value3]
main.py主文件
import list
list.value1 = "d"
print(list.list_of_values)
this returns ["a","b","c"] when I want it o return ["d","b","c"], how do I update list_of_values in list.py so that when it is accessed in main.py it returns the proper values for value1, value2 and value3当我想要它时返回 ["a","b","c"] o 返回 ["d","b","c"],如何更新 list.py 中的 list_of_values 以便在访问时在 main.py 中,它返回 value1、value2 和 value3 的正确值
Any help would be greatly appreciated.任何帮助将不胜感激。
You can use:您可以使用:
import list进口清单
list.list_of_values [0]= "d" list.list_of_values [0] = "d"
print(list.list_of_values)打印(list.list_of_values)
Here we are updating the element of the list direct.这里我们直接更新列表的元素。 Additionally I would suggest you to not use list as the file name as it may lead to confusion as it is already a keyword in python.此外,我建议您不要使用 list 作为文件名,因为它可能会导致混淆,因为它已经是 python 中的关键字。
So you generally don't want to name your files after reserved keywords like list.因此,您通常不希望以 list 等保留关键字命名文件。 The other comments below your post mentioning the immutability of references are also important to note.您帖子下方的其他评论提到引用的不变性也很重要。
As I mentioned in my comment, I believe what you want is:正如我在评论中提到的,我相信你想要的是:
list1.py列表1.py
value1 = "a"
value2 = "b"
value3 = "c"
list_of_values = [value1, value2, value3]
main1.py main1.py
import list1
list1.list_of_values[0] = "d"
print(list1.list_of_values)
Output: Output:
['d', 'b', 'c']
Assuming that you do not need to achieve the output by changing value1假设你不需要通过改变value1来实现output
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.