从字符串列表中获取拆分句子的索引
get index of splited sentences from a string 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))
Yahli,希望这对你有所帮助。
编辑:
你的变量有问题。
你的句子必须是一个字符串——而不是一个列表。
编辑您的变量并再次尝试此函数:)
第二次编辑:
我想我终于明白你想做什么了。如果这个效果更好,请告诉我。
第三次编辑:
耶稣,希望这个能解决你的问题。让我知道它是否成功了:)
我只是删除了 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)
去掉标点符号后。您可以使用此代码获取索引,
for i,j in enumerate(structure):
if j in sentence:
print(i)
希望这能解决您的问题。 python 很灵活,还有很多其他解决方案。
所需的结果是一个函数或一种查找字符串列表中句子位置的方法。
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))
Yahli,希望这对你有所帮助。
编辑:
你的变量有问题。 你的句子必须是一个字符串——而不是一个列表。 编辑您的变量并再次尝试此函数:)
第二次编辑: 我想我终于明白你想做什么了。如果这个效果更好,请告诉我。
第三次编辑: 耶稣,希望这个能解决你的问题。让我知道它是否成功了:)
我只是删除了 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)
去掉标点符号后。您可以使用此代码获取索引,
for i,j in enumerate(structure):
if j in sentence:
print(i)
希望这能解决您的问题。 python 很灵活,还有很多其他解决方案。