查找列表相同元素位置Python
时间: 2023-11-19 11:07:58 浏览: 73
python找出一个列表中相同元素的多个索引实例
5星 · 资源好评率100%
Sure, I can help you find the position of the same elements in a list using Python. Here's an example code snippet:
```
my_list = [1, 2, 3, 2, 4, 1, 5, 6, 5]
duplicates = []
for i in range(len(my_list)):
if my_list[i] in my_list[i+1:] and my_list[i] not in duplicates:
duplicates.append(my_list[i])
print("Duplicate element:", my_list[i])
print("Position(s):", [j for j in range(len(my_list)) if my_list[j] == my_list[i]])
```
This will output:
```
Duplicate element: 1
Position(s): [0, 5]
Duplicate element: 2
Position(s): [1, 3]
Duplicate element: 5
Position(s): [6, 8]
```
Hope this helps!
阅读全文