3 ms·
Instead of this: # Do this lyrics_set = set(lyrics_list) # Linear time set construction words = make_wordlist() for word in words:
by parham 12y ago
Instead of this:
# Do this
lyrics_set = set(lyrics_list) # Linear time set construction
words = make_wordlist()
for word in words:
if word in lyrics_set: # Constant time
print word, "is in the lyrics"
You could do this:
lyrics_set = set(lyrics_list)
words = set(make_wordlist())
matched_words = list(lyrics_set & words)
for word in matched_words:
print word, "is in the lyrics"
- LyndsySimon 12y agoOf, off the top of my head: for word in (set(lyrics_list) & set(words)): print('{} is in the lyrics'.format(word))
- parham 12y agoEven shorter, nice. How about this one liner, the last bit is looking a bit messy any ideas? print " is in the lyrics \n".join([set(lyrics_list) & set(words)]), "is in the lyrics"
- MattConfluence 12y agoHow about >>> lyrics_list = ["her", "name", "is", "rio"] >>> words = ["is", "rio"] >>> print '\n'.join("{} is in the lyrics".format(word) for word in set(lyrics_list) & set(words)) rio is in the lyrics is is in the lyrics
- LyndsySimon 12y agoHeh, I just replied with pretty much this exact code to someone else trying to make it a one-liner. Yes, it's possible. I would never, ever publish code like this, though. It's opaque.
- LyndsySimon 12y agoJust because it can be written as a one-liner doesn't mean it should be written as a one-liner :) Don't ever do this. print('\n'.join(['{} is in the lyrics'.format(word)) for word in (set(lyrics_list) & set(words))])
- parham 12y agoI never said it should, it was a challenge.
- ForHackernews 12y agomatched_words = set(lyrics_list).intersection(make_wordlist())