繁体   English   中英

检查列表并在python中拆分句子

[英]Check the list and split the sentence in python

我的清单如下。

mylist = ['test copy', 'test project', 'test', 'project']

我想看看我的句子中是否包含上述mylist元素,并从第一个匹配项中拆分出该句子并获得其第一部分。

例如:

mystring1 = 'it was a nice test project and I enjoyed it a lot'

输出应该是: it was a nice

mystring2 = 'the example test was difficult'

输出应为: the example

我当前的代码如下。

for sentence in L:
    if mylist in sentence:
        splits = sentence.split(mylist)
        sentence= splits[0]

但是,我收到一条错误消息,提示TypeError: 'in <string>' requires string as left operand, not list 有没有办法解决这个问题?

您需要另一个for循环来遍历mylist每个字符串。

mylist = ['test copy', 'test project', 'test', 'project']
mystring1 = 'it was a nice test project and I enjoyed it a lot'
mystring2 = 'the example test was difficult'

L = [mystring1, mystring2]
for sentence in L:
    for word in mylist:
        if word in sentence:
            splits = sentence.split(word)
            sentence= splits[0]
            print(sentence)
# it was a nice 
# the example

可能最有效的方法是首先构建一个正则表达式,该正则表达式同时测试所有字符串:

import re

split_regex = re.compile('|'.join(re.escape(s) for s in mylist))

for sentence in L:
    first_part = split_regex.split(sentence, 1)[0]

这样产生:

>>> split_regex.split(mystring1, 1)[0]
'it was a nice '
>>> mystring2 = 'the example test was difficult'
>>> split_regex.split(mystring2, 1)[0]
'the example '

如果可能的字符串数量很多,则正则表达式通常可以胜过单独搜索每个字符串。

您可能还希望.strip()字符串(删除字符串的.strip()和结尾的空格):

import re

split_regex = re.compile('|'.join(re.escape(s) for s in mylist))

for sentence in L:
    first_part = split_regex.split(sentence, 1)[0].strip()
mylist = ['test copy', 'test project', 'test', 'project']
L = ['it was a nice test project and I enjoyed it a lot','a test copy']
for sentence in L:
    for x in mylist:
        if x in sentence:
            splits = sentence.split(x)
            sentence= splits[0]
            print(sentence)

该错误表明您正在尝试检查句子列表。 因此您必须迭代list的元素。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM