Python Challenge | Word Frequency

Поділитися
Вставка
  • Опубліковано 5 жов 2024

КОМЕНТАРІ • 14

  • @qman2852
    @qman2852 2 роки тому +3

    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}")

  • @MohdTalib-ym7wb
    @MohdTalib-ym7wb 2 роки тому +1

    you should use Counter available in the collections and pass the list to it.

  • @chandramouli8831
    @chandramouli8831 2 роки тому +1

    x=input()
    fre={}
    for i in x.split():
    fre[i] = x.count(i)
    print(fre)

  • @abhishektayal480
    @abhishektayal480 2 роки тому +1

    Within for loop body , I used a try-except block like:
    try:
    frequency [word] += 1
    except:
    frequency [word] = 1

    • @qman2852
      @qman2852 2 роки тому +2

      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

  • @santoshbhattarai2527
    @santoshbhattarai2527 2 роки тому +2

    It breaks if a sentence contains some word just before a comma or any other symbol.
    "Hary, John and Hary live in same town"

    • @veryacademy
      @veryacademy  2 роки тому

      Thank you Santosh! Would you like to suggest a solution? Ready when you are if you need one.

    • @abhishektayal480
      @abhishektayal480 2 роки тому +2

      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.

  • @treblaoj
    @treblaoj 2 роки тому +1

    How do you configure to get email in var? It looks quite interesting

    • @veryacademy
      @veryacademy  2 роки тому +1

      Not sure what you mean, could you elaborate please

    • @treblaoj
      @treblaoj 2 роки тому +1

      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?

    • @veryacademy
      @veryacademy  2 роки тому +1

      @@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!

    • @treblaoj
      @treblaoj 2 роки тому

      @@veryacademy thank you so much!

  • @hassanbutt352
    @hassanbutt352 2 роки тому

    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}')