Bạn có thể sử dụng thư viện collections trong Python để đếm số lượng các category theo URL trong mảng product.
Dưới đây là một ví dụ cách thực hiện điều đó:
Giả sử bạn có một danh sách products, mỗi sản phẩm là một dictionary chứa thông tin về sản phẩm, bao gồm category và URL.
from collections import defaultdict # Giả sử đây là mảng product của bạn products = [ {'name': 'Product 1', 'category': 'Category A', 'url': 'https://example.com/1'}, {'name': 'Product 2', 'category': 'Category B', 'url': 'https://example.com/2'}, {'name': 'Product 3', 'category': 'Category A', 'url': 'https://example.com/1'}, {'name': 'Product 4', 'category': 'Category C', 'url': 'https://example.com/3'}, {'name': 'Product 5', 'category': 'Category B', 'url': 'https://example.com/2'}, # Thêm nhiều sản phẩm khác ] # Tạo một dictionary để lưu số lượng category theo URL category_count_by_url = defaultdict(lambda: defaultdict(int)) # Đếm số lượng category theo URL for product in products: url = product['url'] category = product['category'] category_count_by_url[url][category] += 1 # In kết quả for url, categories in category_count_by_url.items(): print(f'URL: {url}') for category, count in categories.items(): print(f' Category: {category}, Count: {count}')
Kết quả sẽ giống như sau:
URL: https://example.com/1 Category: Category A, Count: 2 URL: https://example.com/2 Category: Category B, Count: 2 URL: https://example.com/3 Category: Category C, Count: 1
Đoạn mã này sẽ đếm số lượng mỗi category theo URL và in ra kết quả.
Bạn có thể điều chỉnh hoặc mở rộng mã này tùy theo yêu cầu cụ thể của bạn.
Để đưa kết quả đếm vào một mảng groups, bạn có thể cấu trúc lại đoạn mã trên sao cho kết quả được lưu vào mảng thay vì in ra.
Dưới đây là một ví dụ cách thực hiện điều đó:
from collections import defaultdict # Giả sử đây là mảng product của bạn products = [ {'name': 'Product 1', 'category': 'Category A', 'url': 'https://example.com/1'}, {'name': 'Product 2', 'category': 'Category B', 'url': 'https://example.com/2'}, {'name': 'Product 3', 'category': 'Category A', 'url': 'https://example.com/1'}, {'name': 'Product 4', 'category': 'Category C', 'url': 'https://example.com/3'}, {'name': 'Product 5', 'category': 'Category B', 'url': 'https://example.com/2'}, # Thêm nhiều sản phẩm khác ] # Tạo một dictionary để lưu số lượng category theo URL category_count_by_url = defaultdict(lambda: defaultdict(int)) # Đếm số lượng category theo URL for product in products: url = product['url'] category = product['category'] category_count_by_url[url][category] += 1 # Đưa kết quả vào mảng groups groups = [] for url, categories in category_count_by_url.items(): for category, count in categories.items(): groups.append({'url': url, 'category': category, 'count': count}) # In kết quả for group in groups: print(group)
Kết quả thực tế sau khi áp dụng bạn có thể dùng để tạo ra các phiên:
Kết quả sẽ được lưu vào mảng groups và in ra như sau:
{'url': 'https://example.com/1', 'category': 'Category A', 'count': 2} {'url': 'https://example.com/2', 'category': 'Category B', 'count': 2} {'url': 'https://example.com/3', 'category': 'Category C', 'count': 1}
Trong đoạn mã này, mảng groups sẽ chứa các dictionary, mỗi dictionary bao gồm URL, category và số lượng sản phẩm thuộc category đó tại URL đó.
#Mtips5s #Contact
Fanpage: https://www.facebook.com/mtipscoder
Group trao đổi, chia sẻ: https://www.facebook.com/groups/mtipscoder
Website: https://mtips5s.com
Youtube: https://mtips5s.com
Twitter(X): @takagiks99
Instagram: @khuongkara
Threads: @khuongkara
Google Maps: @khuongkara
#Base Code #Souce Code
Bộ công cụ My Self: @tools.mtips5s.com
Github: @github
Npm: @npm
Docker: @docker
Chúc các bạn thành công!
Leave A Comment