Python Numpy在数组索引处添加数组
我正在努力解决一些可能很简单或不可能的问题 我想在特定索引处将一个numpy数组添加到另一个numpy数组中Python Numpy在数组索引处添加数组,python,python-3.x,numpy,numpy-ndarray,Python,Python 3.x,Numpy,Numpy Ndarray,我正在努力解决一些可能很简单或不可能的问题 我想在特定索引处将一个numpy数组添加到另一个numpy数组中 a = np.zeros(shape=(17, 1, 2)) for i in range(10): b = [i] c = [1,2,3,4] b.append(c) # Here I want to add b in a at specific index but it's not working # np.append(a[i][0][0], b) 最后我
a = np.zeros(shape=(17, 1, 2))
for i in range(10):
b = [i]
c = [1,2,3,4]
b.append(c)
# Here I want to add b in a at specific index but it's not working
# np.append(a[i][0][0], b)
最后我想要这样的东西:
a = [[[[0, [1,2,3,4]], ....]]]
谢谢你你的例子不太清楚,你没有说到底出了什么问题。例如,您没有在循环中执行任何操作。您还尝试混合列表和数组。 不过,我想我知道你的意思/需要。 您可以对列表使用insert和append,如下例所示:
a = []
for i in range(10):
b = [i]
c = [1,2,3,4]
b.insert(1,c)
a.append( b )
print a
更新
用于在特定索引处插入对象。如果以下内容与您想要的不太接近,您必须更加具体,亲爱的OP
a = np.zeros(shape=(17, 1, 2))
for i in range(10):
b = [i]
c = [1,2,3,4]
b.append(c)
# Here I want to add b in a at specific index but it's not working
# np.append(a[i][0][0], b)
我承认numpy
是一个功能强大的库,但您要求它初始化零,即int
,然后要添加到list
。您不能期望构造函数在创建时知道它需要为对象
类型数据分配空间。您想要的是帮助numpy ndarray构造函数进行类型推断
a = np.zeros(shape=(17, 1, 2), dtype=object)
for i in range(10):
b = [i]
c = [1,2,3,4]
b.append(c)
a[i] = b
a
#array([[[0, [1, 2, 3, 4]]],
#
# [[1, [1, 2, 3, 4]]],
#
# [[2, [1, 2, 3, 4]]],
#
# [[3, [1, 2, 3, 4]]],
#
# [[4, [1, 2, 3, 4]]],
# [[5, [1, 2, 3, 4]]],
# [[6, [1, 2, 3, 4]]],
# [[7, [1, 2, 3, 4]]],
# [[8, [1, 2, 3, 4]]],
# [[9, [1, 2, 3, 4]]],
# [[0, 0]],
# [[0, 0]],
# [[0, 0]],
# [[0, 0]],
# [[0, 0]],
# [[0, 0]],
# [[0, 0]]], dtype=object)
谢谢你的回答,我已经评论了我在循环中使用a所做的事情。很遗憾,你的答案没有插入到特定的索引中。谢谢你的回答,这是我需要听到的。然后我希望看到绿色的复选标记表示正式接受答案,这样其他有相同问题/愿望的人可以很快找到他们想要的东西。;)我对此表示怀疑,但由于我的声誉不到15个,所以答案并不是绿色的