Python中一个错误的map遍历

当字典的key 是元组的时候,kv 遍历会出问题

 

class Solution:
    def oddString(self, words):
        mp = dict()
        a = [i for i in range(26)]
        b = [chr(i) for i in range(97, 123)]
        ind = dict(zip(b, a))

        for word in words:
            li = []
            for i in range(1,len(word)):
                li.append(ind.get(word[i])- ind.get(word[i-1]))

            t = tuple(li)
            if t in mp:
                mp[t]=(mp.get(t)[0]+word, mp.get(t)[1]+1)
            else:
                mp[t] = (word, 1)

        for key in mp.keys():
            v = mp.get(key)
            if v[1] == 1:
                return v[0]
        # 这种遍历方式(k,v) 其实是mp中的key元组
        # for k, v in mp:
        #     if v[1] == 1:
        #         return v[0]



if __name__ == '__main__':
    s = Solution()
    print(s.oddString(["adc","wzy","abc"]))

 

posted @ 2023-05-25 13:45  r1-12king  阅读(36)  评论(0编辑  收藏  举报