Created
November 29, 2023 16:31
-
-
Save hdary85/97d5e16382021ecd6e5164441a8a2794 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Function to find interdependent product groups | |
def find_interdependent_groups(transactions): | |
product_groups = [] | |
remaining_products = set() | |
# Get unique products | |
for products in transactions: | |
remaining_products |= products | |
transactions_copy = transactions.copy() # Create a copy of the list | |
# Find interdependent groups | |
while remaining_products: | |
current_group = set() | |
product = remaining_products.pop() | |
current_group.add(product) | |
new_products = {product} | |
while new_products: | |
product = new_products.pop() | |
for idx, products in enumerate(transactions_copy): | |
if product in products: | |
current_group |= products | |
new_products |= products | |
remaining_products -= products | |
del transactions_copy[idx] | |
if current_group: | |
product_groups.append(sorted(list(current_group))) | |
return product_groups | |
# Finding interdependent product groups | |
interdependent_groups = find_interdependent_groups(customer_transactions) | |
# Display interdependent product groups | |
for i, group in enumerate(interdependent_groups, start=1): | |
print(f"LISTE DE PRODUIT INTERDEPENDANT {i} = {','.join(map(str, group))}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment