Code Snippet
Code:
from collections import Counter
s1 = "yxxx"
s2 = "xyyy"
map1 = Counter(s1)
map2 = Counter(s2)
print(map1.keys())
print(map2.keys())
print("Are map1's keys and map2's keys equal:", map1.keys() == map2.keys())
print()
print(map1.values())
print(map2.values())
print("Are map1's values and map2's values equal:", map1.values() == map2.values())
print("Are SORTED map1's values and map2's values equal:", sorted(map1.values()) == sorted(map2.values()))
Results:
dict_keys(['y', 'x'])
dict_keys(['x', 'y'])
Are map1's keys and map2's keys equal: True
dict_values([1, 3])
dict_values([1, 3])
Are map1's values and map2's values equal: False
Are SORTED map1's values and map2's values equal: True
I understand that the return type of keys()
function similar to a set-like view, so when evaluating equality the order wouldn’t matter, but how about values()
? Even though the values in map1
and map2
are the same, the value returned is still False
. But why does calling sorted()
return True
although the order of the values remain unchanged?
New contributor
topquartile is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3