Searching for uniqness in a list of data

johnzenger at gmail.com johnzenger at gmail.com
Wed Mar 1 18:10:13 CET 2006


You can come quite close to what you want without splitting the string
at all.  It sounds like you are asking the user to build up a string,
and you want to keep checking through your list to find any items that
begin with the string built up by the user.  Try something like this:

mylist = ['1p2m_3.3-1.8v_sal_ms','1p2m_3.3-1.8_sal_log']
sofar = ""

loop = True
while loop:
    selections = [ x[len(sofar):x.index("_", len(sofar) + 1)]
                   for x in mylist if x.startswith(sofar) ]
    loop = len(selections) > 1
    if loop:
        print selections
        sofar += raw_input("Pick one of those: ")




More information about the Python-list mailing list