简体   繁体   中英

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. 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:

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

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)

this will return the first index value that the search item appears, then you could just return the 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. Inspired by @blhsing's solution

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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