Reputation: 2503
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
Upvotes: 3
Views: 2866
Reputation: 464
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
Upvotes: 0
Reputation: 1487
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))
Upvotes: 1
Reputation: 8273
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)]
Upvotes: 1
Reputation: 811
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
Upvotes: 2
Reputation: 106543
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
Upvotes: 5
Reputation: 21
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
Upvotes: 1