Home > other >  Flask is it possible to search input in a list and then post list?
Flask is it possible to search input in a list and then post list?

Time:06-19

For example if i have 3 lists:

  • apple, pear, bananas.
  • cats, dogs, monkeys.
  • harry, luke, damien.

If input is Luke, i want it to post the entire list, and if it is apple i want it to post that list. Is this even possible?

CodePudding user response:

Try this. It is easier for this.

a =['apple', 'pear', 'bananas', 'cats', 'dogs', 'monkeys', 'harry', 'luke', 'damien']
print('luke' in a)

CodePudding user response:

I suggest you to convert the lists that contain the string elements to Python dictionaries (hash tables). So when you need to check if a certain string is in a list, you would be performing an O(1) operation with a dict, which is more efficient than doing an O(n) search in each list:

d = [{"apple" : None, "pear" : None, "bananas" : None}, 
{"cats" : None, "dogs" : None, "monkeys" : None},
{"harry" : None, "luke" : None, "damien" : None}]

Input = "luke"
outPost = None
for i in d:
    if Input in i:
        outPost = i
        break
    
if Input=="luke":
    outPost = []
    for i in d:
        for j in i.keys():
            outPost.append(j)
else:
    outPost = list(outPost.keys())
print(outPost)

CodePudding user response:

list=[['apple','carrot','orange'],['john','luke','jane'],['cocoa','pepsi','cola']]
s=input('enter your choice')
for i in range(0,3):
    for j in range(0,3):
        if list[i][j]==s:
            print(list[i])

Is this what you need?

  • Related