Last answered:

05 Jun 2023

Posted on:

30 Aug 2022

0

Not getting Sherlock example

Dear Sir/Madam,
                             I am not getting this 'Sherlock' example. Please explain this ('letter_count.get(letter,0) + 1').
Thanks & Regards,
Shardul.

2 answers ( 0 marked as helpful)
Instructor
Posted on:

30 Aug 2022

0

Hey Shardul,

Thank you for reaching out!

Let me re-direct you to the following threads, where this part of the course has been discussed:
https://365datascience.com/q/c5e5aaf3d6
https://365datascience.com/q/d1c4088d64

Let me know if there is something that remains unclear regarding the Sherlock example.

Kind regards,
365 Hristina

Posted on:

05 Jun 2023

1

I can try to explain this t you:


letter_count ={} #it is an empty dictionary, which will be used to store information
for letter in sherlock: #with for we are starting our loop
         letter_count[letter.lower()] = letter_count.get(letter, 0)+1  #letter.lower() - converting letters to lower case

Let's visualize this part: letter_count.get(letter, 0)+1 :

Imagine you have a box called letter_count where you want to keep track of how many times each letter appears in your message. You start with an empty box.

Let's say you have a message written on a piece of paper. You want to count how many times each letter appears in that message. So, you go through the message one letter at a time.

For each letter, you check if it's already in the box. If it's there, you look at the count next to that letter. If it's not there, you assume the count is zero.

Then, you take that count and add 1 to it. This means you increase the count by 1 to show that you found another same letter.

print(letter_count)

Submit an answer