def f(s, d): for k in d.keys(): d[k] = 0 for c in s: if c in d: d[c] += 1 else: d[c] = 0 return d Does the return function return the key of d or value of d? and WHY? Thanks!
what is s? a list of some sort?
s is a list of strings, d is dictionary.
this function returns d, the dictionary, after some modification to the original input. You should break up the code and comment on each section to walk through it more clearly so you can see exactly what it does.
def f(s, d): for k in d.keys(): # iterates over a list of the keys of the dict d[k] = 0 # sets each value for each key to 0 for c in s: #iterates over each element in the list of strings if c in d: #if an element in the list is a key in the dict d[c] += 1 #add 1 to the value in the dict for that key else: d[c] = 0 #otherwise add that key to the dict and set it to 0 return d #return the dictionary
so it returns neither the keys, nor values of d, but d itself in its entirety, albeit modified
Join our real-time social learning platform and learn together with your friends!