+ 1
is it possible to set a dictionnaire for which the Key is a function ? i.e. Can we use dictionnary as à hashmap
By instance: dic ={} dic[len("1234567")] = "1234567" And what if 2 keys are the same? In other words, Can we use dictionnary as a hashmap Thx
2 Antworten
+ 1
Yes you can use functions that return an int value as Keys. But you can not use the same key for 2 or more elements, becouse the last declaration of an element with a given key will ever replace the one before:
squares = {1: 1, 2: 4, 3: "error", 4: 16,}
squares[len("lol")] = "lol"
squares[3]=9
print(squares)
OUTPUT is 1:1,2:4,3:9,4:16
and not 1:1,2:4,3:lol,4:16
becouse:
len("lol") is e qual to 3 and set "lol" as the value for key 3,
next i replace the value "lol" with 9 By re-declareting it.
So as you see, you cannot use a key for more values
0
Thx!