简体   繁体   English

如何在python中制作一个二维元组数组?

[英]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:我还想通过两种方式生成此数组:

  1. An array like the example above where every element is the same tuple一个像上面例子一样的数组,其中每个元素都是同一个元组
  2. An array which I populate iteratively with specific tuples (possibly starting with an empty array of fixed size and then using assignment)我用特定元组迭代填充的数组(可能从固定大小的空数组开始,然后使用赋值)

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.

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