简体   繁体   English

numpy 一维数组可以不连续吗?

[英]Can a numpy 1D array not be contiguous?

Is is possible to make a 1D array not C_CONTIGUOUS or F_CONTIGUOUS in numpy?是否可以在 numpy 中制作不是 C_CONTIGUOUS 或 F_CONTIGUOUS 的一维数组?

I think the notion of contiguous only makes sense for arrays with more dimensions but I couldn't find anything in the docs.我认为连续的概念只对具有更多维度的数组有意义,但我在文档中找不到任何东西。

I tried the following to make a non contiguous 1D array:我尝试了以下方法来制作一个非连续的一维数组:

>>> np.empty(10).flags
  C_CONTIGUOUS : True
  F_CONTIGUOUS : True
  OWNDATA : True
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False
>>> np.empty(10).copy('F').flags
  C_CONTIGUOUS : True
  F_CONTIGUOUS : True
  OWNDATA : True
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False

Just create a view of the array that skips over some of the elements and it will be non-contiguous:只需创建一个跳过某些元素的数组视图,它将是不连续的:

In [2]: a = np.arange(10)                                                                     

In [3]: a.flags                                                                               
Out[3]: 
  C_CONTIGUOUS : True
  F_CONTIGUOUS : True
  OWNDATA : True
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False

In [4]: a[::2].flags                                                                          
Out[4]: 
  C_CONTIGUOUS : False
  F_CONTIGUOUS : False
  OWNDATA : False
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False

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

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