用随机数创建二维数组的简单方法(Python)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24108417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple way of creating a 2D array with random numbers (Python)
提问by user46242
I know that an easy way to create a NxN array full of zeroes in Python is with:
我知道在 Python 中创建一个充满零的 NxN 数组的简单方法是:
[[0]*N for x in range(N)]
However, let's suppose I want to create the array by filling it with random numbers:
但是,假设我想通过用随机数填充数组来创建数组:
[[random.random()]*N for x in range(N)]
This doesn't work because each random number that is created is then replicated N times, so my array doesn't have NxN unique random numbers.
这不起作用,因为创建的每个随机数都被复制了 N 次,所以我的数组没有 NxN 唯一的随机数。
Is there a way of doing this in a single line, without using for loops?
有没有办法在一行中做到这一点,而不使用 for 循环?
回答by DSM
You could use a nested list comprehension:
您可以使用嵌套列表理解:
>>> N = 5
>>> import random
>>> [[random.random() for i in range(N)] for j in range(N)]
[[0.9520388778975947, 0.29456222450756675, 0.33025941906885714, 0.6154639550493386, 0.11409250305307261], [0.6149070141685593, 0.3579148659939374, 0.031188652624532298, 0.4607597656919963, 0.2523207155544883], [0.6372935479559158, 0.32063181293207754, 0.700897108426278, 0.822287873035571, 0.7721460935656276], [0.31035121801363097, 0.2691153671697625, 0.1185063432179293, 0.14822226436085928, 0.5490604341460457], [0.9650509333411779, 0.7795665950184245, 0.5778752066273084, 0.3868760955504583, 0.5364495147637446]]
Or use numpy
(non-stdlib but very popular):
或者使用numpy
(非标准库但非常流行):
>>> import numpy as np
>>> np.random.random((N,N))
array([[ 0.26045197, 0.66184973, 0.79957904, 0.82613958, 0.39644677],
[ 0.09284838, 0.59098542, 0.13045167, 0.06170584, 0.01265676],
[ 0.16456109, 0.87820099, 0.79891448, 0.02966868, 0.27810629],
[ 0.03037986, 0.31481138, 0.06477025, 0.37205248, 0.59648463],
[ 0.08084797, 0.10305354, 0.72488268, 0.30258304, 0.230913 ]])
(P.S. It's a good idea to get in the habit of saying list
when you mean list
and reserving array
for numpy ndarray
s. There's actually a built-in array
module with its own array
type, so that confuses things even more, but it's relatively seldom used.)
(PS 习惯于说list
当你的意思list
并保留array
给 numpy ndarray
s是一个好主意。实际上有一个内置array
模块有自己的array
类型,所以更容易混淆,但它相对很少使用。)
回答by timgeb
Just use [random.random() for i in range(N)]
inside your list comprehension.
只需[random.random() for i in range(N)]
在您的列表理解中使用。
Demo:
演示:
>>> import random
>>> N = 3
>>> [random.random() for i in range(N)]
[0.24578599816668256, 0.34567935734766164, 0.6482845150243465]
>>> M = 3
>>> [[random.random() for i in range(N)] for j in range(M)]
[[0.9883394519621589, 0.6533595743059281, 0.866522328922242], [0.5906410405671291, 0.4429977939796209, 0.9472377762689498], [0.6883677407216132, 0.8215813727822125, 0.9770711299473647]]
回答by Sunjay Varma
You can use list comprehensions.
您可以使用列表推导式。
[[random.random() for x in xrange(N)] for y in xrange(N)]
https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions
https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions
For large multi dimensional arrays, I suggest you use numpy though.
对于大型多维数组,我建议您使用 numpy。
回答by seakyourpeak
It can be done without a loop. Try this simple line of code for generating a 2 by 3 matrix of random numbers with mean 0 and standard deviation 1.
它可以在没有循环的情况下完成。试试这行简单的代码,生成一个 2 x 3 的随机数矩阵,均值为 0,标准差为 1。
The syntax is :
语法是:
import numpy
numpy.random.normal(mean, standard deviation, (rows,columns))
example :
例子 :
numpy.random.normal(0,1,(2,3))
回答by Ra Kuz
This is how you create a 2d array:
这是创建二维数组的方式:
k = np.random.random ([3,4]) * 10
k.astype(int)