将字符串与数组中的所有值进行比较

时间:2022-11-29 20:46:39

I am trying to fumble through python, and learn the best way to do things. I have a string where I am doing a compare with another string to see if there is a match:

我试图通过python摸索,并学习做事的最佳方法。我有一个字符串,我正在与另一个字符串进行比较,看看是否有匹配:

if paid[j].find(d)>=0:
    #BLAH BLAH

If 'd' were an array, what is the most efficient way to see if the string contained in paid[j] has a match to any value in 'd'?

如果'd'是一个数组,那么查看付费[j]中包含的字符串是否与'd'中的任何值匹配的最有效方法是什么?

4 个解决方案

#1


40  

If you only want to know if any item of d is contained in paid[j], as you literally say:

如果您只想知道付款[j]中是否包含任何d项,您可以说:

if any(x in paid[j] for x in d): ...

If you also want to know which items of d are contained in paid[j]:

如果您还想知道付费[j]中包含哪些d项:

contained = [x for x in d if x in paid[j]]

contained will be an empty list if no items of d are contained in paid[j].

如果pay [j]中不包含任何d项,则contains将是一个空列表。

There are other solutions yet if what you want is yet another alternative, e.g., get the first item of d contained in paid[j] (and None if no item is so contained):

还有其他解决方案,如果您想要的是另一种替代方案,例如,获取付费[j]中包含的第一项d(如果没有项目包含,则为无):

firstone = next((x for x in d if x in paid[j]), None)

BTW, since in a comment you mention sentences and words, maybe you don't necessarily want a string check (which is what all of my examples are doing), because they can't consider word boundaries -- e.g., each example will say that 'cat' is in 'obfuscate' (because, 'obfuscate' contains 'cat' as a substring). To allow checks on word boundaries, rather than simple substring checks, you might productively use regular expressions... but I suggest you open a separate question on that, if that's what you require -- all of the code snippets in this answer, depending on your exact requirements, will work equally well if you change the predicate x in paid[j] into some more sophisticated predicate such as somere.search(paid[j]) for an appropriate RE object somere. (Python 2.6 or better -- slight differences in 2.5 and earlier).

顺便说一句,因为在评论中你提到了句子和单词,也许你不一定需要字符串检查(这是我所有的例子都在做),因为他们不能考虑单词边界 - 例如,每个例子都会说'cat'处于'混淆'(因为''混淆''包含'cat'作为子串)。为了允许检查单词边界,而不是简单的子字符串检查,你可以高效地使用正则表达式...但是我建议你打开一个单独的问题,如果这是你需要的 - 这个答案中的所有代码片段,取决于如果你将付费[j]中的谓词x更改为某些更复杂的谓词,例如somere.search(付费[j])以获得适当的RE对象,那么你的确切要求将同样有效。 (Python 2.6或更高版本 - 2.5及更早版本中的细微差别)。

If your intention is something else again, such as getting one or all of the indices in d of the items satisfying your constrain, there are easy solutions for those different problems, too... but, if what you actually require is so far away from what you said, I'd better stop guessing and hope you clarify;-).

如果你的意图又是另一回事,比如获得满足你约束条件的d中的一个或所有索引,那么对于那些不同的问题也有简单的解决方案......但是,如果你实际需要的是如此遥远的话从你所说的,我最好不要再猜测,希望你澄清;-)。

#2


8  

I assume you mean list and not array? There is such a thing as an array in Python, but more often than not you want a list instead of an array.

我假设你的意思是列表而不是数组? Python中有一个数组这样的东西,但通常你需要一个列表而不是一个数组。

The way to check if a list contains a value is to use in:

检查列表是否包含值的方法是:

if paid[j] in d:
    # ...

#3


3  

In Python you may use the in operator. You can do stuff like this:

在Python中,您可以使用in运算符。你可以做这样的事情:

>>> "c" in "abc"
True

Taking this further, you can check for complex structures, like tuples:

更进一步,您可以检查复杂的结构,如元组:

>>> (2, 4, 8) in ((1, 2, 3), (2, 4, 8))
True

#4


0  

for word in d:
    if d in paid[j]:
         do_something()

will try all the words in the list d and check if they can be found in the string paid[j].

将尝试列表d中的所有单词并检查它们是否可以在字符串paid [j]中找到。

This is not very efficient since paid[j] has to be scanned again for each word in d. You could also use two sets, one composed of the words in the sentence, one of your list, and then look at the intersection of the sets.

