Python元素一旦放入函数就无法工作


sample_array = [[[1.25], [1.10]], [[-1.09], [1.29]], [[1.34], [-1.34]], [[-1.12], [1.34]], [[-1.03], [-1.31]], [[-1.10], [-1.35]]]

当我把它放在下面的代码中时,它确实可以从每个子集中提取第一个元素,这就是我试图做的

for i in range(len(sample_array)):
c = sample_array[i][0][0]
d = initializedList.append(c)
i+=1

然而,当我添加一个函数时,它不起作用。

def my_function(b):
initializedList = []
for i in range(len(b)):
c = b[i][0][0]
d = initializedList.append(c)
i+=1
return d

尝试:

def my_function(b):
initializedList = []
for i in range(len(b)):
c = b[i][0][0]
initializedList.append(c)
# i+=1
return initializedList 

sample_array = [[[1.25], [1.10]], [[-1.09], [1.29]], [[1.34], [-1.34]], [[-1.12], [1.34]], [[-1.03], [-1.31]], [[-1.10], [-1.35]]]

print(my_function(sample_array))

输出:

[1.25, -1.09, 1.34, -1.12, -1.03, -1.1]

相关内容

  • 没有找到相关文章

最新更新