পদ্ধতি ১: max() + count() + ফ্ল্যাটেন
def highest_freq(lst):
flat = [el for sub in lst for el in sub]
return max(flat, key=flat.count)
listA = [[45,20,11], [20,17,45], [20,13,9]]
print(highest_freq(listA)) # 20
পদ্ধতি ২: itertools.chain + max() + count()
from itertools import chain
def highest_freq(lst):
flat = list(chain.from_iterable(lst))
return max(flat, key=flat.count)
listA = [[45,20,11], [20,17,45], [20,13,9]]
print(highest_freq(listA)) # 20
পদ্ধতি ৩: Counter + chain (সর্বোত্তম)
from itertools import chain
from collections import Counter
def highest_freq(lst):
return Counter(chain.from_iterable(lst)).most_common(1)[0][0]
listA = [[45,20,11], [20,17,45], [20,13,9]]
print(highest_freq(listA)) # 20
ব্যাখ্যা
chain.from_iterable: নেস্টেড লিস্ট ফ্ল্যাটেন করার দ্রুত পদ্ধতি।
Counter: O(n) ফ্রিকোয়েন্সি কাউন্ট, most_common দিয়ে সরাসরি ফলাফল।
আউটপুট
20