[英]How to fill each element of a 2d array with a for loop in python
Whats happening is the loop is iterating and filling the elements of the array, but 'element wise' - ie it is calculating the first element correctly, then looping again and filling the first element again with what should be the second element....so ultimately, i have a 2d array/matrix filled with the values that should only be in the last element of my matrix.发生的事情是循环正在迭代并填充数组的元素,但是'元素明智' - 即它正确计算第一个元素,然后再次循环并再次用应该是第二个元素的元素填充第一个元素......所以最终,我有一个二维数组/矩阵,其中填充了应该只在矩阵的最后一个元素中的值。 How to make it such that once the first element is filled, the for loop moves on to the next element and the next value?如何使第一个元素被填充后,for循环继续移动到下一个元素和下一个值?
Heres what Ive tried:这是我试过的:
sim_matrix=np.zeros((3,3), dtype=float)
for i in range(0,3):
for j in range(0,3):
for k in range(0,3):
for l in range(0,3):
sim_matrix[i][j]=node_list_bsc[0][k]['bsc mean value']-node_list_bsc[1][l]['bsc mean value']
#node_list_bsc[x][y]['bsc mean value'] are float values - they are stored in a list of dictionaries,
#hence the double indexing
this gives:这给出了:
array([[1.14488261e-08, 1.14488261e-08, 1.14488261e-08],
[1.14488261e-08, 1.14488261e-08, 1.14488261e-08],
[1.14488261e-08, 1.14488261e-08, 1.14488261e-08]])
but it should give:但它应该给出:
[[-5.131371757023141e-10, -7.509188731093747e-08, 7.67647989217221e-07],
[8.933230795306053e-08, 1.475355781782538e-08, 8.574934343459839e-07],
[-7.567123003427902e-07, -8.312910504780254e-07, 1.144882605013307e-08]]
So as you can see, its filling up with the last element only.如您所见,它仅填充了最后一个元素。 I ran the print command from within the centre of these nest for loops to see how it was filling, and its like this: (example)我从这些嵌套 for 循环的中心运行打印命令,以查看它是如何填充的,如下所示:(示例)
[[-5.13137176e-10 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00 0.00000000e+00]]
[[-7.50918873e-08 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00 0.00000000e+00]
[ 0.00000000e+00 0.00000000e+00 0.00000000e+00]]...
ie first element is correct but then gets replaced by 2nd element.即第一个元素是正确的,但随后被第二个元素替换。 This is probably obvious already from the code but I think its still useful to illustrate.从代码中这可能已经很明显了,但我认为它仍然有助于说明。 I've also tried with while loops, and if - else statements - something like:我也尝试过使用 while 循环和 if - else 语句 - 类似:
#pseudo code following:
for i in range(0,3):
for j in range(0,3):
for k in range(0,3):
for l in range(0,3):
if sim_matrix[i][j]==0.0:
sim_matrix[i][j]=node_list_bsc[0][k]['bsc mean value']-node_list_bsc[1][l]['bsc mean value']
else: continue
It feels pretty close - but i just cant quite figure out where Im going wrong!感觉很接近——但我就是不知道我哪里出错了!
As I understand your problem, you are getting the same number in every place of your 2D array.据我了解您的问题,您在二维数组的每个位置都得到相同的数字。 At first, it is difficult to suggest anything without knowing about "node_list_bsc".起初,如果不了解“node_list_bsc”,就很难提出任何建议。 However, to assign values to a 2D array you only need two loops.但是,要将值分配给 2D 数组,您只需要两个循环。 Your code can be written as sim_matrix=np.zeros((3,3), dtype=float)您的代码可以写成 sim_matrix=np.zeros((3,3), dtype=float)
for i in range(0,3):
for j in range(0,3):
if sim_matrix[i][j]==0.0:
sim_matrix[i][j]=node_list_bsc[0][i]['bsc mean value']- node_list_bsc[1][j]['bsc mean value']
If it does not work, let me know and provide more information about node_list_bsc.如果它不起作用,请告诉我并提供有关 node_list_bsc 的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.