繁体   English   中英

如何在列表中找到开始和结束元素的单词索引? 蟒蛇

[英]How to find index of word starting and ending an element in a list? Python

我有字符串列表,我需要找出'American'是否在该字符串中。 如果它存在,那么我想找出美国单词的起始和结束索引

['Here in Americans, people say “Can I get a bag for the stuff?”',
 'Typically in restaurant after you are done with meal, you ask for check in Americans from the waiter.',
 'When mixing coffee, people in American use creamer, which is equivalent of milk.']

期望的输出:找出美国单词的开始和结束索引

8,16
75,83
30,38

您可以使用re.search ,它返回一个带有start方法和end方法的匹配对象,返回您要查找的内容:

import re

l = [
    'Here in Americans, people say “Can I get a bag for the stuff?”',
    'Typically in restaurant after you are done with meal, you ask for check in Americans from the waiter.',
    'When mixing coffee, people in American use creamer, which is equivalent of milk.',
    'Hello World'
]

for string in l:
    match = re.search('American', string)
    if match:
        print('%d,%d' % (match.start(), match.end()))
    else:
        print('no match found')

这输出:

8,16
75,83
30,38
no match found

我想你应该看看str.find方法: https ://docs.python.org/3/library/stdtypes.html#str.find

示例:

>>> str1 = 'Here in Americans, people say "Can I get a bag for the stuff?"'
>>> str2 = "Americans"
>>> print(str1.find(str2))
8

在列表中循环以获得您想要的内容。

希望这是有帮助的

你可以使用类似str.find(search_item)东西

这将返回搜索项出现的第一个索引值,然后您可以返回index + len(search_item)

就像是 :

string = "Hello world!"
search_item = "world"
search_index = string.find(search_item)
search_index_end = search_index+len(search_item)

print(string[search_index] : search_index_end])

输出:

world

search_index = 6
search_index_end = 11

使用re和list理解。 灵感来自@ blhsing的解决方案

import re
a=['Here in Americans, people say “Can I get a bag for the stuff?”',
 'Typically in restaurant after you are done with meal, you ask for check in Americans from the waiter.',
 'When mixing coffee, people in American use creamer, which is equivalent of milk.']

regex  = re.compile('American')

[(match.start(), match.end())  for i in a for match in regex.finditer(i)]
string=['Here in Americans, people say “Can I get a bag for the stuff?”',
 'Typically in restaurant after you are done with meal, you ask for check in Americans from the waiter.',
 'When mixing coffee, people in American use creamer, which is equivalent of milk.']

string2="American"

for sentence in string:
    initial=int(sentence.find(string2))
    end_point=initial+len(string2)
    print ("%d,%d"%(initial,end_point))

这可能是另一种方法:

all_data = ['Here in Americans, people say “Can I get a bag for the stuff?”',
    'Typically in restaurant after you are done with meal, you ask for check in Americans from the waiter.',
    'When mixing coffee, people in American use creamer, which is equivalent of milk.']


for data in all_data:
    words = data.split(' ')
    counter = 0
    for position, word in enumerate(words):
        if 'American' in word:
            print('{}, {}'.format(counter, counter+8))
        else:
            counter += len(word) + 1

暂无
暂无

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

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