【问题标题】:Find a part of an element of a list [duplicate]查找列表元素的一部分[重复]
【发布时间】:2020-03-29 17:41:20
【问题描述】:

请问您知道在 Python 3.7 中是否可以在列表中找到某个单词? 例如,在列表list = ['hello world', 'hello everyone', 'Freddy Krueger'] 中,让程序只对其中包含单词“hello”的元素执行操作。 对不起我的英语不好,我希望你能帮助我找到解决方案。 祝你有个愉快的夜晚!

【问题讨论】:

  • 如果列表中包含'Othello' 之类的内容怎么办?这应该匹配'hello',还是只想要整个单词?那么区分大小写呢?

标签: python python-3.x list


【解决方案1】:

快速简单的方法是遍历列表中的每个字符串,然后检查该字符串是否包含单词hello

for string in list:
    if 'hello' in string:
        #do something

【讨论】:

    猜你喜欢
    • 2014-04-21
    • 2012-04-11
    • 1970-01-01
    • 2010-10-10
    • 2019-12-31
    • 2021-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多