Jun-01-2022, 07:08 PM
Not sure to figure out exactly what you want to do, but I guess the following code may help you. Note that I think it's better to convert directly the list:
import numpy as np List = [[3.3480644 , 2.6733143 , 2.3946035 , 1.651863 , 1.4944869 , 1.4064457 , 2.0166957 , 2.7554176 ], [2.6761208 , 2.147566 , 1.2485328 , 0.6944433 , 0.87446946 , 1.1815729 , 1.879112 , 2.3758152 ]] List2Array = np.asarray(List) # convert a list into a Numpy array print(f"List2Array={List2Array}\n") r, c = np.shape(List2Array) # r=number of rows and c=number of columns # to add zeros in front M1 = np.zeros((r, 1), dtype=np.float32) InFront = np.hstack((M1, List2Array)) print(f"InFront={InFront}\n") # to add zeros at the bottom M2 = np.zeros((1, c), dtype=np.float32) AtBottom = np.vstack((List2Array, M2)) print(f"AtBottom={AtBottom}\n") # to sum the array per row SumPerRow = np.sum(List2Array, axis=1) print(f"SumPerRow={SumPerRow}\n") # to sum the array per column SumPercolumn = np.sum(List2Array, axis=0) print(f"SumPercolumn={SumPercolumn}\n") # to eventually sum all terms SumAllTerms = np.sum(List2Array) print(f"SumAllTerms={SumAllTerms}")
Output:List2Array=[[3.3480644 2.6733143 2.3946035 1.651863 1.4944869 1.4064457
2.0166957 2.7554176 ]
[2.6761208 2.147566 1.2485328 0.6944433 0.87446946 1.1815729
1.879112 2.3758152 ]]
InFront=[[0. 3.3480644 2.6733143 2.3946035 1.651863 1.4944869
1.4064457 2.0166957 2.7554176 ]
[0. 2.6761208 2.147566 1.2485328 0.6944433 0.87446946
1.1815729 1.879112 2.3758152 ]]
AtBottom=[[3.3480644 2.6733143 2.3946035 1.651863 1.4944869 1.4064457
2.0166957 2.7554176 ]
[2.6761208 2.147566 1.2485328 0.6944433 0.87446946 1.1815729
1.879112 2.3758152 ]
[0. 0. 0. 0. 0. 0.
0. 0. ]]
SumPerRow=[17.7408911 13.07763246]
SumPercolumn=[6.0241852 4.8208803 3.6431363 2.3463063 2.36895636 2.5880186
3.8958077 5.1312328 ]
SumAllTerms=30.81852356