python - 如何在Python中找到恰好出现2的列表元素的索引
问题描述
我正在尝试编写一个代码,它返回列表中元素的所有索引,这些索引重复两次。我自己的算法有问题。我的代码只返回它找到的第一个事件。我想解决这个问题。这是我自己的代码(我知道这有点奇怪):
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)
我感谢任何人的帮助。提前致谢。
解决方案
试试这个:
from collections import Counter
userInput = input().split()
counter = Counter(userInput)
print([x[0] for x in counter.items() if x[1] == 2])
推荐阅读
- node.js - 使用参数时Postgres“列$ 3不存在”
- angular - 如何覆盖 nb-theme 字体系列?
- javascript - 如何测试使用 Partials 编写的服务
- node.js - Heroku 构建在 package.json 文件中找不到`@types`
- python - sqlite3.ProgrammingError:提供的绑定数量不正确。当前语句使用 2,并且提供了 1
- testing - 赛普拉斯,cy.visit() 尝试加载 ESOCKETTIMEDOUT 失败
- redux - 默认情况下 CreateEntityAdapter 和 normalizr 兼容吗?- SelectId 始终未定义
- c# - 如何在 JetBrains Rider 中关闭代码“提示”
- wordpress - 如何在 wordpress 中创建分层帖子?
- python - 通过抬腿在中点连接一系列桥梁