【问题标题】:How to transform and reshape multiple numpy arrays如何转换和重塑多个 numpy 数组
【发布时间】:2021-04-15 08:59:09
【问题描述】:

我有几个值列表,每个列表都使用 2 个数字命名,例如 values[1][1] 、 values[1][2] 或 values[2][1]....直到 values[99 ][99]。我需要将每个列表转换为一个 numpy 一维数组,然后将每个数组重塑为一个具有维度(20,10)的二维数组。 我能够为以下一个列表执行此操作,但我需要为所有列表执行此操作(我有 99 x 99 =9801 个列表)

array_1_1 = np.array([values[1][1]])
array_1_1.shape

输出:(1, 200)

new_array_1_1 = np.reshape(array_1_1 ,(20,10))
new_array_1_1.shape

输出 : (20, 10) 谢谢

【问题讨论】:

  • 请添加一个 MCVE。几乎可以保证有一种无需循环的快速方法。

标签: python arrays numpy jupyter reshape


【解决方案1】:

下面应该完成这项工作,将所有形状的数组存储到一个名为store的列表中

store = []
for i in range(1,100):
    for j in range(1,100):
        store.append(np.reshape(np.array([values[i][j]]),(20,10)))

【讨论】:

  • 返回 KeyError: 0 :/
  • 尝试 range(98) 而不是 range(99)
  • 在循环中添加 print(i,j) 以查看发生 keyerror 的位置
  • ``` FILENAME_REGEX = re.compile(r'(\d+)dhs(\d+).out') folder_path = 'Output/dhs/' values = {} for f in next(os .walk(folder_path))[2]: m = FILENAME_REGEX.match(f) 如果 m 为无:继续 first_int, second_int = list(map(int, m.groups())) values.setdefault(first_int, {}) [second_int] = gs.DataFile(os.path.join(folder_path, f))['value'] print(values[10][10])```
  • 好的,很奇怪,尝试 range(1,100) 两个循环,如果这不起作用,你需要解释如何根据你的 values[i][j] 来引用这些值定义
猜你喜欢
  • 2022-01-03
  • 2017-08-15
  • 2020-07-17
  • 2020-04-04
  • 1970-01-01
  • 1970-01-01
  • 2017-11-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多