+ 1
Dictionary .get() method. Please, some help! Thanks.
The answer for this is 1... I have no idea how this works or why its 1..... Any help would be great. Thanks! def histogram(s): d = {} for c in s: d[c] = d.get(c, 0) + 1 return d print(histogram('hello, world')[' ']) What does this line mean? d[c] = d.get(c, 0) + 1 Get the character of value 0 and add 1? Woudlnt that mean the answe is 2? As there is one space and then we +1?
2 Respostas
+ 2
def histogram(the_sentance):
my_histogram_data = {}
for the_character in the_sentance:
# if the dictionary key does not exist create one
# with a default value = 0
# then increment that key value by 1
my_histogram_data[the_character] = \
my_histogram_data.get(the_character, 0) + 1
return my_histogram_data
result = histogram('hello, world')
for item in result:
print(result[item],item)
# it can often be helpful to give the code words
# meaningful to you when trying to decipher the intent.
0
Thank you. Yeah it's a question on a test. Unfortunately I have to abide by the code they give 😒