43

Вопрос по list, algorithm, python, string – Python: найти ближайшую строку (из списка) к другой строке

Допустим, у меня естьstring "Hello" и список words = ['hello', 'Hallo', 'hi', 'house', 'key', 'screen', 'hallo','question', 'Hallo', 'format']Как я могу найтиn words которые ближе всего к"Hello" и присутствует в спискеwords ? В этом случае мы ...

Error: User Rate Limit Exceeded

от tripleee

Error: User Rate Limit Exceeded<i>closest</i>Error: User Rate Limit Exceeded

от Gareth Latty

Error: User Rate Limit Exceeded

от Laura

Error: User Rate Limit Exceeded

от Nick Johnson

Error: User Rate Limit Exceeded

от Peter Wood

4 ответа

1

Error: User Rate Limit ExceededError: User Rate Limit ExceededError: User Rate Limit Exceeded

0

Error: User Rate Limit ExceededError: User Rate Limit ExceededError: User Rate Limit Exceeded

Error: User Rate Limit ExceededHeapError: User Rate Limit ExceedednError: User Rate Limit ExceededHeapError: User Rate Limit ExceededcloseError: User Rate Limit Exceeded

Error: User Rate Limit ExceedednError: User Rate Limit Exceeded

Heap = []
for word in words:
    if len(Heap)<n:
       Heap.insert(word)
    else
       if close(word,Heap[0]): # it means Heap[0] is the nth farthest word until now
             Heap.pop():
             Heap.insert(word)
68

Error: User Rate Limit Exceededdifflib.get_close_matches.

>>> words = ['hello', 'Hallo', 'hi', 'house', 'key', 'screen', 'hallo', 'question', 'format']
>>> difflib.get_close_matches('Hello', words)
['hello', 'Hallo', 'hallo']

Error: User Rate Limit Exceeded

21

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

hello - helo   - deletes    
hello - helol  - transpose    
hello - hallo  - replaces    
hello - heallo - inserts    


def edits1(word):
   splits     = [(word[:i], word[i:]) for i in range(len(word) + 1)]
   deletes    = [a + b[1:] for a, b in splits if b]
   transposes = [a + b[1] + b[0] + b[2:] for a, b in splits if len(b)>1]
   replaces   = [a + c + b[1:] for a, b in splits for c in alphabet if b]
   inserts    = [a + c + b     for a, b in splits for c in alphabet]
   return set(deletes + transposes + replaces + inserts)

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Похожие вопросы