0

I have created a list called ingreso_datos, within which I enter information, note that when working with for loops in my variables I display the result of the last list, that is, of element 15. Explanation image here:image, here I leave the excel: ejercicio_ec

I have created a 36x36 array of zeros, plus I have created another list called counters, which tells me the rows and columns to which I should replace the zeros of my previously created array.

The values that I am going to replace in the zeros matrix are given by another matrix, this 6x6 matrix, called la_matriz.

My problem is that when I do this with a for loop, I get AttributeError: 'list' object has no attribute 'intertuples'.

This week I am learning the manipulation of matrices in Python so I really appreciate the feedback to these questions, regards.

And excuse my English, it is not my native language.

import pandas as pd
import numpy as np

base = pd.ExcelFile('ejercicio_ec.xlsx')

ingreso_datos = [[ 1,    'W',    28,    29,    30,    19,    20,     21,      0,      0,      0,    2.5,    'xh24',    10],
                 [ 2,    'W',    31,    32,    33,    22,    23,      24,    4.5,      0,    4.5,    2.5,    'xh24',    10],
                 [ 3,    'W',    34,    35,    36,    25,    26,      27,    9.5,      0,    9.5,    2.5,    'xh24',    10],
                 [ 4,    'W',    19,    20,    21,    10,    11,      12,      0,    2.5,      0,      5,    'xh24',    10],
                 [ 5,    'W',    22,    23,    24,    13,    14,     15,    4.5,    2.5,    4.5,      5,    'xh24',    10],
                 [ 6,    'W',    25,    26,    27,     6,    17,      18,    9.5,    2.5,    9.5,      5,    'xh24',    10],
                 [ 7,    'W',    10,    11,    12,     1,     2,       3,      0,      5,      0,    7.5,    'xh24',    10],
                 [ 8,    'W',    13,    14,    15,     4,     5,       6,    4.5,      5,    4.5,    7.5,    'xh24',    10],
                 [ 9,    'W',    16,    17,    18,     7,     8,       9,    9.5,      5,    9.5,    7.5,    'xh24',    10],
                 [10,    'X',      19,    20,    21,    22,    23,       24,      0,    2.5,    4.5,    2.5,    'xh24',    10],
                 [11,    'X',      22,    23,    24,    25,    26,       27,    4.5,    2.5,    9.5,    2.5,    'xh24',    10],
                 [12,    'X',      10,    11,    12,    13,    14,       15,      0,      5,    4.5,      5,    'xh24',    10],
                 [13,    'X',      13,    14,    15,    16,    17,       18,    4.5,      5,    9.5,      5,    'xh24',    10],
                 [14,    'X',       1,     2,     3,     4,     5,        6,      0,    7.5,    4.5,    7.5,    'xh24',    10],
                 [15,    'X',       4,     5,     6,     7,     8,        9,    4.5,    7.5,    9.5,    7.5,    'xh24',    10]]


propies = []

for i in range(len(ingreso_datos)):
    if ingreso_datos[i][12] == 'xh24':
        Data = base.parse('xh24')

    for j in range(len(Data)):
        if ingreso_datos[i][13] == Data.values[j][0]:
            propies.append([Data.values[j][1], Data.values[j][2], Data.values[j][3]]) 

counters = []

for i in range(len(ingreso_datos)):

    b = propies [i][0]            
    c = propies [i][1]            
    d = propies [i][2]           
    e = np.sqrt((ingreso_datos[i][10]-ingreso_datos[i][8])**2+(ingreso_datos[i][11]-ingreso_datos[i][9])**2)    

    La_matriz = np.array([[ b*c/e,            0,           0,    -b*c/e,           0,            0],
                       [     0,     c*d/e**3,    c*d/e**2,         0,    c*d/e**3,     c*d/e**2],
                       [     0,     c*d/e**2,       c*d/e,         0,     c*d/e**2,      c*d/e],
                       [-b*c/e,            0,           0,     b*c/e,           0,           0],
                       [     0,     c*d/e**3,    c*d/e**2,         0,     c*d/e**3,    c*d/e**2],
                       [     0,     c*d/e**2,       c*d/e,         0,      c*d/e**2,    c*d/e]])


    # Matrix de zeros
    ceros_grande = np.zeros((36,36)) 

    counters.append([ingreso_datos[i][2], ingreso_datos[i][3], ingreso_datos[i][4],
                     ingreso_datos[i][5], ingreso_datos[i][6], ingreso_datos[i][7]])


    for _, i1, i2, i3, j1, j2, j3 in counters():

        ceros_grande[:, [i1,i2,i3,j1,j2,j3]] = La_matriz
DaniV
  • 489
  • 2
  • 9

1 Answers1

0

I don't like it when people ask something and then others respond with 'why don't you try this or that', but why don't you try np.where()?

It can return you the indices of the matrix that have a specific value.

>>> x = numpy.array([1,0,2,0,3,0,4,5,6,7,8])
>>> numpy.where(x == 0)[0]
array([1, 3, 5]

see this post for reference. It might be more efficient and less painful.

bonobo
  • 132
  • 2
  • 10