Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从字符串列表中获取拆分句子的索引_Python_String_Text Mining - Fatal编程技术网

Python 从字符串列表中获取拆分句子的索引

Python 从字符串列表中获取拆分句子的索引,python,string,text-mining,Python,String,Text Mining,期望的结果是一个函数或一种方法,用于查找字符串列表中的句子位置 sentence = 'The cat went to the pool yesterday' structure = ['The cat went,', 'to the pool yesterday.','I wonder if you realize the effect you are having on me. It hurts. A lot.'] 比如说 def findsentence(sentence, list

期望的结果是一个函数或一种方法,用于查找字符串列表中的句子位置

sentence = 'The cat went to the pool yesterday'

structure = ['The cat went,', 'to the pool yesterday.','I wonder if you realize the effect you are having on me. It hurts. A lot.']
比如说

def findsentence(sentence, list of strings):
      # do something to get the output, vec of positions to find the sentence in hte string list
return output

findsentence(sentence, structure)
> (0,1) # beacuse the phrase is splitted in the list...
小心 挑战在于找不到确切的句子。看这个例子,这个句子是句子位置0的一部分,结构位置1的一部分

因此,这不是一个简单的字符串操作问题。

使用以下方法:

sentence = "foo sam bar go"
structure = ["rq", "foo sam", "bar go", "ca", "da"]

def findsentencelist(sentence, list_of_strings):
    l = []
    for item in list_of_strings:
        if item in sentence:
            l.append(list_of_strings.index(item))
    return l

print str(findsentencelist(sentence, structure))
希望这对你有帮助,亚利

编辑:

您的变量有问题。 你的句子必须是字符串,而不是列表。 编辑变量并重试此函数:)

第二次编辑: 我想我终于明白你想做什么了。让我知道这个是否更好用

第三次编辑: 上帝啊,希望这个能解决你的问题。让我知道它是否奏效:)

使用以下方法:

sentence = "foo sam bar go"
structure = ["rq", "foo sam", "bar go", "ca", "da"]

def findsentencelist(sentence, list_of_strings):
    l = []
    for item in list_of_strings:
        if item in sentence:
            l.append(list_of_strings.index(item))
    return l

print str(findsentencelist(sentence, structure))
希望这对你有帮助,亚利

编辑:

您的变量有问题。 你的句子必须是字符串,而不是列表。 编辑变量并重试此函数:)

第二次编辑: 我想我终于明白你想做什么了。让我知道这个是否更好用

第三次编辑:
上帝啊,希望这个能解决你的问题。让我知道它是否奏效:)

我只是删除了
结构上的标点符号以使其正常工作:

sentence = 'The cat went to the pool yesterday'

structure = ['The cat went,', 'to the pool yesterday.','I wonder if you realize the effect you are having on me. It hurts. A lot.','Life is too short as it is. In short, she had a cushion job.']

import string

def findsentence(sentence, list_of_strings):
    return tuple(i for i, s in enumerate(list_of_strings) if s.translate(None, string.punctuation) in sentence)


print findsentence(sentence, structure)
# (0, 1)

我只是删除了
结构上的标点符号
,以使其正常工作:

sentence = 'The cat went to the pool yesterday'

structure = ['The cat went,', 'to the pool yesterday.','I wonder if you realize the effect you are having on me. It hurts. A lot.','Life is too short as it is. In short, she had a cushion job.']

import string

def findsentence(sentence, list_of_strings):
    return tuple(i for i, s in enumerate(list_of_strings) if s.translate(None, string.punctuation) in sentence)


print findsentence(sentence, structure)
# (0, 1)

删除标点符号后。您可以使用此代码获取索引

for i,j in enumerate(structure):
     if j in sentence:
          print(i) 

希望这能解决你的问题。还有很多其他的解决方案,因为python是灵活的。

删除标点后。您可以使用此代码获取索引

for i,j in enumerate(structure):
     if j in sentence:
          print(i) 

希望这能解决你的问题。还有很多其他的解决方案,因为python是灵活的。

确实如此。只需确保句子必须来自字符串类型,而不是列表!设置变量的正确方法是:句子=‘当我思考你在做什么时’。@PeCaDe,我相应地编辑了我的答案。请再试一次,让我知道它是否适合你@佩卡德,现在试试。请下次一定要把你的问题解释清楚:)答案太多了,我无法正确地表达自己。我很高兴能帮上忙!它是。只需确保句子必须来自字符串类型,而不是列表!设置变量的正确方法是:句子=‘当我思考你在做什么时’。@PeCaDe,我相应地编辑了我的答案。请再试一次,让我知道它是否适合你@佩卡德,现在试试。请下次一定要把你的问题解释清楚:)答案太多了,我无法正确地表达自己。我很高兴能帮上忙!