Home > other >  Print array with a name for each row
Print array with a name for each row

Time:03-13

Is it possible to print an a name and an array for each row?

For example:

a = np.array([[1],
              [2],
              [3],
              [4]])

rv = np.array([["R1x ="],
               ["R1y ="],
               ["R5x ="],
               ["R5y ="]])

print(rv, a)

I want that the output is:

R1x = 1
R1y = 2
R5x = 3
R5y = 4

CodePudding user response:

Use zip for that:

for val, string in zip(a.flatten(), rv.flatten()):
    print(f"{string} {val}")

# out:
R1x = 1
R1y = 2
R5x = 3
R5y = 4

CodePudding user response:

In this case the ouput is: "R1x = (values in single row)"

kportico = np.matrix([[a1, 0, 0, -a1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                      [0, b1, c1, 0, -b1, c1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                      [0, c1, d1, 0, -c1, e1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                      [-a1, 0, 0, a1 a2, 0, 0, -a2, 0, 0, 0, 0, 0, 0, 0, 0],
                      [0, -b1, -c1, 0, b1 b2, -c1 c2, 0, -b2, c2, 0, 0, 0, 0, 0, 0],
                      [0, c1, e1, 0, -c1 c2, d1  d2, 0, -c2, e2, 0, 0, 0, 0, 0, 0],
                      [0, 0, 0, -a2, 0, 0, a2 a3, 0, 0, -a3, 0, 0, 0, 0, 0],
                      [0, 0, 0, 0, -b2, -c2, 0, b2 b3, -c2 c3, 0, -b3, c3, 0, 0, 0],
                      [0, 0, 0, 0, c2, e2, 0, -c2 c3, d2 d3, 0, -c3, e3, 0, 0, 0],
                      [0, 0, 0, 0, 0, 0, -a3, 0, 0, a4 a3, 0, 0, -a4, 0, 0],
                      [0, 0, 0, 0, 0, 0, 0 , -b3, -c3, 0, b4 b3, -c4 c3, 0, -b4, c4],
                      [0, 0, 0, 0, 0, 0, 0, c3, e3, 0, -c4 c3, d4 d3, 0, -c4, e4],
                      [0, 0, 0, 0, 0, 0, 0, 0, 0, -a4, 0, 0, a4, 0, 0],
                      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -b4, -c4, 0, b4, -c4],
                      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, c4, e4, 0, -c4, d4]])

kporticoinv = np.linalg.inv(kportico)

rotulado = 1
encastrado = 2

base = 1

if base == rotulado:
    a = kporticoinv[np.ix_([2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 14],[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 14])]
    fex = f[np.ix_([2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 14])]
    u = np.dot(a, fex)
    k2 = kporticoinv[np.ix_([0, 1, 12, 13],[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 14])]
    f1 = f[np.ix_([0, 1, 12, 13])]

    rv = np.array([["R1x ="],
                   ["R1y ="],
                   ["R5x ="],
                   ["R5y ="]])

    r = np.dot(k2, u) - f1

    for val, string in zip(r.flatten(), rv.flatten()):
        print(f"{string} {val}")
  • Related