简体   繁体   English

如何在列表列表中生成随机数

[英]How to generate random numbers in a list of lists

I'm here to ask help for a program that I don't know how to resolve:我在这里为一个我不知道如何解决的程序寻求帮助:

from random import randint
rain=[]

x=randint(0,500)
y=randint(0,500)

r=2
l=[x,y,r]

for k in range(7):
    rain.append(l)
print(rain)

I just want my program to produce random numbers in the rain list but my code produces:我只想让我的程序在雨表中生成随机数,但我的代码会生成:

 [[46, 117, 2],
  [46, 117, 2],
  [46, 117, 2],
  [46, 117, 2],
  [46, 117, 2],
  [46, 117, 2],
  [46, 117, 2]]

You can do it this way:你可以这样做:

from random import randint

rain = list()

r = 2

for k in range(7):
    x = randint(0, 500)
    y = randint(0, 500)
    rain.append([x, y, r])
print(rain)

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

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