按字位置选择字符串

时间:2011-12-09 09:11:11

标签: python string

对于以下元组

mysentence = 'i have a dog and a cat', 'i have a cat and a dog', 'i have a cat', 
             'i have a dog'

如何只选择字符串'我有一只猫' ,'我有一只狗,即排除中间带有dogcat字样的字符串。

6 个答案:

答案 0 :(得分:4)

您可以使用正则表达式执行此操作。正则表达式.+(dog|cat).+将匹配一个或多个字符,然后是狗或猫,之后是一个或多个字符。然后,您可以使用过滤器来查找与此正则表达式不匹配的字符串:

import re
regex.compile(r'.+(dog|cat).+')
sentence = 'i have a dog and a cat', 'i have a cat and a dog', 'i have a cat', 
           'i have a dog'
filtered_sentence = filter(lambda s: not regex.match(s), sentence)

答案 1 :(得分:1)

您可以使用正则表达式来匹配您不想要的句子。

我们可以按如下方式构建模式:

  • 我们希望匹配狗或猫 - (dog|cat)
  • 后跟一个空格,即不在该行的末尾

所以我们的代码看起来像这样:

>>> mysentence = ('i have a dog and a cat', 'i have a cat and a dog', 'i have a cat', 'i have a dog')
>>> import re                                                                   
>>> pattern = re.compile("(dog|cat) ")
>>> [x for x in mysentence if not pattern.search(x)]                            
['i have a cat', 'i have a dog']

答案 2 :(得分:0)

如果字符串应该以特定短语结尾,那么这将完成工作:

phases = ("I have a cat", "I have a dog")
for sentence in mysentence:
    for phase in phases:
        if sentence.lower().endswith(phase.lower()):
            print(sentence)

答案 3 :(得分:0)

最简单的事情可能有用:

In [10]: [phrase for phrase in mysentence if not ' and ' in phrase]
Out[10]: ['i have a cat', 'i have a dog']

答案 4 :(得分:0)

您可以使用regexp或string方法。

我看到其他人用正则表达式回答,所以我尝试使用字符串方法:使用string.find(),你将获得字符串中子字符串的位置。然后检查它是否在句子的中间。

def filter_function(sentence, words):
    for word in words:
        p = sentence.find(word)
        if p > 0 and p < len(sentence) - len(word):
            return 0
    return 1

for sentence in mysentence:
    print('%s: %d' % (sentence, filter_function(sentence, ['dog', 'cat'])))

当你在句子里只有'猫'时,你也必须定义该怎么做。

答案 5 :(得分:0)

for items in mysentence:
    if (items.find("dog")>=0)^(items.find("cat")>=0):
        print(items)

您只需要一个xor运算符和find函数。无需导入