交叉銷售商品
這家小型雜貨店決定把口香糖與咖啡、麥片或麵包做交叉銷售。為了判斷三者中哪個最適合,店主進行了一個實驗。在一週內,她把口香糖放在收銀台旁邊,並記錄所有同時購買口香糖與咖啡、麥片或麵包的交易。當天的交易以「清單中的清單」形式提供,名稱為 transactions。每筆交易要不是 ['coffee','gum']、就是 ['cereal','gum'],或是 ['bread','gum']。
本練習屬於課程
Python 的 Market Basket Analysis
練習說明
- 計算同時包含 coffee 與 gum 的交易筆數。
- 計算同時包含 cereal 與 gum 的交易筆數。
- 計算同時包含 bread 與 gum 的交易筆數。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Count the number of transactions with coffee and gum
coffee = transactions.count(['____', 'gum'])
# Count the number of transactions with cereal and gum
cereal = transactions.____(['cereal', 'gum'])
# Count the number of transactions with bread and gum
bread = transactions.____(['____', '____'])
# Print the counts for each transaction.
print('coffee:', coffee)
print('cereal:', cereal)
print('bread:', bread)