python - 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)
解决方案
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)
推荐阅读
- html - 在 HTML 视频标签中添加具有不同质量选项的下载按钮
- python - 使用 Cloud Composer 上的 KubernetesPodOperator 通过 Cloud Functions 将变量传递给容器
- ios - React 基于原生的 DatePicker 出现在 iOS 上最小化
- c# - C# 使用字符串数组中的嵌套对象动态创建 JSON
- javascript - JS:如何获取最近日期的对象
- vba - VBA:将 WORD 表导出到 excel
- r - R中的弗里德曼测试
- xpath - 为 Saxonc Xpath 处理器选择 Xpath 版本
- javascript - 在 Jquery 中重复第二类时间
- sql - Postgres 到 Snowflake json 语法