有网友碰到这样的问题“python如何从字符串中筛选出包含词最多的那个字符串呢!”。小编为您整理了以下解决方案,希望对您有帮助:
解决方案1:
代码如下,仅供参考:
text = """
abcdefg
abcmndy
abcynis
"""
# 需要检测的词
word = ['a', 'b', 'c', 'm', 'n', 'y']
text = text.strip().splitlines()
count = []
for i in text:
x = list(map(i.count, word))
count.append(sum(x))
maxWord = count.index(max(count))
print("包检测词最多的字符串是:{0} 有{1}个".format(text[maxWord], count[maxWord]))
输出: