[英]How to make a 2d array of tuples in python?
I want to make a 2D array of 2-tuples of fixed dimension (say 10x10).我想制作一个固定维度(比如 10x10)的 2 元组的二维数组。
eg例如
[[(1,2), (1,2), (1,2)],
[(1,2), (1,2), (1,2)],
[(1,2), (1,2), (1,2)]]
There are also two ways that I'd like to generate this array:我还想通过两种方式生成此数组:
How would I go about doing this?我该怎么做呢? For #1 I tried using numpy.tiles
:对于#1,我尝试使用numpy.tiles
:
>>> np.tile(np.array([1,2]), (3, 3))
array([[1, 2, 1, 2, 1, 2],
[1, 2, 1, 2, 1, 2],
[1, 2, 1, 2, 1, 2]])
But I can't seem to copy it across columns, the columns are just concatenated.但我似乎无法跨列复制它,这些列只是连接在一起。
ie instead of:即而不是:
[[[1,2], [1,2], [1,2]],
[[1,2], [1,2], [1,2]],
[[1,2], [1,2], [1,2]]]
you can use numpy.full :你可以使用numpy.full :
numpy.full((3, 3, 2), (1, 2))
output:输出:
array([[[1, 2],
[1, 2],
[1, 2]],
[[1, 2],
[1, 2],
[1, 2]],
[[1, 2],
[1, 2],
[1, 2]]])
for <1> you can generate like this对于 <1> 你可以这样生成
[[(1,2)] * 3]*3
# get [[(1, 2), (1, 2), (1, 2)], [(1, 2), (1, 2), (1, 2)], [(1, 2), (1, 2), (1, 2)]]
numpy.zeros((3,3,2))
我想会起作用(但它不是它的列表的元组......)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.