Home > Back-end >  Is there a way to iterate through a list of lists without getting an index error?
Is there a way to iterate through a list of lists without getting an index error?

Time:08-17

I have a list of lists and I am trying to pull out every nth term from each list within the list.

Here is my input:

[['', '', '', '', '1', '', '', '', '', '', '', '', '1TD1131D17025-2035', '', '', '', 
'', '', '', '', '', '', '', '', '', '', '09/16/2022', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '2', 
'', '', '', '', 'EA', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '', '353.60', '', '', '', '', '', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '', '', '', '', '', '', '707.20', '\n'], ['', '', '', '', '2', '', 
'', '', '', '', '', '', '1TD1131D17025-2036', '', '', '', '', '', '', '', '', '', '', 
'', '', '', '09/16/2022', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '2', '', '', '', '', 'EA', '', 
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 
'353.60', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '707.20', '\n'], ['', '', '', '', '3', '', '', '', '', '', '', '', 
'1TD1131D17025-2037', '', '', '', '', '', '', '', '', '', '', '', '', '', '09/16/2022', 
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '', '', '2', '', '', '', '', 'EA', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '353.60', '', '', '', '', '', 
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '707.20', 
'\n']]

Right now I am trying to pull out the first integer from each list.

Here is my sample code.

def find(n,e):
    for line in range(len(line_nu)):
        item = line_nu[n][e]
        n  = 1
    return item_nu.append(item)

I'm getting an 'Index out of range' error. I can call ' line_nu[0][4] ' outside of this loop, but using same numbers in def find() I get an error. I have also tried this as a while loop where I replace n with i and start count at 0. Same error. End goal is to get each none '' in a list of its own.

Anyone know what I'm doing wrong?

CodePudding user response:

You'll need two for loops. For example:

test = [['', '', '', '', '1', '', '', '', '', '', '', '', '1TD1131D17025-2035', '', '', '', 
'', '', '', '', '', '', '', '', '', '', '09/16/2022', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '2', 
'', '', '', '', 'EA', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '', '353.60', '', '', '', '', '', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '', '', '', '', '', '', '707.20', '\n'], ['', '', '', '', '2', '', 
'', '', '', '', '', '', '1TD1131D17025-2036', '', '', '', '', '', '', '', '', '', '', 
'', '', '', '09/16/2022', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '2', '', '', '', '', 'EA', '', 
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 
'353.60', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '707.20', '\n'], ['', '', '', '', '3', '', '', '', '', '', '', '', 
'1TD1131D17025-2037', '', '', '', '', '', '', '', '', '', '', '', '', '', '09/16/2022', 
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '', '', '2', '', '', '', '', 'EA', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '353.60', '', '', '', '', '', 
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '707.20', 
'\n']

for row in range(len(test)):
    for col in range(len(test[row])):
        print(test[row][col])

CodePudding user response:

I interpreted your question as looking for the nth non-empty element in each sub list, so elements != ''. This function will execute that and return an array of the nth non-empty element in each sub array

exl = [['', '', '', '', '1', '', '', '', '', '', '', '', '1TD1131D17025-2035', '', '', '', 
'', '', '', '', '', '', '', '', '', '', '09/16/2022', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '2', 
'', '', '', '', 'EA', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '', '353.60', '', '', '', '', '', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '', '', '', '', '', '', '707.20', '\n'], ['', '', '', '', '2', '', 
'', '', '', '', '', '', '1TD1131D17025-2036', '', '', '', '', '', '', '', '', '', '', 
'', '', '', '09/16/2022', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '2', '', '', '', '', 'EA', '', 
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 
'353.60', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '707.20', '\n'], ['', '', '', '', '3', '', '', '', '', '', '', '', 
'1TD1131D17025-2037', '', '', '', '', '', '', '', '', '', '', '', '', '', '09/16/2022', 
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '', '', '2', '', '', '', '', 'EA', '', '', '', '', '', '', '', '', 
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '353.60', '', '', '', '', '', 
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '707.20', 
'\n']]

def find(n,listOfLists):
    result = []
    for list in listOfLists:
        discoveredItems = 0
        while discoveredItems < n:
            for item in list:
                if item != '':
                    discoveredItems  = 1
                    if discoveredItems == n:
                        result.append(item)
    return result

So to get the first non-empty item in each list (the integers you mentioned above)...

find(1,exl)
# ['1', '2', '3']

And the dates (aka 3rd non-empty element):

find(3,exl)
# ['09/16/2022', '09/16/2022', '09/16/2022']
  • Related