Telegram Group & Telegram Channel
🐍 Задача на Python — Топ‑3 самых популярных товаров

Допустим, у нас есть список покупок пользователей:


orders = [
{"user": "alice", "items": ["apple", "banana", "apple"]},
{"user": "bob", "items": ["banana", "orange"]},
{"user": "carol", "items": ["banana", "apple", "orange", "banana"]},
{"user": "dave", "items": ["apple"]},
]


🎯 Задание:
Найти 3 самых популярных товара по количеству покупок (не по числу пользователей, а по общему количеству упоминаний).

Ожидаемый результат:

[('banana', 4), ('apple', 4), ('orange', 2)]

💡 Решение:




from collections import Counter

# Собираем все товары в один список
all_items = []
for order in orders:
all_items.extend(order["items"])

# Считаем количество каждого товара
item_counts = Counter(all_items)

# Получаем топ-3 самых популярных
top_3 = item_counts.most_common(3)

print(top_3)


📌 Что тренирует задача:

• Работа со словарями и списками
• Использование Counter из модуля collections
• Умение работать с вложенными структурами
• Сортировка по частоте с помощью most_common()

#python #задача #кодинг #алгоритмы #учимпитон #collections



tg-me.com/pro_python_code/1841
Create:
Last Update:

🐍 Задача на Python — Топ‑3 самых популярных товаров

Допустим, у нас есть список покупок пользователей:


orders = [
{"user": "alice", "items": ["apple", "banana", "apple"]},
{"user": "bob", "items": ["banana", "orange"]},
{"user": "carol", "items": ["banana", "apple", "orange", "banana"]},
{"user": "dave", "items": ["apple"]},
]


🎯 Задание:
Найти 3 самых популярных товара по количеству покупок (не по числу пользователей, а по общему количеству упоминаний).

Ожидаемый результат:

[('banana', 4), ('apple', 4), ('orange', 2)]

💡 Решение:




from collections import Counter

# Собираем все товары в один список
all_items = []
for order in orders:
all_items.extend(order["items"])

# Считаем количество каждого товара
item_counts = Counter(all_items)

# Получаем топ-3 самых популярных
top_3 = item_counts.most_common(3)

print(top_3)


📌 Что тренирует задача:

• Работа со словарями и списками
• Использование Counter из модуля collections
• Умение работать с вложенными структурами
• Сортировка по частоте с помощью most_common()

#python #задача #кодинг #алгоритмы #учимпитон #collections

BY Python RU


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/pro_python_code/1841

View MORE
Open in Telegram


Python RU Telegram | DID YOU KNOW?

Date: |

Can I mute a Telegram group?

In recent times, Telegram has gained a lot of popularity because of the controversy over WhatsApp’s new privacy policy. In January 2021, Telegram was the most downloaded app worldwide and crossed 500 million monthly active users. And with so many active users on the app, people might get messages in bulk from a group or a channel that can be a little irritating. So to get rid of the same, you can mute groups, chats, and channels on Telegram just like WhatsApp. You can mute notifications for one hour, eight hours, or two days, or you can disable notifications forever.

To pay the bills, Mr. Durov is issuing investors $1 billion to $1.5 billion of company debt, with the promise of discounted equity if the company eventually goes public, the people briefed on the plans said. He has also announced plans to start selling ads in public Telegram channels as soon as later this year, as well as offering other premium services for businesses and users.

Python RU from sg


Telegram Python RU
FROM USA