Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
2 ways: set & dict/zip solution in Clear category for Popular Words by Striga
def popular_words(text: str, words: list) -> dict:
t=text.lower().split()
return {w : t.count(w) for w in words}
#Solution 2:
def popular_words(text: str, words: list) -> dict:
l=[]
t=text.lower().split()
for w in words:
l.append(t.count(w))
return dict(zip(words, l))
if __name__ == '__main__':
assert popular_words('''
When I was One
I had just begun
When I was Two
I was nearly new
''', ['i', 'was', 'three', 'near']) == {
'i': 4,
'was': 3,
'three': 0,
'near': 0
}
Oct. 11, 2020
Comments: