Python 按单词的位置选择字符串
对于以下元组Python 按单词的位置选择字符串,python,string,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' 如何只选择字符串“我有一只猫”,“我有一只狗”,也就是说,排除了中间有单词“狗”或“猫”的字符串。 如果该字符串应该用一个特定的短语结束,那么这将完成任务: phases = ("I have a cat", "I have a dog") for sentenc
mysentence = 'i have a dog and a cat', 'i have a cat and a dog', 'i have a cat',
'i have a dog'
如何只选择字符串“我有一只猫”,“我有一只狗”,也就是说,排除了中间有单词“狗”或“猫”的字符串。
如果该字符串应该用一个特定的短语结束,那么这将完成任务: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)
如果字符串应以特定短语结尾,则此操作将完成:
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)
你可以用正则表达式来匹配你不想要的句子 我们可以建立如下模式: 我们想和狗或猫-狗-猫比赛 后跟空格,即不在行的末尾 因此,我们的代码如下所示:
>>> 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']
你可以用正则表达式来匹配你不想要的句子 我们可以建立如下模式: 我们想和狗或猫-狗-猫比赛 后跟空格,即不在行的末尾 因此,我们的代码如下所示:
>>> 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']
您可以使用正则表达式来实现这一点。regex.+dog | cat.+将匹配一个或多个字符,后跟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)
您可以使用正则表达式来实现这一点。regex.+dog | cat.+将匹配一个或多个字符,后跟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)
可能起作用的最简单的事情:
In [10]: [phrase for phrase in mysentence if not ' and ' in phrase]
Out[10]: ['i have a cat', 'i have a dog']
可能起作用的最简单的事情:
In [10]: [phrase for phrase in mysentence if not ' and ' in phrase]
Out[10]: ['i have a cat', 'i have a dog']
您可以使用regexp或string方法 我看到另一个是用正则表达式回答的,所以我尝试了string方法:with string.find,您将获得子字符串在string中的位置。然后检查它是否在句子的中间。
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'])))
当句子中只有“cat”时,您还必须定义要执行的操作。您可以使用regexp或string方法
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'])))
for items in mysentence:
if (items.find("dog")>=0)^(items.find("cat")>=0):
print(items)
我看到另一个是用正则表达式回答的,所以我尝试了string方法:with string.find,您将获得子字符串在string中的位置。然后检查它是否在句子的中间。
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'])))
你们还必须定义当你们的句子中只有“猫”时该做什么
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'])))
for items in mysentence:
if (items.find("dog")>=0)^(items.find("cat")>=0):
print(items)
您只需要一个xor运算符和find函数。不需要进口
您只需要一个xor运算符和find函数。不需要导入“狗”或“猫”之类的句子吗?用“狗”或“猫”这样的句子做什么?OP想排除中间有单词“狗”或“猫”的字符串,所以你的答案会失败。例如,你有一只猫,我有一只狗。OP想排除中间有单词狗或猫的字符串,所以你的答案会失败,例如你有一只猫,我有一只狗。