Roger Parish III
Roger Parish III

Reputation: 133

Searching Python Lists

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, it can find one item in a list only if you type the string in the terminal exactly as it is in the list, that is the problem.

i need to be able to type one or two characters in the terminal and have it so that it lists the strings inside the list that matches the search (for example: if i were to type "P" the terminal, it would list "Pink" and "Purple" because they match my search so far, but not exactly)

I may be overlooking something but, is there a way i can search a list this way without having to have over 200 lines of code (200+ lines because what i need to implement this to has over 150 strings in the list) just for searching for the string?

Upvotes: 4

Views: 255

Answers (7)

Jonathan Shock
Jonathan Shock

Reputation: 121

Using regular expressions will allow you to determine how much of the text and which part of it needs to be matched. The following will just search the start of the string.

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

Upvotes: 2

Shankar
Shankar

Reputation: 3624

You can use difflib standard Python library.

Sample Code:

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 ratio as per your needs. Here I have used a ratio of .25 and above for mining patterns.

Upvotes: 2

lucemia
lucemia

Reputation: 6617

if performance is not an issue, (ex: color list 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

Upvotes: 2

Summer_More_More_Tea
Summer_More_More_Tea

Reputation: 13356

What you need is a proper data structure. From your requirement description, I think trie is just the one.

You build a trie with a list of colors and then search the trie with user inputs (prefix is allowed). You can find diverse implementations on github or implement it yourself. :)

Upvotes: 2

insertusernamehere
insertusernamehere

Reputation: 51

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

Not the absolute most elegant solution, but it gets the job done. Just loop over the list and compare the relevant substrings. Admittedly, you might want to wrap this in a try/catch block for a KeyError in case the search string is longer than any element in colors.

Upvotes: 1

Peter Graham
Peter Graham

Reputation: 11701

The simplest way, using a list comprehension:

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

If you have a large list this might not perform so well.

Upvotes: 5

Patashu
Patashu

Reputation: 21773

search = str(raw_input())

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

Then loop over matches and print.

Upvotes: 1

Related Questions