I found something really strange when I try to return list(dict.values())
.
class Solution:
def groupAnagrams(self, strs):
dict = {}
for i in strs:
key = ''.join(set(i))
if key in dict.keys():
dict[key].append(i)
else:
dict.update({key:[i]})
return list(dict.values())
#The input could be ["eat","tea","tan","ate","nat","bat"]
#And it's supposed to get the result: [["bat"],["nat","tan"],["ate","eat","tea"]]
The thing is that , when I print list(dict.values())
, the result is the same as the correct answer. But when I do return list(dict.values())
, everytime the result is different. It could be [["nat"],["tan"],["eat","tea","ate"],["bat"]]
, or [['eat'], ['tea'], ['tan'], ['ate'], ['nat'], ['bat']]
and etc. I am so confused about why this happened.