gpt4 book ai didi

python - 如果大海捞针

转载 作者:行者123 更新时间:2023-11-28 20:05:53 25 4
gpt4 key购买 nike

我知道我能做到:

if 'hello' in 'hello world':

如果我有几个针,比如 ('.css', '.js', '.jpg', '.gif', '.png', '.com') 我想检查其中一个这些在一个字符串中。

(注意:endswith在这种情况下不会这样做,它们可能不是后缀)

最佳答案

您可能会发现 any有用:

haystack = 'hello world'
needles = ['.css', '.js', '.jpg', '.gif', '.png', '.com']
if any(needle in haystack for needle in needles):
pass # ...

关于python - 如果大海捞针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26600557/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com