首页 > 解决方案 > 如何在python上找到它以前出现的索引?

问题描述

(我刚刚用多行输入编辑了这个问题)对于这个文本中的每个单词,找到它之前在文本中出现的索引。第一个单词的索引为0。如果第一次出现单词,则打印-1。

例如:输入是:(4行输入)

She sells sea shells on the sea shore;
The shells that she sells are sea shells I'm sure.
So if she sells sea shells on the sea shore,
I'm sure that the shells are sea shore shells.

输出是:

-1 -1 -1 -1 -1 -1 2 -1 -1 3 -1 -1 1 -1 6 9 -1 -1 -1 -1 11 12 14 15 4 5 22 -1 16 -1 10 25 23 13 26 -1 -1

她 [0] 在 [4] [5] 海 [6] 海岸 [7] 上出售 [1] 海 [2] 贝壳 [3] ...

对于第 6 位的海,之前出现在第 2 位。

我尝试使用 get 方法,但现在确定如何通过获取索引值get()

我的代码:(我需要从输入中获取这些字符串)

while True:
   ans = input().split()

   if len(ans)==0:
       break
   else:
       lst = [-1 if i == ans.index(ans[i]) else i-ans[:i][::-1].index(ans[i])-1 for i in range(len(ans))]
       print(*lst)

标签: python-3.xlistdictionary

解决方案


您可以使用列表推导来迭代单词:

s = 'She sells sea shells on the sea shore by shells sea'.split()
lst = [-1 if i == s.index(s[i]) else i-s[:i][::-1].index(s[i])-1 for i in range(len(s))]

print(lst)

输出

[-1, -1, -1, -1, -1, -1, 2, -1, -1, 3, 6]

- - 更新 - -

split()也适用于多行字符串,因此可以使用相同的代码:

s = '''
She sells sea shells on the sea shore;
The shells that she sells are sea shells I'm sure.
So if she sells sea shells on the sea shore,
I'm sure that the shells are sea shore shells.
'''

print(repr(s))  # confirm newline characters

s = s.split()

lst = [-1 if i == s.index(s[i]) else i-s[:i][::-1].index(s[i])-1 for i in range(len(s))]

print(lst)

输出

"\nShe sells sea shells on the sea shore;\nThe shells that she sells are sea shells I'm sure.\nSo if she sells sea shells on the sea shore,\nI'm sure that the shells are sea shore shells.\n"
[-1, -1, -1, -1, -1, -1, 2, -1, -1, 3, -1, -1, 1, -1, 6, 9, -1, -1, -1, -1, 11, 12, 14, 15, 4, 5, 22, -1, 16, -1, 10, 25, 23, 13, 26, -1, -1]

--- 更新 #2 ---

如果您希望用户单独输入每一行,您可以使用列表来收集全文。

txt = []

i = '-'
while i:
   i = input('Enter line or return to end: ')
   txt.append(i)
   
s = ' '.join(txt).split()

lst = [-1 if i == s.index(s[i]) else i-s[:i][::-1].index(s[i])-1 for i in range(len(s))]

print(lst)

输出

Enter line or return to end: She sells sea shells on the sea shore;
Enter line or return to end: The shells that she sells are sea shells I'm sure.
Enter line or return to end: So if she sells sea shells on the sea shore,
Enter line or return to end: I'm sure that the shells are sea shore shells.
Enter line or return to end:
[-1, -1, -1, -1, -1, -1, 2, -1, -1, 3, -1, -1, 1, -1, 6, 9, -1, -1, -1, -1, 11, 12, 14, 15, 4, 5, 22, -1, 16, -1, 10, 25, 23, 13, 26, -1, -1]

--- 更新 #3 ---

这使用 defaultdict 对象生成结果:

from collections import defaultdict

s = '''
She sells sea shells on the sea shore by shells sea
'''
words = s.split()

idx = [(w,i) for i,w in enumerate(words)]  # word, index

d = defaultdict(list) # each element is empty list

for k, v in idx:
    d[k].append(v)  # get word indexes

print(d)  # {'She': [0], 'sells': [1], 'sea': [2, 6, 10], 'shells': [3, 9], 'on': [4], 'the': [5], 'shore': [7], 'by': [8]})

lst = [-1] * len(words)  # default -1

for k,v in d.items():
   if len(v) > 0:  # word appears more than once
      for i in range(1,len(v)):  # iterate indexes
          lst[v[i]] = v[i-1]

print(lst)  # [-1, -1, -1, -1, -1, -1, 2, -1, -1, 3, 6]

推荐阅读