Alexander Schmolck <[EMAIL PROTECTED]> writes: > The easiest way to do this is to have a nested dictionary of prefixes: for > each prefix as key add a nested dictionary of the rest of the split as value > or an empty dict if the split is empty. Accessing the dict with an userinput > will give you all the possible next choices.
Oops I was reading this too hastily -- forgot to compact and take care of sep. You might also want to google 'trie', BTW. (again, not really tested) def addSplit(d, split): if len(split): if split[0] not in d: d[split[0]] = addSplit({}, split[1:]) else: addSplit(d[split[0]], split[1:]) return d def compactify(choices, parentKey='', sep=''): if len(choices) == 1: return compactify(choices.values()[0], parentKey+sep+choices.keys()[0], sep) else: for key in choices.keys(): newKey, newValue = compactify(choices[key], key, sep) if newKey != key: del choices[key] choices[newKey] = newValue return (parentKey, choices) def queryUser(chosen, choices, sep=''): next = raw_input('So far: %s\nNow type one of %s: ' % (chosen,choices.keys())) return chosen+sep+next, choices[next] wordList=['1p2m_3.3-1.8v_sal_ms','1p2m_3.3-1.8v_pol_ms','1p3m_3.3-18.v_sal_ms'] choices = compactify(reduce(addSplit,(s.split('_') for s in wordList), {}), sep='_')[1] chosen = "" while choices: chosen, choices = queryUser(chosen, choices, '_') print "You chose:", chosen -- http://mail.python.org/mailman/listinfo/python-list