+ 1
can anyone explain me this get() code?
Please expain me how this is counting the repeated names i'm getting stucked here. https://code.sololearn.com/cTtIf5ZZ0WGP/?ref=app
2 Respuestas
+ 2
name are values from list, using as key for dictionary.
c.get(name,0) will return key value of name in c dictionary if exists otherwise return 0(2nd argument as default value).
+ 2
Mr. 12
get is usually used to retrieve a key or confirm membership in a dictionary. It usually comes in pairs, like get(key, return value if key is not found).
So in your code:
c[name] = c.get(name,0) + 1
c[name] means make a key and assign a value 0 if the key is not in the dictionary else add 1 to the value of the key if it does exist.
Hope this clarifies some things 😃😃