'How to append data properly in the next line of an array?

def FunctionA():

...
...
...
sol #Here,sol is an array

output = []
for i in sol:
    output.append(FunctionB(i))
print(output)
print(output[0,:])

def FunctionB(i):

metrics = [i[1],i[2],i[3]]
return metrics

driver()

Question No. 01:

Whenever I run the code, the Output array becomes-

[[x1,y1,z1],[x2,y2,z2], ... ... ,[xn,yn,zn]]

But I want the output array to be as follows-

[[x1,y1,z1],

[x2,y2,z2],
... ,

... ,

[xn,yn,zn]]

How do I do this?

Question No. 02:

And in the code, to print the values of first column, I am using this-

print(output[0,:]) this gives the following error.

print(output[0,:])

TypeError: list indices must be integers or slices, not tuple

How do I get all the values of contents of any particular column?



Solution 1:[1]

Using Numpy may help you to get your expected format

import string
import numpy as np

def FunctionA():

        sol = [[1,2,3],[4,5,6],[7,8,9]]

        output = []
        for i in sol:
                output.append(FunctionB(i))
        output = np.array(output)
        print(output)
        print(output[:,0]) #Column
        print(output[0,:]) #Row

def FunctionB(i):
        metrics = [i[0],i[1],i[2]]
        return metrics

FunctionA()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1