简体   繁体   English

如何通过遍历整数和字符串的组合来创建变量?

[英]How can I create variables by iterating through a combination of integers and strings?

So I want to create variables by looping through some sort of name assigning statement or function. 所以我想通过遍历某种名称分配语句或函数来创建变量。 I want to end up having variables titled " t1 , t2 , t3 " etc. How can I accomplish what 我想结束标题为“ t1t2t3 ”等的变量。如何完成

i = 1
while 1:
    ("t" + str(i)) = [0,0,0,0]
    i = i + 1

looks like it would do. 看起来会做。 (Keep the str("t") at the front than make the second character the changing int(i) . This is to create variables to later put in an array without having to type them out in the the code.) I'm trying to make a list of 10,000 lists with 4 elements each. (将str("t")放在最前面,而不是将第二个字符更改为int(i) 。这是为了创建变量,以便稍后将其放入数组中,而不必在代码中键入它们。)尝试制作10,000个列表,每个列表包含4个元素。 Is this more clear? 这更清楚吗?

Don't try. 不要尝试

Instead, use a single dictionary to keep all the values - your titles can be the keys: 而是使用一个字典来保留所有值-您的标题可以是键:

my_dict = {}
for i in range(1, 4):
    my_dict['t%s' % i] = foo

While it is technically possible to do what you want, it's a really bad practice. 尽管从技术上讲可以做您想做的事,但这是一种非常糟糕的做法。 This is exactly the use case that lists (or arrays) were made for, and you should be using one here. 这正是列表(或数组)用于的用例,您应该在这里使用一个。

Based on your code sample, something like 根据您的代码示例,类似

t = [[0] * 4 for i in xrange(10000)]

(as David Alber mentioned in a comment) should work for you; (如大卫·阿尔伯(David Alber)在评论中所述)应该为您工作; it will create a list of 10000 lists of 4 elements each. 它将创建一个包含4个元素的10000个列表的列表。

PS In Python 3, the xrange function has been renamed to range . PS在Python 3中, xrange函数已重命名为range

t = [[0] * 4] * 10000 works, but probably doesn't do what you expect. t = [[0] * 4] * 10000有效,但可能未达到您的期望。 Rather than creating a list of 10000 lists, it will create a list of 10000 references to a single list. 与其创建10000个列表的列表,不如创建一个对单个列表的10000个引用的列表。 Have a look at what happens when you try to start changing elements of the inner lists: 看看当您尝试开始更改内部列表的元素时会发生什么:

>>> t = [[0] * 4] * 3
>>> t
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
>>> t[0][0] = 1
>>> t
[[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]

Instead, the following is probably what you were expecting: 相反,以下可能是您所期望的:

>>> t = [[0] * 4 for _ in xrange(3)]
>>> t
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
>>> t[0][0] = 1
>>> t
[[1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

The reason is that multiplying a list just creates a longer list with multiple references to the elements inside the original list. 原因是将一个列表相乘只会创建一个更长的列表,并带有多个对原始列表内元素的引用。 This is almost never a good idea unless you know the list contains entirely immutable objects (such as numbers and strings); 除非您知道列表包含完全不可变的对象(例如数字和字符串),否则这几乎不是一个好主意。 that's why it's still okay to use list multiplication in [0] * 4 in my version of the answer, even though the outer list multiplication in [[0] * 4] * 10000 is dangerous. 这就是为什么在我的答案版本中仍可以在[0] * 4中使用列表乘法,即使[[0] * 4] * 10000的外部列表乘法很危险。

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

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