简化 Python 中的表达式

发布于 2024-11-16 11:14:38 字数 486 浏览 9 评论 0原文

我觉得必须有一种更简单/更干净/更快(选择一个或多个)的方法来编写这个表达式......

采用 BigString =“这是一个关于一只名叫花花公子的红猫的长句子。”

和 LittleStringList = [ "red dog", "red cat", "red mouse" ]

我实际上想要一个当 LittleStringList 之一位于 BigString 中时返回 true 的函数/表达式。我是这样写的:

def listcontains(list, big):
    contains = False
    for string in list:
        if string in big:
            contains = True
        else:
            pass
    return contains

感谢任何帮助!谢谢。

编辑:修正了一个小错误!

I feel there must be a simpler/cleaner/faster (choose one or more) way to write this expression...

take a BigString = "This is a long sentence about a red cat named dude."

and LittleStringList = [ "red dog", "red cat", "red mouse" ]

I effectively want a function/expression that returns true when one of LittleStringList is in BigString. I wrote it like this:

def listcontains(list, big):
    contains = False
    for string in list:
        if string in big:
            contains = True
        else:
            pass
    return contains

Any help is appreciated! Thanks.

edit: Fixed a small error!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

吐个泡泡 2024-11-23 11:14:38

any([BigString 中的 s for LittleStringList 中的 s])

或者使用生成器表达式更好 - 正如 @GWW 所指出的:

any(BigString 中的 s for LittleStringList 中的 s) >

any([s in BigString for s in LittleStringList])

or even better using a generator expression - as pointed out by @GWW:

any(s in BigString for s in LittleStringList)

妄想挽回 2024-11-23 11:14:38

使用任何():

>>> BigString = "This is a long sentence about a red cat named dude."
>>> LittleStringList = [ "red dog", "red cat", "red mouse" ]
>>> any([str in BigString for str in LittleStringList])
True

>>> BigString = "This is a long sentence about a red bear named dude."
>>> any([str in BigString for str in LittleStringList])
False

use any():

>>> BigString = "This is a long sentence about a red cat named dude."
>>> LittleStringList = [ "red dog", "red cat", "red mouse" ]
>>> any([str in BigString for str in LittleStringList])
True

>>> BigString = "This is a long sentence about a red bear named dude."
>>> any([str in BigString for str in LittleStringList])
False
岛歌少女 2024-11-23 11:14:38

我假设你的意思是if string in big
也许然后尝试:

def listcontains(list, big):
    return any([string in big for string in list])

或者带有生成器的版本:

def listcontains(list, big):
    def gen():
        for s in list:
            yield s in big
    return any(gen())

I assume you mean if string in big?
Maybe then try:

def listcontains(list, big):
    return any([string in big for string in list])

Or a version with a generator:

def listcontains(list, big):
    def gen():
        for s in list:
            yield s in big
    return any(gen())
爱给你人给你 2024-11-23 11:14:38
  1. 您不需要遍历整个列表,在第一个匹配时返回
  2. 不要使用 string 作为变量名,它是一个模块,str 是一种类型,更好的词会是单词
  3. 不要使用 list 作为变量名
  4. 您正在循环 list 并再次检查 list,而不是 big

so

def listcontains(words, big):
    for word in words:
        if word in big:
            return True
    return False
  1. You don't need to go thru whole list, return on first match
  2. Do not use string as variable name, it is a module, str is a type, better word would be word.
  3. Do not use list as variable name
  4. You are looping on list and checking again in list, instead of big

so

def listcontains(words, big):
    for word in words:
        if word in big:
            return True
    return False
风为裳 2024-11-23 11:14:38

为了缩短它,你可以写:

def listcontains(list, big):
    for s in list:
        if s in big:
            return True
    return False

To shorten it a little you could instead write:

def listcontains(list, big):
    for s in list:
        if s in big:
            return True
    return False
往事随风而去 2024-11-23 11:14:38

any(filter(lambda x: x in BigString, LittleStringList))

过滤器将返回 BigString 中包含 LittleStringList 单词的列表,如果过滤器返回出现某种情况的列表,则 any 将返回 true

any(filter(lambda x: x in BigString, LittleStringList))

filter will return a list with LittleStringList words inside BigString and any will return true if filter returns a list with some occurence

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文