Чистая версия вашей проблемы в виде списка:
In [121]: sample_list = [[1, 2, 3], [4, 5, 6]]
...: sample_objects = [["object_1", "object_2", "object_3"], ["object_4", "object_5", "object_6"]]
In [122]: sample_list
Out[122]: [[1, 2, 3], [4, 5, 6]]
In [123]: sample_objects
Out[123]: [['object_1', 'object_2', 'object_3'], ['object_4', 'object_5', 'object_6']]
In [124]: [list(zip(a,b)) for a,b in zip(sample_list, sample_objects)]
Out[124]:
[[(1, 'object_1'), (2, 'object_2'), (3, 'object_3')],
[(4, 'object_4'), (5, 'object_5'), (6, 'object_6')]]
Упаковка в np.array
добавляет немного, если вообще что-то, к этой проблеме. Обратите внимание, что мне пришлось заменить строки для вашего objects
.
sample_objects
, если он действительно содержит «объекты», в то время как object
dtype:
sample_objects = [np.array(["object_1", "object_2", "object_3"],object),
np.array(["object_4", "object_5", "object_6"],object)]
====
Определите пример класса
class MyObj:
def __init__(self,n):
self.n = n
def __repr__(self):
return f'object_{self.n}'
и ваши массивы:
In [141]: sample_list = [np.array([1, 2, 3]), np.array([4, 5, 6])]
...: sample_objects = [np.array([MyObj(n) for n in [1,2,3]]), np.array([MyObj(n) for n in
...: [4,5,6]])]
In [142]: sample_list
Out[142]: [array([1, 2, 3]), array([4, 5, 6])]
In [143]: sample_objects
Out[143]:
[array([object_1, object_2, object_3], dtype=object),
array([object_4, object_5, object_6], dtype=object)]
И список zip способ их объединения:
In [153]: [np.array(list(zip(a,b))) for a,b in zip(sample_list, sample_objects)]
Out[153]:
[array([[1, object_1],
[2, object_2],
[3, object_3]], dtype=object), array([[4, object_4],
[5, object_5],
[6, object_6]], dtype=object)]
и вариант с использованием np.stack
:
In [154]: [np.stack((a,b), axis=1) for a,b in zip(sample_list, sample_objects)]
Out[154]:
[array([[1, object_1],
[2, object_2],
[3, object_3]], dtype=object), array([[4, object_4],
[5, object_5],
[6, object_6]], dtype=object)]
hstack
не чередует их:
array([1, 2, 3, object_1, object_2, object_3], dtype=object)