2010-09-13 62 views
0

還有更優雅(pythonic +有效)如何找到給定位置上的文字?找文字給定位置文字

FIRST_WORD = re.compile(r'^(\w+)', re.UNICODE) 
LAST_WORD = re.compile(r'(\w+)$', re.UNICODE) 

def _get_word(self, text, position): 
    """ 
    Get word on given position 
    """ 
    assert position >= 0 
    assert position < len(text) 

    # get second part of word 
    # slice string and get first word 
    match = FIRST_WORD.search(text[position:]) 
    assert match is not None 
    postfix = match.group(1) 

    # get first part of word, can be empty 
    # slice text and get last word 
    match2 = LAST_WORD.search(text[:position]) 
    if match2 : prefix = match2.group(1) 
    else : prefix = '' 

    return prefix + postfix 


#         | 21. 
>>> _get_word("Hello, my name is Earl.", 21) 
Earl 
>>> _get_word("Hello, my name is Earl.", 20) 
Earl 

感謝

回答

1

以下是我會做:

s = "Hello, my name is Earl." 
def get_word(text, position): 
    words = text.split() 
    characters = -1 
    for word in words: 
     characters += len(word) 
     if characters > = position: 
      return word 
>>> get_word(s, 21) 
Earl. 

剝離標點符號可以''.strip()或正則表達式或東西哈克像

for c in word: 
    final += c if c.lower() in 'abcdefghijklmnopqrstuvwxyz' 
0
import string 

s = "Hello, my name is Earl." 
def get_word(text, position): 
    _, _, start = text[:position].rpartition(' ') 
    word,_,_ = text[position:].partition(' ') 
    return start+word 

print get_word(s, 21).strip(string.punctuation) 
完成
0

下面的解決方案是讓周圍的給定位置的字母字符:

def get_word(text, position): 
    if position < 0 or position >= len(text): 
     return '' 

    str_list = [] 

    i = position 
    while text[i].isalpha(): 
     str_list.insert(0, text[i]) 
     i -= 1 

    i = position + 1 
    while text[i].isalpha(): 
     str_list.append(text[i]) 
     i += 1 

    return ''.join(str_list) 

以下是測試案例:

get_word("Hello, my name is Earl.", 21) # 'Earl' 
get_word("Hello, my name is Earl.", 20) # 'Earl' 

我不認爲這是分裂的文本是個好主意換成split函數,因爲位置對於這個問題是必不可少的。如果文本中出現連續的空白,split功能可能會導致故障。