查找字符串中的数组项

发布于 2024-11-04 09:17:25 字数 241 浏览 4 评论 0原文

我知道可以使用 string.find() 来查找字符串中的子字符串。

但是,在不使用循环的情况下找出数组项之一是否在字符串中具有子字符串匹配的最简单方法是什么?

伪代码:

string = 'I would like an apple.'
search = ['apple','orange', 'banana']
string.find(search) # == True

I know can use string.find() to find a substring in a string.

But what is the easiest way to find out if one of the array items has a substring match in a string without using a loop?

Pseudocode:

string = 'I would like an apple.'
search = ['apple','orange', 'banana']
string.find(search) # == True

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

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

发布评论

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

评论(3

九歌凝 2024-11-11 09:17:25

您可以使用生成器表达式(它在某种程度上是一个循环)

any(x in string for x in search)

生成器表达式是括号内的部分。它创建一个迭代器,为元组 search 中的每个 x 返回 x in string 的值。 x in string 依次返回 string 是否包含子字符串 x。最后,Python 内置 any()迭代它所传递的可迭代对象,如果其任何项的计算结果为 True,则返回。

或者,您可以使用正则表达式来避免循环:

import re
re.search("|".join(search), string)

我会选择第一个解决方案,因为正则表达式有陷阱(转义等)。

You could use a generator expression (which somehow is a loop)

any(x in string for x in search)

The generator expression is the part inside the parentheses. It creates an iterable that returns the value of x in string for each x in the tuple search. x in string in turn returns whether string contains the substring x. Finally, the Python built-in any() iterates over the iterable it gets passed and returns if any of its items evaluate to True.

Alternatively, you could use a regular expression to avoid the loop:

import re
re.search("|".join(search), string)

I would go for the first solution, since regular expressions have pitfalls (escaping etc.).

奶气 2024-11-11 09:17:25

Python 中的字符串是序列,您可以通过询问一个字符串是否存在于另一个字符串中来进行快速成员资格测试:

>>> mystr = "I'd like an apple"
>>> 'apple' in mystr
True

Sven 在上面的第一个答案中得到了正确的答案。要检查其他字符串中是否存在任何多个字符串,您可以这样做:

>>> ls = ['apple', 'orange']
>>> any(x in mystr for x in ls)
True

值得注意的是,供将来参考的是,内置的“all()”函数仅当 all 项在“ls”是“mystr”的成员:

>>> ls = ['apple', 'orange']
>>> all(x in mystr for x in ls)
False
>>> ls = ['apple', 'like']
>>> all(x in mystr for x in ls)
True

Strings in Python are sequences, and you can do a quick membership test by just asking if one string exists inside of another:

>>> mystr = "I'd like an apple"
>>> 'apple' in mystr
True

Sven got it right in his first answer above. To check if any of several strings exist in some other string, you'd do:

>>> ls = ['apple', 'orange']
>>> any(x in mystr for x in ls)
True

Worth noting for future reference is that the built-in 'all()' function would return true only if all items in 'ls' were members of 'mystr':

>>> ls = ['apple', 'orange']
>>> all(x in mystr for x in ls)
False
>>> ls = ['apple', 'like']
>>> all(x in mystr for x in ls)
True
悲欢浪云 2024-11-11 09:17:25

更简单的是

import re
regx = re.compile('[ ,;:!?.:]')

string = 'I would like an apple.'
search = ['apple','orange', 'banana']

print any(x in regx.split(string) for x in search)

编辑

更正,在阅读斯文的答案后:显然,字符串必须不被分割,愚蠢的! any(x in string for x in search) 效果很好

如果你不想循环:

import re
regx = re.compile('[ ,;:!?.:]')

string = 'I would like an apple.'
search = ['apple','orange', 'banana']
print regx.split(string)

print set(regx.split(string)) & set(search)

结果

set(['apple'])

The simpler is

import re
regx = re.compile('[ ,;:!?.:]')

string = 'I would like an apple.'
search = ['apple','orange', 'banana']

print any(x in regx.split(string) for x in search)

EDIT

Correction, after having read Sven's answer: evidently, string has to not be splited, stupid ! any(x in string for x in search) works pretty well

If you want no loop:

import re
regx = re.compile('[ ,;:!?.:]')

string = 'I would like an apple.'
search = ['apple','orange', 'banana']
print regx.split(string)

print set(regx.split(string)) & set(search)

result

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