【办公类-111-01】20250821Python计算商铺优惠红包如何使用最便宜(金珠为例)

发布时间:2026/7/19 10:25:07
【办公类-111-01】20250821Python计算商铺优惠红包如何使用最便宜(金珠为例) 背景需求用红包买小克重的金珠降低克价但是每天的红包金额不同如40-8, 50-4,5660-3,16如何能够选择不同的克数的商品总价达到规定数字并让克数尽量最多从而降低克价。我把需求给了deepseek写答案最后AI给我一个不错的代码操作步骤在Excel里面写上当天的金珠店铺的实施克数和价格代码展示 优惠券最优组合计算Python程序金珠红包优惠券-折扣小数点后两位 deepseek阿夏 20250819 import pandas as pd from itertools import combinations_with_replacement def find_optimal_combinations(file_path, sheet_name, coupon_threshold, coupon_discount): # 读取Excel文件 try: df pd.read_excel(file_path, sheet_namesheet_name) weights df.iloc[:, 0].tolist() # 第一列是克数 prices df.iloc[:, 1].tolist() # 第二列是金额 if len(weights) ! len(prices): raise ValueError(克数和金额列长度不一致) except Exception as e: print(f读取文件出错: {e}) return None # 生成所有可能的组合(最多考虑4件商品组合避免组合爆炸) all_combinations [] items list(zip(weights, prices)) # 考虑单件、两件、三件和四件组合 for r in range(1, 5): for combo in combinations_with_replacement(range(len(items)), r): # 计算组合的总重量和总价格 total_weight sum(items[i][0] for i in combo) total_price sum(items[i][1] for i in combo) # 存储组合的索引而不是具体值以便后续引用 all_combinations.append((combo, total_weight, total_price)) # 筛选符合条件的组合 valid_combinations [] for combo in all_combinations: indices, weight, price combo if price coupon_threshold: discounted_price price - coupon_discount unit_price discounted_price / weight # 计算均价(元/克) valid_combinations.append((indices, weight, price, discounted_price, unit_price)) # 如果没有刚好满足条件的组合找最接近的 if not valid_combinations: # 找略低于阈值的组合中总价最高的 below_threshold [c for c in all_combinations if c[2] coupon_threshold] if below_threshold: max_below max(below_threshold, keylambda x: x[2]) unit_price max_below[2] / max_below[1] # 计算均价 valid_combinations.append((max_below[0], max_below[1], max_below[2], max_below[2], unit_price)) # 找略高于阈值的组合中总价最低的 above_threshold [c for c in all_combinations if c[2] coupon_threshold] if above_threshold: min_above min(above_threshold, keylambda x: x[2]) discounted_price min_above[2] - coupon_discount unit_price discounted_price / min_above[1] # 计算均价 valid_combinations.append((min_above[0], min_above[1], min_above[2], discounted_price, unit_price)) # 按优惠后的价格排序然后按均价排序 valid_combinations.sort(keylambda x: (x[3], x[4])) return valid_combinations, items def main(): # 输入参数 file_path rC:\Users\jg2yXRZ\OneDrive\桌面\淘宝数据合并金珠\两款价目表.xlsx # 修改为实际文件路径 name[玛贝丝,水贝旺,岚金福K18] nint(input(0玛贝丝,1.水贝旺,2.岚金福K18\n)) if n0: sheet_name name[0] elif n1: sheet_name name[1] else: sheet_name name[2] # # sheet_name 水贝旺 # sheet_name 岚金福K18 print(请输入优惠券信息(例如: 80-5 或 80.2-7.5 表示满80减5或满80.2减7.5):) coupon_input input().strip() try: # 处理可能的浮点数输入 parts coupon_input.split(-) coupon_threshold float(parts[0]) coupon_discount float(parts[1]) except: print(输入格式不正确请使用如 80-5 或 80.2-7.5 的格式) return # 计算最优组合 results, items find_optimal_combinations(file_path, sheet_name, coupon_threshold, coupon_discount) if not results: print(没有找到合适的商品组合) return print(f\n优惠券: 满{coupon_threshold}减{coupon_discount}) print(推荐购买组合(按优惠后价格和均价排序):) for i, (indices, weight, original_price, discounted_price, unit_price) in enumerate(results[:2], 1): print(f\n组合 {i}:) # 统计每种商品的购买数量 item_counts {} for idx in indices: item items[idx] key (item[0], item[1]) item_counts[key] item_counts.get(key, 0) 1 for (w, p), count in item_counts.items(): print(f {w}g 商品: {count}件, 单价: {p}元) print(f 总克数: {weight}g) print(f 原总价: {original_price:.2f}元) print(f 优惠后价: {discounted_price:.2f}元) print(f 节省金额: {original_price - discounted_price:.2f}元) print(f 优惠后均价: {unit_price:.4f}元/克) if __name__ __main__: main()首先测试大额省钱包里面的80-5红包准备买两个店铺的金珠“吗贝斯”金珠结果显示了2个组合第1个组合是克数0.095g ,总价80.49均价794.63。第2个组合是克数0.095g ,总价80.50均价794.73。总之第一个组合总价最低少1分均价最低少1角但是今天金价大盘的回收价是770.45所以无论怎么买794都是溢价的所以不买。再测试一个“水贝旺”金珠结果显示前两种组合都是合计0.091g总价都是80.02大于80元一点点但是均价是824元不值得买如果显示5个还有一个0.091的组合方式足金999不行再测试一个18K金的红包。今天18K金回收价573.99元用最大的红包27-6计算直接买1个0.0512-5.2计算买0.01两次均价409。其他红包测算共支付84.29去掉每单2元邮费除以0.16g,等于526.81元我还有一个支付宝红包签到18-3.98最适合的买1个0.025g均价720g用这个Python代码先计算一下优惠红包可以筛选哪些红包可以用价格低于贵金属大盘价并获取最佳商品组合不过红包获得需要做很多任务即使我用了Python和UIBOT程序自动刷也需要大量时间等候执行程序。所以只能假期偶尔玩玩我只有1个账号也感觉它太伤精力了刷一天也就省1-2元。小红书上那些攒金珠做买卖差价的专业人员动不动就5个10个账户每天就买10-15克10万差价收入高但耗费de体力、脑力、财力也很厉害效仿不了。20260720,我的老电脑坏了换了一个电脑Python版本是3.8不兼容pandas安装不了修改路径制作EXCEL里面的价格和克数 优惠券最优组合计算Python程序金珠红包 移除pandas纯csv标准库版本读取csv文件替代Excel多sheet 20260718 import csv from itertools import combinations_with_replacement import os def read_target_csv(file_name, base_dir): 读取指定名称的csv返回克数列表、金额列表替代pd.read_excel file_path os.path.join(base_dir, f{file_name}.csv) weights [] prices [] try: with open(file_path, r, encodingutf-8, newline) as f: reader csv.reader(f) for row in reader: # 跳过空行 if not row or len(row) 2: continue # 转换为浮点数字 w float(row[0].strip()) p float(row[1].strip()) weights.append(w) prices.append(p) if len(weights) ! len(prices): raise ValueError(csv文件中克数列和金额列行数不一致) return weights, prices except FileNotFoundError: print(f错误未找到文件 {file_path}请确认csv文件已放在对应目录) return None, None except Exception as e: print(f读取csv文件出错: {e}) return None, None def find_optimal_combinations(base_dir, sheet_name, coupon_threshold, coupon_discount): # 读取csv数据 weights, prices read_target_csv(sheet_name, base_dir) if weights is None or prices is None: return None, None items list(zip(weights, prices)) all_combinations [] # 生成1~4件商品组合 for r in range(1, 5): for combo in combinations_with_replacement(range(len(items)), r): total_weight sum(items[i][0] for i in combo) total_price sum(items[i][1] for i in combo) all_combinations.append((combo, total_weight, total_price)) valid_combinations [] # 筛选满足满减条件的组合 for combo in all_combinations: indices, weight, price combo if price coupon_threshold: discounted_price price - coupon_discount unit_price discounted_price / weight valid_combinations.append((indices, weight, price, discounted_price, unit_price)) # 无满足满减时补充低于阈值最高价、高于阈值最低价组合 if not valid_combinations: below_threshold [c for c in all_combinations if c[2] coupon_threshold] if below_threshold: max_below max(below_threshold, keylambda x: x[2]) unit_price max_below[2] / max_below[1] valid_combinations.append((max_below[0], max_below[1], max_below[2], max_below[2], unit_price)) above_threshold [c for c in all_combinations if c[2] coupon_threshold] if above_threshold: min_above min(above_threshold, keylambda x: x[2]) discounted_price min_above[2] - coupon_discount unit_price discounted_price / min_above[1] valid_combinations.append((min_above[0], min_above[1], min_above[2], discounted_price, unit_price)) # 按优惠后总价升序再按均价升序 valid_combinations.sort(keylambda x: (x[3], x[4])) return valid_combinations, items def main(): # csv文件存放根目录修改为你的文件夹路径 base_folder rC:\Users\mhjg2\Desktop\淘宝数据合并金珠 sheet_name_list [玛贝丝, 水贝旺, 岚金福K18] print(请选择品牌0-玛贝丝, 1-水贝旺, 2-岚金福K18) try: n int(input(输入数字).strip()) if n not in [0, 1, 2]: print(输入超出范围只能输入0/1/2) return select_sheet sheet_name_list[n] except ValueError: print(输入格式错误请输入数字0、1或2) return print(\n请输入优惠券规则格式示例80-5 或 80.2-7.5代表满80减5) coupon_input input().strip() try: threshold_str, discount_str coupon_input.split(-) coupon_threshold float(threshold_str) coupon_discount float(discount_str) except Exception: print(输入格式错误必须使用「金额-优惠」格式如 200-20) return # 计算最优组合 results, items find_optimal_combinations(base_folder, select_sheet, coupon_threshold, coupon_discount) if not results or items is None: print(未能获取商品数据程序终止) return print(f\n 优惠券规则满{coupon_threshold:.2f} 减{coupon_discount:.2f} ) print(推荐最优2组组合优惠后总价均价升序) for group_idx, (indices, weight, original_price, discounted_price, unit_price) in enumerate(results[:2], 1): print(f\n【组合 {group_idx}】) item_count_map {} for idx in indices: w_val, p_val items[idx] key (w_val, p_val) item_count_map[key] item_count_map.get(key, 0) 1 for (w, p), count in item_count_map.items(): print(f {w}g 单品 ×{count}件 | 单品价{p:.2f}元) save_money original_price - discounted_price print(f 合计总克重{weight:.2f}g) print(f 未优惠总价{original_price:.2f}元) print(f 优惠后实付{discounted_price:.2f}元) print(f 直接节省{save_money:.2f}元) print(f 优惠后单克均价{unit_price:.4f} 元/克) if __name__ __main__: main()始终读取不了xlsx运行不了。必须升级Python版本使之能够安装pandas或openpyxl。最后豆包把xslx里面的参数做成列表 金珠优惠券最优组合计算 - 零依赖版 无需安装任何库、无需读Excel文件内置全部商品数据直接运行 from itertools import combinations_with_replacement # 内置你Excel里玛贝丝全部商品数据不用改 # 格式(克数, 单价)和你Excel里完全一致 items [ (0.007, 6.7), (0.01, 9.58), (0.013, 12.45), (0.015, 14.37), (0.017, 16.28), (0.02, 19.16), (0.025, 23.95), (0.03, 28.71), (0.035, 33.49), (0.04, 38.28), (0.05, 47.8), (0.06, 57.36), (0.07, 66.92), (0.08, 76.33), (0.09, 85.87), (0.1, 94.84), (0.15, 142.26), (0.2, 189.68), (0.3, 284.52), (0.4, 379.36), (0.5, 474.21) ] # def find_optimal_combinations(coupon_threshold, coupon_discount): all_combinations [] # 生成1~4件商品所有可重复组合 for r in range(1, 5): for combo in combinations_with_replacement(range(len(items)), r): total_weight sum(items[i][0] for i in combo) total_price sum(items[i][1] for i in combo) all_combinations.append((combo, total_weight, total_price)) valid_combinations [] # 筛选满足满减门槛的组合 for combo in all_combinations: indices, weight, price combo if price coupon_threshold: discounted_price price - coupon_discount unit_price discounted_price / weight valid_combinations.append((indices, weight, price, discounted_price, unit_price)) # 没有刚好满足门槛的组合时自动补充临界最优组合 if not valid_combinations: below_threshold [c for c in all_combinations if c[2] coupon_threshold] if below_threshold: max_below max(below_threshold, keylambda x: x[2]) unit_price max_below[2] / max_below[1] valid_combinations.append((max_below[0], max_below[1], max_below[2], max_below[2], unit_price)) above_threshold [c for c in all_combinations if c[2] coupon_threshold] if above_threshold: min_above min(above_threshold, keylambda x: x[2]) discounted_price min_above[2] - coupon_discount unit_price discounted_price / min_above[1] valid_combinations.append((min_above[0], min_above[1], min_above[2], discounted_price, unit_price)) # 排序先按优惠后总价升序再按单克均价升序 valid_combinations.sort(keylambda x: (x[3], x[4])) return valid_combinations def main(): print( 玛贝丝金珠 优惠券最优组合计算 ) print(请输入优惠券规则格式示例80-5 代表满80减5支持小数如80.2-7.5) coupon_input input().strip() try: threshold_str, discount_str coupon_input.split(-) coupon_threshold float(threshold_str) coupon_discount float(discount_str) except Exception: print(输入格式错误必须使用「门槛-优惠」格式比如 200-20) return results find_optimal_combinations(coupon_threshold, coupon_discount) if not results: print(没有找到合适的组合) return print(f\n 优惠券规则满{coupon_threshold:.2f} 减{coupon_discount:.2f} ) print(推荐最优TOP3组合优惠后总价单克均价升序) for group_idx, (indices, weight, original_price, discounted_price, unit_price) in enumerate(results[:3], 1): print(f\n【组合 {group_idx}】) item_count_map {} for idx in indices: w_val, p_val items[idx] key (w_val, p_val) item_count_map[key] item_count_map.get(key, 0) 1 for (w, p), count in item_count_map.items(): print(f {w}g 金珠 ×{count}件 | 单件价{p:.2f}元) save_money original_price - discounted_price print(f 总克重{weight:.3f}g) print(f 原价合计{original_price:.2f}元) print(f 券后实付{discounted_price:.2f}元) print(f 立省金额{save_money:.2f}元) print(f 券后单克价{unit_price:.4f} 元/克) if __name__ __main__: main()三个方案。前两个单价一样克数一样如果单买一个整数克数就要0.035G才能用到红包。均价899.71Python的combinations枚举计算后用凑多单方式正好用到红宝额度减少克数降低均价成本1颗0.035G均价899.71元2颗0.007G1颗0.0170.031G 均价796.129元。再测试一个5999-150的红包只有一个组合但是均价949.42超过黄金盘876不值得买还有几个红包也测试今天还有一个5999-150的红包我看到一个书签的商品要看优惠前的价格 金珠优惠券最优组合计算 - 零依赖版 无需安装任何库、无需读Excel文件内置全部商品数据直接运行 from itertools import combinations_with_replacement # 内置你Excel里玛贝丝全部商品数据不用改 # 格式(克数, 单价)和你Excel里完全一致 items [ (0.56, 507.8), (1, 905.5), (1.5, 1358.25), ] # def find_optimal_combinations(coupon_threshold, coupon_discount): all_combinations [] # 生成1~4件商品所有可重复组合 for r in range(1, 20): for combo in combinations_with_replacement(range(len(items)), r): total_weight sum(items[i][0] for i in combo) total_price sum(items[i][1] for i in combo) all_combinations.append((combo, total_weight, total_price)) valid_combinations [] # 筛选满足满减门槛的组合 for combo in all_combinations: indices, weight, price combo if price coupon_threshold: discounted_price price - coupon_discount unit_price discounted_price / weight valid_combinations.append((indices, weight, price, discounted_price, unit_price)) # 没有刚好满足门槛的组合时自动补充临界最优组合 if not valid_combinations: below_threshold [c for c in all_combinations if c[2] coupon_threshold] if below_threshold: max_below max(below_threshold, keylambda x: x[2]) unit_price max_below[2] / max_below[1] valid_combinations.append((max_below[0], max_below[1], max_below[2], max_below[2], unit_price)) above_threshold [c for c in all_combinations if c[2] coupon_threshold] if above_threshold: min_above min(above_threshold, keylambda x: x[2]) discounted_price min_above[2] - coupon_discount unit_price discounted_price / min_above[1] valid_combinations.append((min_above[0], min_above[1], min_above[2], discounted_price, unit_price)) # 排序先按优惠后总价升序再按单克均价升序 valid_combinations.sort(keylambda x: (x[3], x[4])) return valid_combinations def main(): print( 玛贝丝金珠 优惠券最优组合计算 ) print(请输入优惠券规则格式示例80-5 代表满80减5支持小数如80.2-7.5) coupon_input input().strip() try: threshold_str, discount_str coupon_input.split(-) coupon_threshold float(threshold_str) coupon_discount float(discount_str) except Exception: print(输入格式错误必须使用「门槛-优惠」格式比如 200-20) return results find_optimal_combinations(coupon_threshold, coupon_discount) if not results: print(没有找到合适的组合) return print(f\n 优惠券规则满{coupon_threshold:.2f} 减{coupon_discount:.2f} ) print(推荐最优TOP3组合优惠后总价单克均价升序) for group_idx, (indices, weight, original_price, discounted_price, unit_price) in enumerate(results[:3], 1): print(f\n【组合 {group_idx}】) item_count_map {} for idx in indices: w_val, p_val items[idx] key (w_val, p_val) item_count_map[key] item_count_map.get(key, 0) 1 for (w, p), count in item_count_map.items(): print(f {w}g 金珠 ×{count}件 | 单件价{p:.2f}元) save_money original_price - discounted_price print(f 总克重{weight:.3f}g) print(f 原价合计{original_price:.2f}元) print(f 券后实付{discounted_price:.2f}元) print(f 立省金额{save_money:.2f}元) print(f 券后单克价{unit_price:.4f} 元/克) if __name__ __main__: main()