python - 在 Python 中将函数结果保存为连续的列表名称?
问题描述
我想生成 4 个从 1 到 50 的随机数。我想定义这个随机代码生成器的函数 n 次。并将每个随机 4 个数字保存在一个连续的列表名称中。
def eko():
print(random.sample(range(1, 50), 4))
def eko_times(times):
for i in range(times):eko()
例如,当我输入时,eko_times(2)
我想看到这个结果:
eko_1:[1,43,23,3]
eko_2:[8,32,34,2]
如何将此输出另存为 eko_i 的新列表名称?
解决方案
内联评论
def eko_times(times, low, high, n):
out = [] # list for collecting results
for _ in range(times): # you don't need the value of i
out.append(random.sample(range(low, high+1), n))
return out
现在您可以访问单个结果
>>> results = eko_times(3, 1, 50, 4)
>>> results[0]
[10, 40, 11, 25]
推荐阅读
- python - 如何在 Tkinter GUI 中应用滚动条?
- python - 使用python生成密钥流并对明文进行加密和解密的线性同余生成器
- python - Python - 尝试打印具有与提供的索引匹配的字符串值的每一行
- python - 如何在 PyQt5 QStackWidget 中重新打开我关闭的子窗口?
- python - 我无法在 ffmpeg 中保存动画
- python - 当我通过它运行数组时,为什么我的树节点函数会产生 Null?
- mysql - 重新排序表中的行,然后按此顺序将表保存在mysql数据库中
- django - 如何从 Django HTML 中提取方法?
- java - 无法导入 com.vaadin.data.Binder
- google-bigquery - 如何在 Big Query 中构建用户步骤的封闭漏斗?