[英]Python: adding to list of lists both out and in a for loop
I want to create a new list with the 0-indexed value outside a for loop, then add to that same list using a for loop.我想在 for 循环之外创建一个具有 0 索引值的新列表,然后使用 for 循环添加到同一个列表中。 My toy example is:我的玩具示例是:
import random
data = ['t1', 't2', 't3']
masterlist = [['col1', 'animal1', 'animal2', 'animal3']]
animals = ['cat', 'dog', 'chinchilla']
for item in data:
masterlist.append([item])
for animal in animals:
number1 = random.randint(1, 100)
number2 = random.randint(1, 100)
masterlist.append([str(number1) + ',' + str(number2)])
masterlist
but this outputs:但这输出:
[['col1', 'animal1', 'animal2', 'animal3'],
['t1'],
['52,69'],
['8,77'],
['75,66'],
['t2'],
['67,33'],
['85,60'],
['98,12'],
['t3'],
['60,34'],
['25,27'],
['100,25']]
My desired output is:我想要的输出是:
[['col1', 'animal1', 'animal2', 'animal3'],
['t1', '52,69', '8,77', '75,66'],
['t2', '67,33', '85,60', '98,12'],
['t3', '60,34', '25,27', '100,25']]
What about this solution which refactors your code a bit?这个稍微重构你的代码的解决方案怎么样?
import random
data = ['t1', 't2', 't3']
masterlist = [['col1', 'animal1', 'animal2', 'animal3']]
animals = ['cat', 'dog', 'chinchilla']
for item in data:
rv = []
rv.append(item)
for animal in animals:
number1 = random.randint(1, 100)
number2 = random.randint(1, 100)
rv.append(str(number1) + ',' + str(number2))
masterlist.append(rv)
Output:输出:
>>> masterlist
[['col1', 'animal1', 'animal2', 'animal3'],
['t1', '88,43', '85,62', '84,21'],
['t2', '44,99', '32,54', '83,50'],
['t3', '82,87', '90,83', '91,84']]
Alternatively, the following would give the same result.或者,以下将给出相同的结果。
import random
data = ['t1', 't2', 't3']
masterlist = [['col1', 'animal1', 'animal2', 'animal3']]
animals = ['cat', 'dog', 'chinchilla']
for item in data:
masterlist.append([item] + [f"{random.randint(1, 100)},{random.randint(1, 100)}" for animal in animals])
You can do something like this.你可以做这样的事情。 Use a temp
list to store items of each iteration.使用temp
列表来存储每次迭代的项目。 Moreover you were using to many square brackets while appending, this caused the too nested list.此外,您在追加时使用了许多方括号,这导致列表嵌套过多。
import random
data = ['t1', 't2', 't3']
masterlist = [['col1', 'animal1', 'animal2', 'animal3']]
animals = ['cat', 'dog', 'chinchilla']
for item in data:
temp = []
temp.append(item)
for animal in animals:
number1 = random.randint(1, 100)
number2 = random.randint(1, 100)
temp.append(str(number1) + ',' + str(number2))
masterlist.append(temp)
print(masterlist)
You can also use list comprehension as follows:您还可以使用列表理解如下:
import random
data = ['t1', 't2', 't3']
masterlist = [['col1', 'animal1', 'animal2', 'animal3']]
animals = ['cat', 'dog', 'chinchilla']
for d in data:
rv = [d] + [str(random.randint(1, 100)) + ',' + str(random.randint(1, 100)) for a in animals]
masterlist.append(rv)
Or even more hardcore in a single list comprehension:或者在单个列表理解中更加核心:
masterlist+= [[d] + [str(random.randint(1, 100)) + ',' + str(random.randint(1, 100)) for a in animals] for d in data]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.