Screen Link: Learn data science with Python and R projects
I try to change my for loop to list comprehension about creating a dictionary. but i am not sure if my code is correct when i use list comprehension. I also dont understand the code below from the solution. parameters_spam = {unique_word:0 for unique_word in vocabulary}
if i want to change it to for loop code…what it will be? Thank you.
My Code:
u={}
for word in vocabulary:
if word in u:
u[word]+=1
else:
u[word]=0
#list comprehension
{(u[word] += 1 if word in u else uu[word]=0) for word in vocabulary }