Home > Back-end >  how to search and print a specific thing from a list
how to search and print a specific thing from a list

Time:09-17

currently I have a list called itemList whereby the data are retrieved and passed into it from a text file. After all the formatting, the data in the list comes out as shown as below:

 ['1', 'Apple', '2.00', '0'], ['2', 'Orange', '1.50', '0'], ['3', 'Cabbage', '5.50', '100']

the arrangement is [item code, item name, price, quantity].

right now, what im trying to do is that i want to let users to search for a specific thing and it will search through everything in the list and prints out whatever that contains it but what I have now is that you need to be specific with what you search, meaning lets just say i want to search for Apple, I need to type in "Apple" specifically to search for it, but, what I want is for Apple to show up even if i only typed in "a".

I want to know if there's any built in functions or any way to do this? My current code is as shown:

with open("itemList.txt", "r") as itemFile:
     for row in itemFile:
         row = row.strip("\n")
         itemList.append(row.split())
for everything in itemList:
        if str(search) in everything:
            print(everything[0] " " everything[1] " " everything[2] " " everything[3] "\n")

CodePudding user response:

String matching could be an expensive operation, if done over the entire dataset. You could compare each string and check if it starts with a given string, but that wouldn't be optimal.

A simple optimisation over this would be to use a trie. Each end node in the trie would point to the item struct. This way, you can avoid iterating over the items that don't start with the given string, and optimise the brute force string matching approach.

CodePudding user response:

It seems like instead of a list that includes all values, you may want a dictionary with the produce name as the key, and the remaining values from the list you have now as the values in the dict. So you would be able to call print(my_produce_dict['Apple']) and all of the values of "Apple" would be printed out.

As for the partial entry results, you could check my_produce_dict.keys to see if any of the keys in that list start with the user input up to that point and print out all of the keys that do.

CodePudding user response:

You can achieve the desired effect by changing the search to lower as well as the apples, as shown below:

itemList = [['1', 'Apple', '2.00', '0'], ['2', 'Orange', '1.50', '0'], ['3', 'Cabbage', '5.50', '100']]
search = "a"
for everything in itemList:
        if str(search.lower()) in everything[1].lower():
            print(everything[0] " " everything[1] " " everything[2] " " everything[3] "\n")

Gives you this output:

1 Apple 2.00 0

2 Orange 1.50 0

3 Cabbage 5.50 100

CodePudding user response:

No, There is no bult in function for this, but there are bult in module, import the funtion search from re module and sort the results with the class Counter of the other bult in module collections, take thais peas of code as an example:

from re import search
from collections import Counter

options=['apple', 'my applecation', 'banana', 'orange', 'platform']
while True:#keep asking for search input
    pattern=input('search for :')
    pattern=pattern.lower()#convert all the letters to the lower case
    results={}
    for option in options:
        if not search(pattern, option.lower()) is None:#if search pattern is in the current option
            results[option]=-len(option.split(pattern)[0])#{'option': /hot far from the start is the pattern}
    results=Counter(results).most_common()#sort the results according to how far from the start is the pattern
    #now results=[(option, num), (option, num), (option, num)]
    final=[]
    for result in results:
        final.append(result[0])#append the option of the results without thier number
    for result in final:
        print(result)

input:

search for: pl

output:

plateform

apple

my applecation

Tell me if enything went wrong.

  • Related