简体   繁体   English

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

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

I have list of strings in that i need to find out 'American' is in that string or not. 我有字符串列表,我需要找出'American'是否在该字符串中。 If it exists, then I want to find out starting and ending index of the American word 如果它存在,那么我想找出美国单词的起始和结束索引

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

Desired output: find out starting and ending index of the American word 期望的输出:找出美国单词的开始和结束索引

8,16
75,83
30,38

You can use re.search , which returns a match object with a start method and an end method that return what you're looking for: 您可以使用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')

This outputs: 这输出:

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

I think you should take a look at str.find method : https://docs.python.org/3/library/stdtypes.html#str.find 我想你应该看看str.find方法: https ://docs.python.org/3/library/stdtypes.html#str.find

Example : 示例:

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

Loop on your list to get what you want. 在列表中循环以获得您想要的内容。

Hope this is helpful 希望这是有帮助的

You can use something like str.find(search_item) 你可以使用类似str.find(search_item)东西

this will return the first index value that the search item appears, then you could just return the index + len(search_item) 这将返回搜索项出现的第一个索引值,然后您可以返回index + len(search_item)

something like : 就像是 :

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])

output: 输出:

world

search_index = 6
search_index_end = 11

Using re and list comprehension. 使用re和list理解。 Inspired by @blhsing's solution 灵感来自@ 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))

This could be another approach: 这可能是另一种方法:

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.

相关问题 如何在 python 中自动查找列表中元素的开始和结束索引 - How to find the starting and ending index of elements in list automatically in python 如何找到 python 中整数列表的最大总和,从任何索引开始并在任何结束索引处结束而不进行排序 - How to find the largest sum of a list of integers in python starting from any index and end at any ending index without sorting 如何在 Python 中找到列表的中间元素/索引? - How to find the middle element/index of a list in Python? 如何在列表Python中查找元素的所有索引 - how to find all the index of an element in a list Python 如何在不使用起始索引或范围作为参数的情况下找到单词中第二次或第三次出现的索引号? (蟒蛇) - How to find the index number of second or third occurence of a word in a sentence, without using starting index or range as parameter? (python) 在 Python 中查找列表元素的索引 - Find the index of a list element in Python 如何在Python中该列表的子列表中最小的列表中找到元素的索引? - How to find the index of an element in a list which is minimum in a sublist of that list in Python? 在列表中查找子列表的开始和结束索引 - Find starting and ending indices of sublist in list 如何查找列表元素的索引 - How to find index of a list element 如何找到列表元素的-index? - How to find the -index of an element of a list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM