我试图编写一个代码,返回列表中元素的所有索引,这些索引重复两次。我自己的算法有问题。我的代码只返回它找到的第一个匹配项。我想把这个修好。这是我自己的代码(我知道这有点奇怪):
from collections import Counter
length = int(input())
user_input = [int(x) for x in input().split()]
occurrances = Counter(user_input)
check_list = []
check_list.append(list(occurrances.keys())[list(occurrances.values()).index(2)])
print(check_list)
我感谢任何人的帮助。提前谢谢
试试这个:
查找两次出现的项目的索引
要获取索引,可以在列表中使用计数器和枚举:
如果不允许使用库,则可以不使用计数器(尽管运行速度较慢):
相关问题 更多 >
编程相关推荐