字符串中短语之前的前序单词数

时间:2018-09-05 09:14:22

标签: python list

假设我有一个短语列表:

list = ['new york', 'school', 'new']

和一个字符串

text = 'i am going to a school in new york and therefore i have to buy a new uniform to go to new york'

我想找到每个短语之前的单词数量(仅针对首次出现),即输出应为:

new york = 7
school = 5
new = 7

有什么想法可以有效地实现这一目标吗?

3 个答案:

答案 0 :(得分:0)

天真的方法,不考虑任何性能或NLP:

lst = ['new york', 'school', 'new']  # do not use 'list' as a name
text = 'i am going to a school in new york and therefore i have to buy a new uniform to go to new york'

{p: len(text[:text.find(p)].strip().split()) for p in lst}
# {'new york': 7, 'school': 5, 'new': 7}

答案 1 :(得分:0)

使用countindex

lst = ['new york', 'school', 'new']
text = 'i am going to a school in new york and therefore i have to buy a new uniform to go to new york'

for x in lst:
    print(f"{x} = {text.count(' ', 0, text.index(x))}")

# new york = 7
# school = 5                                                   
# new = 7

count从开始算起text中的空格,直到遇到与该短语之前的单词数量相同的短语为止。

答案 2 :(得分:0)

lst = ['new york', 'school', 'new']
text = 'i am going to a school in new york and therefore i have to buy a new uniform to go to new york'

这将为您提供要搜索其计数和字符串数的字符串

for x in lst:
    print(x +": "+str(len(text[0:text.index(x)].split(' ')) -1))