在多个字典中查找常用字母表-python

在多个字典中查找常用字母表-python,python,dictionary,Python,Dictionary,我正在使用给定字符串生成返回字符的代码。 我知道使用计数器功能更容易使用,但我尽量不使用它 这是我的密码 class Solution: def commonChars(self, A): dic = {} for i in range(len(A)): A[i] = list(A[i]) dic[i] = self.checkLetter(A[i]) print(dic)

我正在使用给定字符串生成返回字符的代码。 我知道使用计数器功能更容易使用,但我尽量不使用它

这是我的密码

class Solution:
    def commonChars(self, A):
        dic = {}
        for i in range(len(A)):
            A[i] = list(A[i])
            dic[i] = self.checkLetter(A[i])
        print(dic)
        
    def checkLetter(self, Letter_list) : 
        letter_cnt = {}
        for l in Letter_list:
            if l in letter_cnt : letter_cnt[l] += 1
            else : letter_cnt[l] = 1
        return letter_cnt
我已经用字典做了一个字母计数器,但我不知道下一步该怎么做。你能给我一个提示吗

# given input_1 : ["bella","label","roller"]
# expected output is e,l,l because it is common in every string in the given list
>>> ["e","l","l"]

# result of my code
>>> {0: {'a': 1, 'b': 1, 'e': 1, 'l': 2}, 1: {'a': 1, 'b': 1, 'e': 1, 'l': 2}, 2: {'r': 2, 'e': 1, 'l': 2, 'o': 1}}

# given input_2 : ["cool","lock","cook"]
# expected output
>>> ["c","o"]

使用
reduce
的可能解决方案。仅添加一行:

from functools import reduce


class Solution:
    def commonChars(self, A):
        dic = {}
        for i in range(len(A)):
            A[i] = list(A[i])
            dic[i] = self.checkLetter(A[i])
        print([char for char, count in reduce(lambda x, y:{k:min(x[k], y[k]) for k,v in x.items() if y.get(k)}, dic.values()).items() for _ in range(count)])

    def checkLetter(self, Letter_list):
        letter_cnt = {}
        for l in Letter_list:
            if l in letter_cnt:
                letter_cnt[l] += 1
            else:
                letter_cnt[l] = 1
        return letter_cnt


s = Solution()
s.commonChars(["bella","label","roller"])
# ['e', 'l', 'l']
s.commonChars(["cool","lock","cook"])
# ['c', 'o']
拆分它:

result_dict = reduce(lambda x, y:{k:min(x[k], y[k]) for k,v in x.items() if y.get(k)}, dic.values())

print([char for char, count in result_dict.items() for _ in range(count)])

您的预期输出是什么?我编辑了我的问题,谢谢@Ns68,你能解释一下你的预期输出吗,为什么
e
在那里,因为
l
在第二和第三节是重复的,但是
e
不是。@Susanth我的错误是,我测试了不同的输入,但忘了更改。@Ns68,仍然为什么
l
是重复的而不是
e