Warning: a Python newbie here.
I am trying to convert the following to list comprehension but struggling:-
lorem_ipsum = """
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
"""
def word_count2(str):
counts = dict()
words = str.split()
for word in words:
if word in counts:
counts[word] += 1
else:
counts[word] = 1
return counts
print(word_count2(lorem_ipsum))
So far I have tried a few variations on this:-
aString = lorem_ipsum
counts = dict()
words = aString.split
[counts[word] += 1 if word in counts else counts[word] = 1 for word in words]
Unfortunately, it has been some hours now but nothing I have tried seems to work
Aucun commentaire:
Enregistrer un commentaire