首页 > 解决方案 > 有没有办法用普通列表实现 numpy 的方法?

问题描述

我正在尝试实现一个将大 numpy 数组分解为n 个数组列表的类。问题是,由于空间的原因,很难使用非常大的数组,所以我试图做的就是将它们拆分成列表。因为列表在存储方式上更加灵活。

我目前拥有的是一个已经在做部分技巧的类:

class Array_list:
  data = list()
  sliced = 0

  def __init__(self, new_list = None):
    self.data = [new_list] if ( type(new_list) == type(list()) ) else list()
    self.sliced = 0
  
  def __getitem__(self, index):
    self.data[index // self.sliced][index % self.sliced]

  def __str__(self):
    return self.data.__str__()

  def __repr__(self):
    return self.data.__repr__()

  def slice(self, n):
    concat, output = [], []
    while (len(self.data) > 0):
      size = 0
      while ((size < n) and (len(self.data) > 0)):
        if (len(self.data[0]) <= (n - size)):
          concat.append(self.data[0])
          size += len(self.data[0])
          del self.data[0]
        else:
          concat.append(self.data[0][:(n - size)])
          size += len(self.data[0])
          self.data.insert(1, self.data[0][(n - size):])
          del self.data[0]
      output.append(np.concatenate(concat))
      concat = list()
    self.data = output
    self.sliced = n

我坚持的部分是处理 numpy 的功能,主要是列提取器和求和,如下所示:

arr = np.array([ [0, 1, 2],
                 [9, 8, 7] ])
print(arr[:, 0])
print(arr[:, [1, 0]])
print(arr + 5)

我需要在我的主代码中使用它们,但我也遇到了数组太大的问题。有人对我可以做什么或搜索什么有任何提示吗?

PS:当谈到 Python 时,我是一个初学者,这也是我在 Stack Overflow 上的第一个问题,所以非常欢迎任何关于任何事情的反馈

标签: pythonarrayslistnumpy

解决方案


推荐阅读