S
S
Sergey Zolotarev2020-08-01 11:22:59
Python
Sergey Zolotarev, 2020-08-01 11:22:59

How to correctly get the element number of a multidimensional array?

Good afternoon!
I'm still trying to figure out how to get the element number of a multidimensional array.
The piece of code where it applies:

mls_scc = LearnMaxLevelDataArray("school")

        for rowm, index in mls_scc:

            if row['gender'] == "M": 
                input_ssc_level = rd[rowm[1][0].index(index)].value
            if row['gender'] == "F": 
                input_ssc_level = rd[rowm[0][0].index(index)].value

            maxlevelbalance = 100 - input_ssc_level #Получает баланс уровня от максимального уровня критерии
            userlevelbalance = 100 - row['ssc_p']

            if maxlevelbalance > userlevelbalance:
                    levelbalance = maxlevelbalance - userlevelbalance #Подсчитываем баланс от уровня

            if maxlevelbalance < userlevelbalance:
                levelbalance = userlevelbalance - maxlevelbalance

            for rowms, index in mlsb.iterrows():
                if levelbalance == rowms[index] or levelbalance > rowms[index]:

                    #Если текущий баланс уровня соответствует нужным критериям, то система для списка успешных выпускников оставит уровень школьных знании, которого добился выпускник
                    sgl['profile']['info']['school'] = row['ssc_p']

LearnMaxLevelDataArray - the function itself implemented by me, which consists of multidimensional arrays and selects the cell codes of the Excel table by key to select the maximum values.
And the compiler brought some surprises:
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
 in 
     43 
     44             if row['gender'] == "M":
---> 45                 input_ssc_level = rd[rowm[1][0].index(index)].value
     46             if row['gender'] == "F":
     47                 input_ssc_level = rd[rowm[0][0].index(index)].value

TypeError: must be str, not list

Inside the [] I used the index method to get the number of the array element.
It requires that the function be a list, and I will send the function code in the comments to the question.

How can Python correctly get the element number of multidimensional arrays?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
soremix, 2020-08-01
@seregazolotaryow64

TypeError: must be str

It requires the function to be a list

It's almost
unclear what mls_scc is and what content it contains, how to solve the error?
As I understand it, something in the form was planned here. Why then .index()
for index, rowm in enumerate(mls_scc):
    if row['gender'] == "M": 
        input_ssc_level = rd[rowm[1][0].index(index)].value
    if row['gender'] == "F": 
        input_ssc_level = rd[rowm[0][0].index(index)].value

A
Andrey, 2020-08-01
@anerev

We made it up, of course) You have a main list da it consists of 2 objects, you call the for loop which goes through 2 objects, respectively, the variables rowm, index are two separate non-intersecting lists. Briefly speaking

input_ssc_level = rd[>>>>rowm[1][0].index(index)<<<<].value
rowm[1][0] - you are not accessing the list, but the text, more precisely, even the letter of the text, and after that you try to call the index method on it by passing the list

D
deeppsycoder, 2020-08-01
@deeppsycoder

If a is a multidimensional array, and x is the value of the array whose index you want to find, then you can do this.

import numpy as np

indeces = np.unravel_index((a == x).argmax(), a.shape)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question