Вот мой текущий алгоритм, но он неэффективен при обработке большого количества списков:
Код: Выделить всё
from collections import defaultdict
combinations_of_n = [ ('a','b','c'), ('e','f','g'), ('h','i','j') ] # 3.8 million combinations
df_list = [ ['b','v','e','a','b','c'], ['g','g','a','b','c','f','b'], ['i','k','l','a','i','k'] ] # 170000 lists
# Count occurrences of each combination in lst_of_lsts
combination_count = defaultdict(int)
for sublist in df_list:
for comb in combinations_of_n:
if all(elem in sublist for elem in comb):
combination_count[comb] += 1
# Find the top 5 most frequent combinations
top_combinations = sorted(combination_count.items(), key=lambda x: x[1], reverse=True)[:5]
# Print the results
print("Top 5 most frequent combinations:")
for comb, count in top_combinations:
print(f"{comb}: {count} occurrences")
Подробнее здесь: https://stackoverflow.com/questions/786 ... tions-that