If you want to sort by the count and not the keys. # ascending sorted_by_count = dict(sorted(frequency.items(), key=lambda count: count[1]) # descending sorted_by_count = dict(sorted(frequency.items(), key=lambda count: count[1], reverse=True) for word, count in sorted_by_count.items(): print(f"{word} = {count}")
Don't get in the habit of using except without using an type. A blind except like that will cause you to miss other errors and make your code very hard to debug. try: frequency [word] += 1 except KeyError: frequency [word] = 1
We can use replace method like sentence.replace(".", "").split() Also replace method can be used multiple times in the above expression because replace() simply returns a string.
I think that you got several emails during the vid, for instance in 5:36 in the cli you got the following message "you have new mail in var/mail/zander". I believe that you have configured and email services maybe postfix or something like that. Based on the message, I got the question how do you do that?
@@treblaoj Honestly, I noticed it but didn't follow up on it. Here is some info superuser.com/questions/306163/what-is-the-you-have-new-mail-message-in-linux-unix. If it happens again I will investigate!
we can also do this but it will print the word and its frequency every time. data=input("ENTER YOUR DATA : ") data_=data.replace(',',' ').replace('.',' ').split() for word in data_: frequency=data_.count(word) print(f'{word} : {frequency}')
If you want to sort by the count and not the keys.
# ascending
sorted_by_count = dict(sorted(frequency.items(), key=lambda count: count[1])
# descending
sorted_by_count = dict(sorted(frequency.items(), key=lambda count: count[1], reverse=True)
for word, count in sorted_by_count.items():
print(f"{word} = {count}")
you should use Counter available in the collections and pass the list to it.
x=input()
fre={}
for i in x.split():
fre[i] = x.count(i)
print(fre)
Within for loop body , I used a try-except block like:
try:
frequency [word] += 1
except:
frequency [word] = 1
Don't get in the habit of using except without using an type. A blind except like that will cause you to miss other errors and make your code very hard to debug.
try:
frequency [word] += 1
except KeyError:
frequency [word] = 1
It breaks if a sentence contains some word just before a comma or any other symbol.
"Hary, John and Hary live in same town"
Thank you Santosh! Would you like to suggest a solution? Ready when you are if you need one.
We can use replace method like sentence.replace(".", "").split()
Also replace method can be used multiple times in the above expression because replace() simply returns a string.
How do you configure to get email in var? It looks quite interesting
Not sure what you mean, could you elaborate please
I think that you got several emails during the vid, for instance in 5:36 in the cli you got the following message "you have new mail in var/mail/zander". I believe that you have configured and email services maybe postfix or something like that. Based on the message, I got the question how do you do that?
@@treblaoj Honestly, I noticed it but didn't follow up on it. Here is some info superuser.com/questions/306163/what-is-the-you-have-new-mail-message-in-linux-unix. If it happens again I will investigate!
@@veryacademy thank you so much!
we can also do this but it will print the word and its frequency every time.
data=input("ENTER YOUR DATA : ")
data_=data.replace(',',' ').replace('.',' ').split()
for word in data_:
frequency=data_.count(word)
print(f'{word} : {frequency}')