- #1
- 7,120
- 10,768
Hi, I am looking into some code in Python 3.7.2 that counts the number of appearances of in a message string.
We are given a string. We then define an empty dictionary to be ultimately filled with the characters as the keys and the values will be (are the ) number of appearences of the characters , i.e., we will go from :
{} to : { Char1: Frequency of Char 1,..., Charn: Frequency of Char n } , where the frequency is the number of appearances of the character in the message string.
Now, I understand why we use count() and set default . QUESTION: I just don't see where we are specifying what the key and values of the dictionary are. How do we know characters will be the keys and their frequency will be the value?( Obviously this is what we want to do, but how am I communicating this to Python in the code I am writing?). Is it in the line of code : count[character]? :' :
EDIT: I am also confused at the fact that we are using the same term : count as the name of the dictionary and as the function(). Isn't 'count' a reserved word since count() is a function, or is it count() that is reserved?
EDIT2: I am also confused at the fact that the syntax for count() I am familiar with is str_i.count(str_i, 1st value, last value) and not the one in the code below.
I ( think I ) understand (please correct me if I am wrong and/ or comment):
We start with a message string. We will count the frequency of each character in the string.
We start with an empty dictionary, which will be updated.
We use setdefault(character,0) to pratctice for cases where a character is not in the message, to avoid exceptions/errors : in case a character does not appear.
We then loop over each character in message, counting the number of appearances of each character..
We are given a string. We then define an empty dictionary to be ultimately filled with the characters as the keys and the values will be (are the ) number of appearences of the characters , i.e., we will go from :
{} to : { Char1: Frequency of Char 1,..., Charn: Frequency of Char n } , where the frequency is the number of appearances of the character in the message string.
Now, I understand why we use count() and set default . QUESTION: I just don't see where we are specifying what the key and values of the dictionary are. How do we know characters will be the keys and their frequency will be the value?( Obviously this is what we want to do, but how am I communicating this to Python in the code I am writing?). Is it in the line of code : count[character]? :' :
EDIT: I am also confused at the fact that we are using the same term : count as the name of the dictionary and as the function(). Isn't 'count' a reserved word since count() is a function, or is it count() that is reserved?
EDIT2: I am also confused at the fact that the syntax for count() I am familiar with is str_i.count(str_i, 1st value, last value) and not the one in the code below.
Python:
message = 'It was a cold day in April and the clocks were striking thirteen. '
count = {}
for character in message:
count.setdefault(character,0)
count[character]= count[character] +1
print(count)
We start with a message string. We will count the frequency of each character in the string.
We start with an empty dictionary, which will be updated.
We use setdefault(character,0) to pratctice for cases where a character is not in the message, to avoid exceptions/errors : in case a character does not appear.
We then loop over each character in message, counting the number of appearances of each character..
Last edited: