How to search through an array and remove items that don't match a certain critera

Question:

I’m new to python and currently trying to complete an assignment. I have tried searching for an answer in other places but I have found my specific issues anywhere.

Basically the assignment is to create an ATM transaction log reader that allows a user to enter the file name, which is then loaded into the program. They should then be able to view the file contents and search through using the different details i.e. account number, ATM id etc.

I am having issues with creating the search function, I am only allowed to use one function and I can not import any modules.

The goal: add any lists (inside the 2D list) that meets the user input search criteria to a new 2D list. Which can then be printed in a table format so that the user can view the transactions from only a specific account or atm.

The code (I commented on the sections with the errors):

def search_records(array, search_field, search_value):
   
   rows = len(str(array))
   search_result = []

   if search_field == "id":  #ERROR HERE ---> TypeError: 'int' object is not subscriptable
      for elem in rows:
         if elem == search_value:
            search_result = array.append(str(elem))
            #Print a table only containing lists which meet search criteria (from new array).
            print(f"Records with ATM ID: {search_value}:")
            print(" ")
            print("ATM ID : Account : Operation : Amount")
            print("-------------------------------------")
            for elem in search_result:
                print(elem [0], " " * (8 - len(str(elem[0]))), elem [1], " " * (10 - len(str(elem[1]))), elem [2], " " * (7 - len(str(elem[2]))), elem [3], " " * (3 - len(str(elem[3]))))
        else:
            print(f"No records found with ATM ID: {search_value}")

if search_field == "account":  
    for elem in rows:  #ERROR HERE ---> TypeError: 'int' object is not subscriptable
        if elem == search_value:
            search_result = array.append(str(elem))
            
            print(f"Records with ATM ID: {search_value}:")
            print(" ")
            print("ATM ID : Account : Operation : Amount")
            print("-------------------------------------")
            for elem in search_result:
                print(elem [0], " " * (8 - len(str(elem[0]))), elem [1], " " * (10 - len(str(elem[1]))), elem [2], " " * (7 - len(str(elem[2]))), elem [3], " " * (3 - len(str(elem[3]))))
        else:
            print(f"No records found with account number: {search_value}")

if search_field == "operation":  
    for elem in rows: #ERROR HERE ---> TypeError: 'int' object is not subscriptable
        if elem == search_value:
            search_result = array.append(str(elem))
            
            print(f"Records with ATM ID: {search_value}:")
            print(" ")
            print("ATM ID : Account : Operation : Amount")
            print("-------------------------------------")
            for elem in search_result:
                print(elem [0], " " * (8 - len(str(elem[0]))), elem [1], " " * (10 - len(str(elem[1]))), elem [2], " " * (7 - len(str(elem[2]))), elem [3], " " * (3 - len(str(elem[3]))))
        else:
            print(f"No records found with operation: {search_value}")

Because of the TypeError: ‘int’ object is not subscriptable error I can’t even test if the code would otherwise work. I have tried converting everything I can think of to a string but I just can’t figure out what is causing the error.

EDIT!!!!

Changing rows to arrays got rid of the error!

The issue I’m having now is I’m always getting ‘no records found’ even when I know there’s definitely a recording for what I’m searching.

Asked By: Loree

||

Answers:

The problem occurs when you try to iterate with a for loop over the int variable. Here is an example of the error:

var = 5
for i in var:
    ...

You have created this situation by doing the following:

rows = len(str(array))
for elem in rows:
    ...

You could maybe fix the error by removing rows = len(str(array)) from code and then just iterating over the array:

for elem in array:
    ...
Answered By: Aleksa Majkic