Home > Enterprise >  How to check if an element is in a sub list, then print the sub list
How to check if an element is in a sub list, then print the sub list

Time:11-10

My program requires the user to input a keyword just like below:

search = input("Enter data name: ").upper()

    with open('schedule.txt', 'r') as f:
        list_of_lists = []
        for line in f:
            stripped_line = line.strip()
            line_list = stripped_line.split(',')
            list_of_lists.append(line_list)
        print(list_of_lists)
        for lists in list_of_lists:
            if search in lists:
                output = lists
                print('The available flights are:', *output, sep='\n')
            else:
                print('No flights found')
        f.close()

If the keyword is present in the sublist, then my program will print that sublist (Could be found in more than one sublist, so several outputs are expected). The problem i have right now is, however, the output does not checkk for the element and it displayed in a long as list instead of separating in a new line.

My text file:

[1] MH371, MALAYSIAN AIRLINE, KUALA LUMPUR, BEIJING , 19-12-2021, N/A
[2] SX849, AIRASIA          , KUALA LUMPUR, BANGKOK , 05-01-2022, N/A
[3] MH234, MALAYSIAN AIRLINE, KUALA LUMPUR, LANGKAWI, 03-02-2022, N/A
[4] FD709, KOREA AIRLINE    , KUALA LUMPUR, SEOUL   , 29-12-2021, N/A
[5] Z1314, CATHAY AIRLINE   , KUALA LUMPUR, TOKYO   , 21-01-2022, N/A
[6] HY520, EMIRATES         , KUALA LUMPUR, TAIPEI  , 15-11-2021, N/A
[7] TT879, MALINDO AIR      , KUALA LUMPUR, HAWAII  , 08-02-2022, N/A

My output:

Enter data name: bangkok
[['[1] MH371', ' MALAYSIAN AIRLINE', ' KUALA LUMPUR', ' BEIJING ', ' 19-12-2021', ' N/A'], ['[2] SX849', ' AIRASIA          ', ' KUALA LUMPUR', ' BANGKOK ', ' 05-01-2022', ' N/A'], ['[3] MH234', ' MALAYSIAN AIRLINE', ' KUALA LUMPUR', ' LANGKAWI', ' 03-02-2022', ' N/A'], ['[4] FD709', ' KOREA AIRLINE    ', ' KUALA LUMPUR', ' SEOUL   ', ' 29-12-2021', ' N/A'], ['[5] Z1314', ' CATHAY AIRLINE   ', ' KUALA LUMPUR', ' TOKYO   ', ' 21-01-2022', ' N/A'], ['[6] HY520', ' EMIRATES         ', ' KUALA LUMPUR', ' TAIPEI  ', ' 15-11-2021', ' N/A'], ['[7] TT879', ' MALINDO AIR      ', ' KUALA LUMPUR', ' HAWAII  ', ' 08-02-2022', ' N/A']]

As you can see, it prints entire list instead of just the sub list where the element is present

CodePudding user response:

You can see if the word is in the line then convert it to a list and output:

search = input("Enter data name: ").upper()

with open('filename.csv') as input_file:
    for line in input_file:
        sublist = list(map(str.strip, line.split(',')))
        if search in sublist:
            print(sublist)

Output:

>>> Enter data name: bangkok
['[2] SX849', 'AIRASIA', 'KUALA LUMPUR', 'BANGKOK', '05-01-2022', 'N/A']

CodePudding user response:

As the correct answer as been pointed out by @Jab already, I'll correct your mistake so that it helps with your homework assignment

In your code from line 9 to line 13:

for lists in list_of_lists:
    if search in lists:
        output = lists
        print('The available flights are:', *output, sep='\n')

lists variable will be equal to :

[1] MH371, MALAYSIAN AIRLINE, KUALA LUMPUR, BEIJING , 19-12-2021, N/A

in the first run and your really searching for the list of strings instead of just a string like "KUALA LUMPUR"

So, to correct that you can use 2 loops instead of one to access the inner elements.

like this :

for lists in list_of_lists:
        for elements in lists:
            if search in elements:  
                output = lists
                print('The available flights are: \n', *output, sep='')

I hope it solves your problem. Please upvote if it did.

  • Related