有网友碰到这样的问题“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]))
输出:
Copyright © 2019- 517ttc.cn 版权所有 赣ICP备2024042791号-8
违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务