python - 如何找到列表中重复项的所有索引?

标签 python python-3.x list

<分区>

如何找到重复项目的所有索引?例如:

list = ['A', 'A', 'B', 'A', 'B', 'B', 'A']

我想返回所有出现的“B”,所以它会返回:

indexes = [2, 4, 5]

最佳答案

在这里使用枚举

>>> l = ['A','A','B','A','B','B','A']
>>> [i for i,d in enumerate(l) if d=='B']
[2, 4, 5]

关于python - 如何找到列表中重复项的所有索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46701978/

相关文章:

python - 检查 python 3 支持的要求

c# - 如何在不使用循环的情况下将项目从列表复制到堆栈

python - 致命的 Python 错误和 `BufferedWriter`

Python 3 and-or vs if-else

python - 如何将嵌套属性编码到架构?

python - 使用 Python 请求提取 href URL

python-3.x - 如何从提供的向量中找到最大的数字?

python - 如何仅显示 django 模板中列表中的不同值?

python - Matplotlib 无法通过 PyCharm 工作

Python PAM 非交互式身份验证