15 Sept 2023

Posted on:

14 Sept 2023

0

HI,

i would love to ask a couple of questions for better understanding:

-zip(*letter_count_clean.items()) ---- what does the * do ?

-plt.show(x,y) what does .show do since it the plots appear even if we dont use it ?

-     letter_count[letter.upper()]= letter_count.get(letter,0)+1 ------ why doesn't upper function give the same results as lower :

{'I': 1, 'T': 1, ' ': 10, 'S': 1, 'N': 1, 'O': 1, 'H': 1, 'G': 1, 'D': 1, 'E': 1, ';': 1, 'R': 1, 'A': 1, 'F': 1, 'U': 1, 'L': 1, 'V': 1, '.': 1}(upper 's dictionary)

{'i': 7, 't': 6, ' ': 10, 's': 2, 'n': 3, 'o': 4, 'h': 1, 'g': 1, 'd': 3, 'e': 3, ';': 1, 'r': 1, 'a': 1, 'f': 1, 'u': 1, 'l': 2, 'v': 1, '.': 1}(lower's dictionary)

the full code source :

-------------------------------------------------------

Posted on:

14 Sept 2023

0

import matplotlib.pyplot as plt

quote ='''It is nothing to die; it is dreadful not to live.'''

letter_count = {}

for letter in quote:
letter_count[letter.upper()]= letter_count.get(letter,0)+1

print(letter_count)

x,y = zip(*letter_count.items())

plt.bar(x,y)
plt.show(x,y)

Instructor
Posted on:

15 Sept 2023

0

Hi Amazigh,

Good to hear from you. Here are some answers to your questions:

1) The * in this context is called the "unpacking" operator. When placed before an iterable in a function call, it unpacks the iterable into individual elements.

For example, consider the dictionary:

dict_example={’a’:1,’b’:2}

If you call dict_example.items(), you'll get a set of key-value pairs like this:

[(’a’,1),(’b’,2)]

Now, zip(*dict_example.items()) will effectively be the same as calling:

zip((’a’,1),(’b’,2))
This will separate the keys and values into two separate tuples, i.e., ('a', 'b') and (1, 2).

2) The function plt.show() in matplotlib is used to display the plot. In some environments, like Jupyter notebooks, the plots may automatically be displayed without needing to call plt.show(). However, in other environments or when running scripts, you may need to explicitly call plt.show() to visualize the plot. It's generally a good practice to include it to ensure the plot is displayed regardless of the environment.

3)

The difference in results between using upper() and lower() is because of the way alphabets are treated in English . For instance, both "I" and "i" will be converted to "I" when using upper(), but only "i" will be converted to "i" when using lower(). This can lead to different counts for letters when analyzing text.

The text had both uppercase and lowercase versions of certain letters. When using upper(), both versions are converted to uppercase, leading to a different count than when both versions are converted to lowercase using lower().