Search code examples
python-3.xloopsfor-looptry-except

Nested For and Try Loop - Main loop doesn't solve


My Data is a list of lists with r rows with varying length containing strings of data - some of which are floats but have been read in as strings.

I would like to loop first through all rows, then through all elements and then apply a try/except function on said elements to find the first instance of a string in the row that can be converted to a float.

My code outputs as expected for when I explicitly tell the second loop for which row it should do the operation, however, when I try to loop over all rows, it only outputs the expected output for the first row and none of the following rows.

The expected output list float_index (with lenght = len(data)) would be a list with the index of the first convertable element for all rows.

Here's the code with the explicit row definition outputting [2], since for the second row, it's the 2nd element that is convertible to a float:

data = [['Mittl.', 'Halleninnenpegel,', 'Volllast', 'Li', '124', '132', '132', '132', '139', '138', '141', '139', '131', '146'],
['Abgaskamin', 'LW', '130', '129', '121', '104', '100', '96', '94', '89', '86', '108']]


row= 1
floats = []
float_index = []
for i in data[row]:
    try:
        floats.append(str(int(float(i))))
        float_index = [data[row].index(floats[0])]
    except:
        pass
print(float_index)

Here's the code looping all rows in data, but only outputting the expected value for the first row float_index = [4], while the expected was float_index = [4,2]:

data = [['Mittl.', 'Halleninnenpegel,', 'Volllast', 'Li', '124', '132', '132', '132', '139', '138', '141', '139', '131', '146'],
['Abgaskamin', 'LW', '130', '129', '121', '104', '100', '96', '94', '89', '86', '108']]

floats = []
float_index = []
for r in range(len(data)):
    for i in data[r]:
        try:  
            floats.append(str(int(float(i))))
            float_index = [data[r].index(floats[0])]
        except:
            pass
print(float_index)

The floats list is probably the problem - it just collects all convertible elements into a long list with one row - I need the floats list to be of the same way as data, that it puts all convertible elements into new rows, so that by floats[0] I find the first elements for all rows, but somehow can't get my head around achieving this.

Would appreciate the help, thank you!


Solution

  • No need to loop through each element just break out of the loop once the first one is found:

    data = [['Mittl.', 'Halleninnenpegel,', 'Volllast', 'Li', '124', '132', '132', '132', '139', '138', '141', '139', '131', '146'],
    ['Abgaskamin', 'LW', '130', '129', '121', '104', '100', '96', '94', '89', '86', '108']]
    
    floats = []
    float_index = []
    for lest in data:
        float_temp = None
        float_ind_temp = None
        for el in lest:
            try:
                float_temp = str(int(float(el)))
                floats.append(float_temp)
                float_index_temp = lest.index(float_temp)
                break
            except:
                pass
        float_index.append(float_index_temp)
    print(float_index)