Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中找到重复单词的索引_Python_Indexing - Fatal编程技术网

如何在python中找到重复单词的索引

如何在python中找到重复单词的索引,python,indexing,Python,Indexing,我用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 la

我用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)
这个输入的输出是:a, 是:[0,2],并且是正确的, 但在这个信号中:a, 答案是:[0,2,1], 我想看看:[0,2,4], 我想要一个动态代码,因为我不知道什么时候从输入中得到重复的单词。 如果有任何解决方案,我想得到更多的重复字索引
谢谢

您可以这样做:

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”
,我得到结果:

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

你可以这样做:

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”
,我得到结果:

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
投入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]}
投入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]}

什么是精确输入?噢,我的算法复杂度!找出一种避免线性搜索的方法。输入必须是字符串吗?如果字符串是
split()
,则复制的索引不同。例如:
“a”.split()
->
['a',a',a']
->
[0,1,2]
。而且,重复的单词不同于重复的字母。你对“foo-bar aaaa-aaa-bar”有什么期待?确切的输入是什么?噢,我的算法复杂度!找出一种避免线性搜索的方法。输入必须是字符串吗?如果字符串是
split()
,则复制的索引不同。例如:
“a”.split()
->
['a',a',a']
->
[0,1,2]
。而且,重复的单词不同于重复的字母。你对“foo bar aaaa aaa bar”有什么期待??我刚刚做了,很抱歉耽搁了。为什么要投反对票呢?名字“word\u pos”没有定义,虽然很好,但缺少一个索引。请输入文本:a,a在文本中找到0,a在文本中找到2text@MeeladRavasani是否可以使用当前编辑重试?“这对我很有效。”米拉德拉瓦萨尼:没问题。请随意接受答案:)我刚刚接受了,很抱歉耽搁了。为什么要投反对票呢?名字“word\u pos”没有定义,虽然很好,但缺少一个索引。请输入文本:a,a在文本中找到0,a在文本中找到2text@MeeladRavasani是否可以使用当前编辑重试?“这对我很有效。”米拉德拉瓦萨尼:没问题。那就接受答案吧:)