1 回答

TA贡献1877条经验 获得超6个赞
reduce.only 添加一行的可能解决方案:
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)])
添加回答
举报