簡體   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