首页 > 解决方案 > Generating and printing random multiple lists in python

问题描述

I want to generate multiple random lists and print them. How shall I append each list uniquely with every loop? The code below doesn't append the list that is being generated with the empty lists. It simply prints the empty lists.

import numpy as np
a,b,c,d,e=[],[],[],[],[]
for i in range(0,4):
    j=np.random.randint(0,15,size=7)
    [a,b,c,d,e].append(j)
print(a,b,c,d,e)

标签: pythonlistrandom

解决方案


a,b,c,d,e=[],[],[],[],[]
# ...
[a,b,c,d,e].append(j)

This does not change a, b, c, d, e.

It creates a new list containing a, b, c, d, e, appends j to it, and then discards it.

To append j to each of a, b, c, d, e, use a for loop:

for lst in [a,b,c,d,e]:
    lst.append(j)

推荐阅读