Python - 如何在末尾生成带有新数字的变量列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19845924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:47:45  来源:igfitidea点击:

Python - how to generate list of variables with new number at end

pythonvariablesauto-generate

提问by SchoonSauce

What I would like to do is find a more concise way of creating variables of empty lists that will be similar to each other, with the exception of different numbers at the end.

我想要做的是找到一种更简洁的方法来创建彼此相似的空列表变量,但最后的数字不同。

#For example:
var1 = []
var2 = []
var3 = []
var4 = []
#...
varN = []

#with the end goal of:
var_list = [var1,var2,var3,var4, ... varN]

回答by vitiral

Use a list comprehension:

使用列表理解

[[] for n in range(N)]

or a simple for loop

或一个简单的 for 循环

empt_lists = []
for n in range(N):
    empt_lists.append([])

Note that [[]]*Ndoes NOT work, it will use the same pointer for all the items!!!

请注意,[[]]*N这不起作用,它将对所有项目使用相同的指针!!!

回答by nbpeth

you can concatenate the variable assignments, if that's what you mean, you can set them all to [] var1, var2, var3 = [] did you want all the variables to be set to an empty array or to actual numbers

您可以连接变量赋值,如果这就是您的意思,您可以将它们全部设置为 [] var1, var2, var3 = [] 您是否希望将所有变量设置为空数组或实际数字

回答by nbpeth

Dynamic variables are considered a really bad practice. Hence, I won't be giving a solution that uses them. :)

动态变量被认为是一种非常糟糕的做法。因此,我不会给出使用它们的解决方案。:)

As @GarrettLinux pointed out, you can easily make a list comprehension that will create a list of lists. You can then access those lists through indexing (i.e. lst[0], lst[1], etc.)

正如@GarrettLinux 指出的那样,您可以轻松地进行列表理解,以创建列表列表。然后,您可以通过索引访问这些列表(即lst[0]lst[1]等)

However, if you want those lists to be named (i.e. var1, var2, etc.), which is what I think you were going for, then you can use a defaultdict from collections.defaultdict:

但是,如果您希望这些列表被命名(即var1,,var2等等),这就是我认为您想要的,那么您可以使用 defaultdict from collections.defaultdict

from collections import defaultdict
dct = defaultdict(list)
for i in xrange(5):
    dct["var{}".format(i+1)]

Demo:

演示:

>>> dct
defaultdict(<type 'list'>, {'var5': [], 'var4': [], 'var1': [], 'var3': [], 'var2': []})
>>> dct["var1"]
[]
>>> dct["var1"].append('value')
>>> dct["var1"]
['value']
>>> dct
defaultdict(<type 'list'>, {'var5': [], 'var4': [], 'var1': ['value'], 'var3': [], 'var2': []})
>>>

As you can see, this solution closely mimics the effect of dynamic variables.

如您所见,此解决方案非常模仿动态变量的效果。

回答by nbpeth

Ok...possible solution is to generate class attributes, which you can use after:

好的...可能的解决方案是生成类属性,您可以在之后使用:

class MyClass:
    pass
myinstance = MyClass()  # if you want instance for example
setattr(myinstance, "randomname", "desiredvalue")
print myinstance.randomname  # wonderfull!

put it in the list if possible in your case!

如果可能的话,把它放在列表中!

回答by Matthew Chachkin

I also had this question. I ended up making a all of my variables and the making a list with the variable names

我也有这个疑问。我最终制作了所有变量并制作了一个带有变量名称的列表

var1 = 123456 
var2 = 123457 
list1 = [var1, var2] 
raninput = input("Words") 
if raninput not in list1: 
  print("Hi")