当我尝试返回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"]]
问题是,当我打印list(dict.values())
时,结果与正确答案相同。但是当我做return list(dict.values())
时,每次结果都不同。它可能是[["nat"],["tan"],["eat","tea","ate"],["bat"]]
,或[['eat'], ['tea'], ['tan'], ['ate'], ['nat'], ['bat']]
等等。我对此为何发生了如此困惑。