Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.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_Python 3.x - Fatal编程技术网

如何在列表中查找以元素开头和结尾的单词索引?python

如何在列表中查找以元素开头和结尾的单词索引?python,python,python-3.x,Python,Python 3.x,我有一个字符串列表,我需要找出“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,

我有一个字符串列表,我需要找出
“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
期望输出:找出美国单词的起始和结束索引

['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

我想你应该看看str.find方法:

例如:

>>> str1 = 'Here in Americans, people say "Can I get a bag for the stuff?"'
>>> str2 = "Americans"
>>> print(str1.find(str2))
8
在列表上循环以获得所需内容


希望这是有帮助的

您可以使用
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(搜索项目)

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

比如:

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
和列表理解。受@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)]

这可能是另一种方法:

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

因为如果句子中有两个单词,它只会显示第一个,好的。但是使用find方法,这是我的答案。提问者想要单词的所有开始和结束索引。您的解决方案只提供第一个起始索引。
find
方法总是返回第一个索引,因此不是这个问题的合适解决方案……好吧。。。如果句子中有两个单词,它只会显示第一个但同时包含“开始”和“结束”的美语单词“开始”和“结束”索引。美国人认为足球默认为美式足球< /代码> @麦迪。