python中统计同一个数字连续出现的位置
时间: 2024-04-07 14:33:51 浏览: 10
你可以使用Python中的循环和条件判断来统计同一个数字连续出现的位置。以下是一个示例代码:
```python
def find_consecutive_positions(lst):
positions = []
start = 0
length = 1
for i in range(1, len(lst)):
if lst[i] == lst[i-1]:
length += 1
else:
if length > 1:
positions.append((lst[i-1], start, start+length-1))
start = i
length = 1
if length > 1:
positions.append((lst[-1], start, start+length-1))
return positions
# 示例用法
numbers = [1, 2, 2, 3, 4, 4, 4, 5, 6, 6, 7, 7, 7, 7]
positions = find_consecutive_positions(numbers)
for number, start, end in positions:
print(f"The number {number} appears consecutively from index {start} to {end}")
```
这段代码会输出:
```
The number 2 appears consecutively from index 1 to 2
The number 4 appears consecutively from index 4 to 6
The number 6 appears consecutively from index 8 to 9
The number 7 appears consecutively from index 10 to 13
```
这样你就可以得到同一个数字连续出现的位置信息。