2017-09-14 57 views
0

我寫在python如何找到重複字的索引在python

import re 

text = input('please enter text: ') 
word = re.findall('\w+', text) 
len_word = len(word) 
word_pos = [] 

for i in range(len_word): 
    if text.index(word[i]) in word_pos: 
     prev_index = text.index(word[i]) + 1 
     last_index = 0 
     # print('index1: ' , last_index) 
     text = text[prev_index:] 
     # print('new_text: ' , new_text) 
     word_pos.append(text.index(word[i]) + prev_index + last_index) 
     last_index += prev_index 
    else: 
     word_pos.append(text.index(word[i])) 

print(word_pos) 

該代碼和該輸入的輸出:AA, 是:[0,2],是正確的, 但在這個意思是:aaa, 答案是:[0,2,1], 我想看看:[0,2,4], ,我想要一個動態代碼,因爲我不知道我什麼時候從duplacated word輸入。 如果有任何解決方案,我想獲得更多的重複的字索引 感謝

+0

什麼是確切輸入? –

+0

哎,算法複雜!找出一種避免線性搜索的方法。 – o11c

+0

是否需要輸入是字符串?如果字符串被分割成單詞列表,則複製索引是不同的。例如:'a a a「.split()' - >'['a','a','a']' - >'[0,1,2]'。而且,重複的單詞與重複的單詞不同。你對''foo bar aaaa aaa bar''期待什麼? – pylang

回答

2

你可以做這樣的事情:

import re 

text = input('please enter text: ') 
words = re.findall('\w+', text) 
word_pos = [] 
pos = 0 # this will help us track the word's position in the original text 

for i in range(len(words)): 
    word = words[i] 
    pos += text[pos:].index(word) # we use the position of the last word to find the position of the current word 
    if word in words[i+1:] or word in words[:i]: # we have a duplicate so we can append this position 
     word_pos.append(pos) 
     print('{} found at {} in text'.format(word,pos)) 
    pos += 1 

隨着輸入:"a a a a",我得到的結果是:

please enter text: a a a a a 
a found at 0 in text 
a found at 2 in text 
a found at 4 in text 
a found at 6 in text 
a found at 8 in text 
+0

我只是做了,對於延遲抱歉。爲什麼downvote雖然? – Flynsee

+0

name'word_pos'沒有定義 – Arun

+0

@Arun我的不好,它在OP的代碼中。剛添加它。 – Flynsee

0
import re 
text = input('please enter text: ') 
print({word: [w.start() for w in re.finditer(word, text)] for word in text.split()}) 

input 1:

please enter text: a a a a 

output: 
{'a': [0, 2, 4, 6]} 

input 2: 
please enter text: Hello Arun Hello man 

output: 
{'Arun': [6], 'Hello': [0, 11], 'man': [17]} 
相關問題