Python高级切片 [英] Python advanced slicing

查看:101
本文介绍了Python高级切片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Python的高级切片感到有些困惑.我基本上有一本字典,在SO的帮助下,我将它整理成一个数组.

I am a little confused with Python's advanced slicing. I basically had a dictionary and with help from SO, I made it into an array.

   array1 = 
   ([[[36, 16],
    [48, 24],
    [12,  4],
    [12,  4]],

   [[48, 24],
    [64, 36],
    [16,  6],
    [16,  6]],

   [[12,  4],
    [16,  6],
    [ 4,  1],
    [ 4,  1]],

   [[12,  4],
    [16,  6],
    [ 4,  1],
    [ 4,  1]]])

要使用矩阵求解器进行练习,可使用以下命令将阵列变成正方形矩阵(4 x 4):

To practice using matrix solver, the array was turned into a square matrix (4 x 4) using:

 array_matrix_sized = array[:, :, 0] 

我读到这意味着[索引,行,列的数量].对于[:,:,0]为什么返回4 x 4矩阵,我一点也不了解.为了提供帮助,我制作了一个长度为100的数组,并且一直试图以类似的方式将其转换为10 x 10的矩阵,但没有成功.如果我正确地理解了这个概念,那么让我烦恼的是行数是:",列数是"0".对于4 x 4矩阵,为什么不使用array [:, 4,4]?我还假设:是因为我对所有值都感兴趣.

I read that this means [number of indices, rows, columns]. I am a little clueless as to why [:,:,0] returns a 4 x 4 matrix. To try to help, I made an array that has a length 100, and I have been trying to turn it into a 10 x 10 matrix in a similar manner with no success. What throws me off is the number of rows is ":" and the number of columns is "0", if I read this concept correctly. For a 4 x 4 matrix, why isn't it array[:, 4, 4]? I am also assuming the : is because I am interested in all the values.

在此先感谢您的帮助/建议.如果这是一个简单的问题,我确实表示歉意,但我确实可以对它的工作原理进行澄清.

Thank you in advance for any help/advice. I do apologize if this is a simple question, but I really could use the clarification on how this works.

仍然不太了解. 如果我有

Still not quite understanding. If I have

 array2 = array([  0,   1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,
    13,  14,  15,  16,  17,  18,  19,  20,  21,  22,  23,  24,  25,
    26,  27,  28,  29,  30,  31,  32,  33,  34,  35,  36,  37,  38,
    39,  40,  41,  42,  43,  44,  45,  46,  47,  48,  49,  50,  51,
    52,  53,  54,  55,  56,  57,  58,  59,  60,  61,  62,  63,  64,
    65,  66,  67,  68,  69,  70,  71,  72,  73,  74,  75,  76,  77,
    78,  79,  80,  81,  82,  83,  84,  85,  86,  87,  88,  89,  90,
    91,  92,  93,  94,  95,  96,  97,  98,  99, 100, 101, 102, 103,
   104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116,
   117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129,
   130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142,
   143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155,
   156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168,
   169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181,
   182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194,
   195, 196, 197, 198, 199])

要将其放入10 X 10的矩阵中,我尝试使用array2 [:,:,0]并得到错误IndexError:数组索引过多.这跟我的第一个例子不一样吗?

To get it into a 10 X 10 matrix, I tried using array2[:,:,0] and get the error IndexError: too many indices for array. Isn't this similar to my first example?

推荐答案

我读到这意味着[索引,行,列的数量]. [...]如果我正确地理解了这个概念,那么让我失望的是行数是:",列数是"0".

I read that this means [number of indices, rows, columns]. [...] What throws me off is the number of rows is ":" and the number of columns is "0", if I read this concept correctly.

不.这意味着[我想要在尺寸1上的哪些零件,我想要在尺寸2上的哪些零件,我想要在3维上的哪些零件].索引不是您想要多少行/列,它们是您想要的哪些.而且,正如您所说的,:在此上下文中表示全部".

No. It means [which parts I want on dimension 1, which parts I want on dimension 2, which parts I want on dimension 3]. The indices are not how many rows/columns you want, they are which ones you want. And, as you said : means "all" in this context.

对于4 x 4矩阵,为什么不是array [:, 4,4]?

For a 4 x 4 matrix, why isn't it array[:, 4, 4]?

您未指定结果的形状.结果的形状取决于原始数组的形状.由于您的数组是4x4x2,因此在最后一个维度上获取一个元素将为您提供4x4.如果数组是8x7x2,那么[:, :, 0]会给您8x7的结果.

You don't specify the shape of the result. The shape of the result depends on the shape of the original array. Since your array is 4x4x2, getting one element on the last dimension gives you 4x4. If the array was 8x7x2, then [:, :, 0] would give you an 8x7 result.

所以[:, :, 0]的意思是给我头两个维度上的所有内容,但给我最后一个维度上的第一项.这等于获取每个行"(或出现的第一个列")的第一个元素在显示中),这就是为什么您得到结果的原因:

So [:, :, 0] means "give me everything on the first two dimensions, but only the first item on the last dimension. This amounts to getting the first element of each "row" (or the first "column" as it appears in the display) which is why you get the result you get:

>>> array1[:, :, 0]
array([[36, 48, 12, 12],
       [48, 64, 16, 16],
       [12, 16,  4,  4],
       [12, 16,  4,  4]])

同样,执行[0, :, :]会给您第一个块":

Likewise, doing [0, :, :] gives you the first "chunk":

>>> array1[0, :, :]
array([[36, 16],
       [48, 24],
       [12,  4],
       [12,  4]])

执行[:, 0, :]会给您每个块的第一行:

And doing [:, 0, :] gives you the first row of each chunk:

>>> x[:, 0, :]
array([[36, 16],
       [48, 24],
       [12,  4],
       [12,  4]])

这篇关于Python高级切片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