这不是非常有效,因为必须为d中的每个单词再次扫描付费[j]。您还可以使用两个集合,一个由句子中的单词组成,一个列表,然后查看集合的交集。

sentence = "words don't come easy"
d = ["come", "together", "easy", "does", "it"]

s1 = set(sentence.split())
s2 = set(d)

print (s1.intersection(s2))

Output:

{'come', 'easy'}

#1


40  

If you only want to know if any item of d is contained in paid[j], as you literally say:

如果您只想知道付款[j]中是否包含任何d项,您可以说:

if any(x in paid[j] for x in d): ...

If you also want to know which items of d are contained in paid[j]:

如果您还想知道付费[j]中包含哪些d项:

contained = [x for x in d if x in paid[j]]

contained will be an empty list if no items of d are contained in paid[j].

如果pay [j]中不包含任何d项,则contains将是一个空列表。

There are other solutions yet if what you want is yet another alternative, e.g., get the first item of d contained in paid[j] (and None if no item is so contained):

还有其他解决方案,如果您想要的是另一种替代方案,例如,获取付费[j]中包含的第一项d(如果没有项目包含,则为无):

firstone = next((x for x in d if x in paid[j]), None)

BTW, since in a comment you mention sentences and words, maybe you don't necessarily want a string check (which is what all of my examples are doing), because they can't consider word boundaries -- e.g., each example will say that 'cat' is in 'obfuscate' (because, 'obfuscate' contains 'cat' as a substring). To allow checks on word boundaries, rather than simple substring checks, you might productively use regular expressions... but I suggest you open a separate question on that, if that's what you require -- all of the code snippets in this answer, depending on your exact requirements, will work equally well if you change the predicate x in paid[j] into some more sophisticated predicate such as somere.search(paid[j]) for an appropriate RE object somere. (Python 2.6 or better -- slight differences in 2.5 and earlier).

顺便说一句,因为在评论中你提到了句子和单词,也许你不一定需要字符串检查(这是我所有的例子都在做),因为他们不能考虑单词边界 - 例如,每个例子都会说'cat'处于'混淆'(因为''混淆''包含'cat'作为子串)。为了允许检查单词边界,而不是简单的子字符串检查,你可以高效地使用正则表达式...但是我建议你打开一个单独的问题,如果这是你需要的 - 这个答案中的所有代码片段,取决于如果你将付费[j]中的谓词x更改为某些更复杂的谓词,例如somere.search(付费[j])以获得适当的RE对象,那么你的确切要求将同样有效。 (Python 2.6或更高版本 - 2.5及更早版本中的细微差别)。

If your intention is something else again, such as getting one or all of the indices in d of the items satisfying your constrain, there are easy solutions for those different problems, too... but, if what you actually require is so far away from what you said, I'd better stop guessing and hope you clarify;-).

如果你的意图又是另一回事,比如获得满足你约束条件的d中的一个或所有索引,那么对于那些不同的问题也有简单的解决方案......但是,如果你实际需要的是如此遥远的话从你所说的,我最好不要再猜测,希望你澄清;-)。

#2


8  

I assume you mean list and not array? There is such a thing as an array in Python, but more often than not you want a list instead of an array.

我假设你的意思是列表而不是数组? Python中有一个数组这样的东西,但通常你需要一个列表而不是一个数组。

The way to check if a list contains a value is to use in:

检查列表是否包含值的方法是:

if paid[j] in d:
    # ...

#3


3  

In Python you may use the in operator. You can do stuff like this:

在Python中,您可以使用in运算符。你可以做这样的事情:

>>> "c" in "abc"
True

Taking this further, you can check for complex structures, like tuples:

更进一步,您可以检查复杂的结构,如元组:

>>> (2, 4, 8) in ((1, 2, 3), (2, 4, 8))
True

#4


0  

for word in d:
    if d in paid[j]:
         do_something()

will try all the words in the list d and check if they can be found in the string paid[j].

将尝试列表d中的所有单词并检查它们是否可以在字符串paid [j]中找到。

This is not very efficient since paid[j] has to be scanned again for each word in d. You could also use two sets, one composed of the words in the sentence, one of your list, and then look at the intersection of the sets.

这不是非常有效,因为必须为d中的每个单词再次扫描付费[j]。您还可以使用两个集合,一个由句子中的单词组成,一个列表,然后查看集合的交集。

sentence = "words don't come easy"
d = ["come", "together", "easy", "does", "it"]

s1 = set(sentence.split())
s2 = set(d)

print (s1.intersection(s2))

Output:

{'come', 'easy'}