Python में सूची में लगातार (consecutive) आने वाले समान तत्वों की गिनती करने के लिए लूप और set() का उपयोग किया जा सकता है। यह लगातार डुप्लिकेट के अद्वितीय मान गिनता है।
समस्या
सूची [24, 24, 24, 15, 15, 64, 64, 71, 13, 95, 100] में लगातार समान तत्व हैं: 24 (3 बार), 15 (2 बार), 64 (2 बार)। अद्वितीय मान जो लगातार दोहराते हैं: 3 (24, 15, 64)।
उदाहरण
my_list = [24, 24, 24, 15, 15, 64, 64, 71, 13, 95, 100]
print("मूल सूची:", my_list)
# विधि 1: लूप + set (मूल लेख)
my_result = []
for i in range(len(my_list) - 1):
if my_list[i] == my_list[i + 1]:
my_result.append(my_list[i])
# अद्वितीय मानों की गिनती
unique_consecutive = len(set(my_result))
print(f"लगातार समान तत्वों के अद्वितीय मान: {unique_consecutive}") # 3
print(f"कौन से मान: {set(my_result)}") # {24, 15, 64}
# विधि 2: itertools.groupby (अधिक कुशल)
from itertools import groupby
consecutive_vals = [key for key, group in groupby(my_list) if sum(1 for _ in group) > 1]
print(f"groupby से: {len(set(consecutive_vals))} - {set(consecutive_vals)}")
आउटपुट
मूल सूची: [24, 24, 24, 15, 15, 64, 64, 71, 13, 95, 100]
लगातार समान तत्वों के अद्वितीय मान: 3
कौन से मान: {24, 15, 64}
groupby से: 3 - {24, 15, 64}
विस्तृत विश्लेषण
from itertools import groupby
my_list = [24, 24, 24, 15, 15, 64, 64, 71, 13, 95, 100]
print("विस्तृत समूह विश्लेषण:")
for key, group in groupby(my_list):
group_list = list(group)
count = len(group_list)
if count > 1:
print(f" मान {key}: {count} बार लगातार - {group_list}")
else:
print(f" मान {key}: {count} बार (लगातार नहीं)")
आउटपुट
विस्तृत समूह विश्लेषण:
मान 24: 3 बार लगातार - [24, 24, 24]
मान 15: 2 बार लगातार - [15, 15]
मान 64: 2 बार लगातार - [64, 64]
मान 71: 1 बार (लगातार नहीं)
मान 13: 1 बार (लगातार नहीं)
मान 95: 1 बार (लगातार नहीं)
मान 100: 1 बार (लगातार नहीं)
फंक्शन के रूप में
def count_consecutive_duplicates(lst):
"""लगातार डुप्लिकेट वाले अद्वितीय मानों की गिनती"""
from itertools import groupby
return len({key for key, group in groupby(lst) if sum(1 for _ in group) > 1})
def get_consecutive_groups(lst):
"""लगातार समूहों का विस्तृत विवरण"""
from itertools import groupby
return [
{'value': key, 'count': len(list(group)), 'consecutive': len(list(group)) > 1}
for key, group in groupby(lst)
]
# उपयोग
print("गिनती:", count_consecutive_duplicates(my_list))
print("विवरण:", get_consecutive_groups(my_list))
आउटपुट
गिनती: 3
विवरण: [
{'value': 24, 'count': 3, 'consecutive': True},
{'value': 15, 'count': 2, 'consecutive': True},
{'value': 64, 'count': 2, 'consecutive': True},
{'value': 71, 'count': 1, 'consecutive': False},
{'value': 13, 'count': 1, 'consecutive': False},
{'value': 95, 'count': 1, 'consecutive': False},
{'value': 100, 'count': 1, 'consecutive': False}
]
विविधताएं
1. अधिकतम लगातार लंबाई खोजना
def max_consecutive_run(lst):
from itertools import groupby
return max((len(list(g)) for _, g in groupby(lst)), default=0)
print(f"अधिकतम लगातार लंबाई: {max_consecutive_run(my_list)}") # 3
2. विशिष्ट मान के लिए लगातार गिनती
def count_consecutive_of_value(lst, target):
max_run = current = 0
for val in lst:
if val == target:
current += 1
max_run = max(max_run, current)
else:
current = 0
return max_run
print(f"24 की अधिकतम लगातार गिनती: {count_consecutive_of_value(my_list, 24)}") # 3
print(f"15 की अधिकतम लगातार गिनती: {count_consecutive_of_value(my_list, 15)}") # 2
3. Pandas Series में
import pandas as pd
s = pd.Series([24, 24, 24, 15, 15, 64, 64, 71, 13, 95, 100])
# लगातार समान मानों के समूह
groups = (s != s.shift()).cumsum()
consecutive_counts = s.groupby(groups).size()
print("लगातार समूह आकार:\n", consecutive_counts)
print("अद्वितीय लगातार मान:", consecutive_counts[consecutive_counts > 1].index.tolist())