Удалите средний набор скобок внутри numpy.ndarray

#python #numpy

Вопрос:

У меня есть numpy.ndarray, который выглядит так:

 [[['37.7602,-122.4703' '37.7602,-122.4703']
['37.7602,-122.4703' '37.7941,-122.4078']
['37.7602,-122.4703' '37.8156,-122.284']
['37.7602,-122.4703' '37.8163,-122.2467']
['37.7602,-122.4703' '37.7599,-122.4148']
['37.7602,-122.4703' '37.8268,-122.2526']]

[['37.7941,-122.4078' '37.7602,-122.4703']
['37.7941,-122.4078' '37.7941,-122.4078']
['37.7941,-122.4078' '37.8156,-122.284']
['37.7941,-122.4078' '37.8163,-122.2467']
['37.7941,-122.4078' '37.7599,-122.4148']
['37.7941,-122.4078' '37.8268,-122.2526']]]
 

Есть ли эффективный способ удалить средний набор скобок, чтобы он выглядел так:

 [['37.7602,-122.4703' '37.7602,-122.4703']
['37.7602,-122.4703' '37.7941,-122.4078']
['37.7602,-122.4703' '37.8156,-122.284']
['37.7602,-122.4703' '37.8163,-122.2467']
['37.7602,-122.4703' '37.7599,-122.4148']
['37.7602,-122.4703' '37.8268,-122.2526']
['37.7941,-122.4078' '37.7602,-122.4703']
['37.7941,-122.4078' '37.7941,-122.4078']
['37.7941,-122.4078' '37.8156,-122.284']
['37.7941,-122.4078' '37.8163,-122.2467']
['37.7941,-122.4078' '37.7599,-122.4148']
['37.7941,-122.4078' '37.8268,-122.2526']]
 

Я предполагаю, что это можно сделать с помощью цикла, но мне любопытно, есть ли способ сделать это без цикла, так как я хочу, чтобы моя реализация легко масштабировалась.

Спасибо!

Комментарии:

1. Кажется x[0] , стоит это сделать?

2. arr.reshape(-1, arr.shape[2]) . reshape делает то, что ты хочешь.

3. @иван — не совсем. Форма примера такова (2,6,2) , но ее трудно разглядеть из-за пропущенных запятых.

4. @Michaelsczesny Действительно, ты прав

5. Скобки существуют не просто для вида. Они передают информацию о форме массива, которая при подсчете блоков, строк и столбцов выглядит (2,6,2). Из-за того, как хранятся массивы, их легко и эффективно преобразовать в такие, как (12,2) или (2,12)..

Ответ №1:

Просто чтобы формализовать то, что правильно сказал Майкл:

 import numpy as np

array = np.array(
[[['37.7602,-122.4703', '37.7602,-122.4703'],
['37.7602,-122.4703', '37.7941,-122.4078'],
['37.7602,-122.4703', '37.8156,-122.284'],
['37.7602,-122.4703', '37.8163,-122.2467'],
['37.7602,-122.4703', '37.7599,-122.4148'],
['37.7602,-122.4703', '37.8268,-122.2526']],

[['37.7941,-122.4078', '37.7602,-122.4703'],
['37.7941,-122.4078', '37.7941,-122.4078'],
['37.7941,-122.4078', '37.8156,-122.284'],
['37.7941,-122.4078', '37.8163,-122.2467'],
['37.7941,-122.4078', '37.7599,-122.4148'],
['37.7941,-122.4078', '37.8268,-122.2526']]])

print(array)
array = array.reshape(-1,array.shape[2])
print(array)
 

Выход:

 [[['37.7602,-122.4703' '37.7602,-122.4703']
  ['37.7602,-122.4703' '37.7941,-122.4078']
  ['37.7602,-122.4703' '37.8156,-122.284']
  ['37.7602,-122.4703' '37.8163,-122.2467']
  ['37.7602,-122.4703' '37.7599,-122.4148']
  ['37.7602,-122.4703' '37.8268,-122.2526']]

 [['37.7941,-122.4078' '37.7602,-122.4703']
  ['37.7941,-122.4078' '37.7941,-122.4078']
  ['37.7941,-122.4078' '37.8156,-122.284']
  ['37.7941,-122.4078' '37.8163,-122.2467']
  ['37.7941,-122.4078' '37.7599,-122.4148']
  ['37.7941,-122.4078' '37.8268,-122.2526']]]
[['37.7602,-122.4703' '37.7602,-122.4703']
 ['37.7602,-122.4703' '37.7941,-122.4078']
 ['37.7602,-122.4703' '37.8156,-122.284']
 ['37.7602,-122.4703' '37.8163,-122.2467']
 ['37.7602,-122.4703' '37.7599,-122.4148']
 ['37.7602,-122.4703' '37.8268,-122.2526']
 ['37.7941,-122.4078' '37.7602,-122.4703']
 ['37.7941,-122.4078' '37.7941,-122.4078']
 ['37.7941,-122.4078' '37.8156,-122.284']
 ['37.7941,-122.4078' '37.8163,-122.2467']
 ['37.7941,-122.4078' '37.7599,-122.4148']
 ['37.7941,-122.4078' '37.8268,-122.2526']]