Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 18 Online
OpenStudy (anonymous):

Is there an equivalent way to search a set that only contains unique string elements for a word that starts with an arbitrary substring? similar to a list search like this: for word in list: if word[:len(substring)] == substring:

OpenStudy (anonymous):

you can iterate with a set just like a list - for word in set: the startswith method would work http://docs.python.org/library/stdtypes.html#str.startswith

OpenStudy (anonymous):

here is basically the same thing using a list comprehension: if [word for word in wordlist if word.startswith(substring)]: the list comprehension will produce an empty list if it doesn't find a word. if it does find a word it will produce a list with all words that start with the substring. when used in an if statement, an empty list evaluates to False http://docs.python.org/library/stdtypes.html#truth-value-testing http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#truth-values

OpenStudy (anonymous):

thought i better test that to make sure it works and it did :) http://ideone.com/OLyiF i don't use list comprehensions much because i always forget about them

OpenStudy (anonymous):

yeah, I was looking for set comprehensions, not sure if they worked the same, apparently they do! Set comps check all items at once right? As opposed to a list that has to look one item at a time?

OpenStudy (anonymous):

beats me.... seems that a processor can only do one thing at a time ..

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!