python-3.x - 如何在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)
解决方案
您可以使用列表推导来迭代单词:
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]
推荐阅读
- javascript - 根据 ${selectedcount} 显示不同的输出
- android - 如何阻止机器人使用谷歌登录?
- magento - 如何将 Magento 网站从一台服务器转移到另一台服务器
- c++ - 运算符重载使用运算符 + 为类模板添加
- exception - 异常向量是中断向量的更好名称吗?(在 ARM 的上下文中)
- swift - SwiftUI NavigationLink - “懒惰”的目的地?或者如何不编码重复的“标签”?
- c - 我从一位同事那里看到了一些有趣的代码。我很困惑发生了什么。它是哪个标准?我没能在 GOOGLE 中找到答案
- c++ - “警告:数组索引表达式中的逗号”但逗号分隔函数参数
- python-3.x - Opencv findCountours 函数
- swift - 如何在 SwiftUI 中使用 ForEach 仅更改一个核心数据项的切换?