Python Listing

ok, I have this code:

colors = ["Red" , "Green" , "Blue" , "Pink" , "Purple" , "Cyan"] search = str(raw_input()) found = "not" if search in colors: print "Found!" else: print "not Found" 

so far, he can find one element in the list only if you enter a line in the terminal exactly as it is in the list, that is a problem.

I need to be able to type one or two characters in the terminal and have it so that it lists the lines inside the list that match the search (for example: if I were to enter "P" on the terminal, this is the list "Pink" and "Purple" , because they match my search so far, but not quite)

I can ignore something, but is there a way I can search the list this way without having more than 200 lines of code (200+ lines, because I need to implement this in order to have more than 150 lines in the list) just to search for the string ?

+4
source share
7 answers

The easiest way using list comprehension:

 matches = [color for color in colors if color.startswith(search)] 

If you have a large list, this may not work so well.

+5
source

You need the right data structure. From your description of the requirements, I think trie is just one.

You create a trie with a list of colors, and then look for a trie with custom inputs (prefix allowed). You can find various implementations on github or implement it yourself. :)

+2
source

if performance is not a problem (for example: the list of colors is small):

 colors = ["Red" , "Green" , "Blue" , "Pink" , "Purple" , "Cyan"] search = str(raw_input()) found = "not" for color in colors: # or if color.startswith(search), depend on your needs if search in color: print "Found" print "not Found" 

otherwise use Trie: http://en.wikipedia.org/wiki/Trie

+2
source

You can use the standard Python library for difflib.

Code example:

 from difflib import SequenceMatcher colors = ["Red", "Green", "Blue", "Pink", "Purple", "Cyan"] search = str(raw_input()) for color in colors: s = SequenceMatcher(None, search, color) if s.ratio() > 0.25: print color 

Output:

 xxxx$ python foo.py p Purple 

Note:

You can manipulate the match rate according to your needs. Here I used a ratio of 0.25 and higher for mining patterns.

+2
source

Using regular expressions will allow you to determine which part of the text and which part of it should be matched. The following will simply look for the beginning of the line.

 import re colors = ["Red" , "Green" , "Blue" , "Pink" , "Purple" , "Cyan"] search = re.compile("^"+str(raw_input())) isthere=[] for col in colors: if search.findall(col)!=[]: isthere.append(col) if isthere==[]: print "Nothing there" else: print isthere 
+2
source
 search = str(raw_input()) matches = [s for s in colors if s.startswith(search)] 

Then iterate over the matches and print.

+1
source
  for c in colors: if c[0:len(search)-1] == search: print "Found!" 

Not the absolute most elegant solution, but it does its job. Just go through the list and compare the corresponding substrings. Admittedly, you can wrap this in a try / catch block for KeyError if the search string is longer than any item in colors.

+1
source

All Articles