gpt4 book ai didi

python代码在一个文件上工作但在其他文件上失败

转载 作者:太空宇宙 更新时间:2023-11-04 01:18:49 24 4
gpt4 key购买 nike

大家好,我有这段代码,它会打印出商品的最低成本和餐厅 ID。客户不想光顾多家餐厅。因此,例如,如果他要求“A,B”,那么代码应该打印同时提供它们的商店,而不是将用户需求分散在不同的餐厅(即使某些餐厅提供的价格便宜)。

此外,如果假设用户要汉堡。那么如果某个餐厅“X”以 4 美元的价格提供“汉堡”,而另一家餐厅“Y”以 3 美元的价格提供“汉堡+金枪鱼+ bean 腐”,那么我们将告诉用户去 RESTAURANT 'Y' 餐厅,即使它除了用户要求的 '汉堡' 之外还有额外的元素,但我们很乐意为他们提供额外的元素,只要它便宜。

一切都很好,但是代码在两个格式相同的输入文件(在 input.csv 上失败但在 input-2.csv 上运行)上表现奇怪,它给出正确的输出而另一个失败。这是我需要你帮助修复的唯一一个微小错误。请帮助我,我想我已经碰壁了,无法超越这一切。

def build_shops(shop_text):
shops = {}
for item_info in shop_text:
shop_id,cost,items = item_info.replace('\n', '').split(',')
cost = float(cost)
items = items.split('+')

if shop_id not in shops:
shops[shop_id] = {}
shop_dict = shops[shop_id]

for item in items:
if item not in shop_dict:
shop_dict[item] = []
shop_dict[item].append([cost,items])
return shops


def solve_one_shop(shop, items):
if len(items) == 0:
return [0.0, []]
all_possible = []
first_item = items[0]
if first_item in shop:
print "SHOP",shop.get(first_item)
for (price,combo) in shop[first_item]:
#print "items,combo=",items,combo
sub_set = [x for x in items if x not in combo]
#print "sub_set=",sub_set
price_sub_set,solution = solve_one_shop(shop, sub_set)
solution.append([price,combo])
all_possible.append([price+price_sub_set, solution])

cheapest = min(all_possible, key=(lambda x: x[0]))
return cheapest


def solver(input_data, required_items):
shops = build_shops(input_data)
#print shops
result_all_shops = []
for shop_id,shop_info in shops.iteritems():
(price, solution) = solve_one_shop(shop_info, required_items)
result_all_shops.append([shop_id, price, solution])

shop_id,total_price,solution = min(result_all_shops, key=(lambda x: x[1]))
print('SHOP_ID=%s' % shop_id)
sln_str = [','.join(items)+'(%0.2f)'%price for (price,items) in solution]
sln_str = '+'.join(sln_str)
print(sln_str + ' = %0.2f' % total_price)



shop_text = open('input-1.csv','rb')
solver(shop_text,['burger'])

=====input-1.csv=====restaurant_id, price, item

1,2.00,burger
1,1.25,tofulog
1,2.00,tofulog
1,1.00,chef_salad
1,1.00,A+B
1,1.50,A+CCC
1,2.50,A
2,3.00,A
2,1.00,B
2,1.20,CCC
2,1.25,D

=====输出和错误====:

{'1': {'A': [[1.0, ['A', 'B']], [1.5, ['A', 'CCC']], [2.5, ['A', 'D']]], 'B': [[1.0, ['A', 'B']]], 'D': [[2.5, ['A', 'D']]], 'chef_salad': [[1.0, ['chef_salad']]], 'burger': [[2.0, ['burger']]], 'tofulog': [[1.25, ['tofulog']], [2.0, ['tofulog']]], 'CCC': [[1.5, ['A', 'CCC']]]}, '2': {'A': [[3.0, ['A']]], 'B': [[1.0, ['B']]], 'D': [[1.25, ['D']]], 'CCC': [[1.2, ['CCC']]]}}
SHOP [[2.0, ['burger']]]
Traceback (most recent call last):
File "work.py", line 55, in <module>
solver(shop_text,['burger'])
File "work.py", line 43, in solver
(price, solution) = solve_one_shop(shop_info, required_items)
File "work.py", line 26, in solve_one_shop
for (price,combo) in shop[first_item]:
KeyError: 'burger'

而如果我在 input-2.csv 上运行相同的代码,并查询 solver(shop_text,['A','CCC']),我会得到正确的结果

=====input-2.csv======

1,2.00,A
1,1.25,B
1,2.00,B
1,1.00,A
1,1.00,A+B
1,1.50,A+CCC
1,2.50,A+D
2,3.00,A
2,1.00,B
2,1.20,CCC
2,1.25,D

=========输出====

{'1': {'A': [[2.0, ['A']], [1.0, ['A']], [1.0, ['A', 'B']], [1.5, ['A', 'CCC']], [2.5, ['A', 'D']]], 'B': [[1.25, ['B']], [2.0, ['B']], [1.0, ['A', 'B']]], 'D': [[2.5, ['A', 'D']]], 'CCC': [[1.5, ['A', 'CCC']]]}, '2': {'A': [[3.0, ['A']]], 'B': [[1.0, ['B']]], 'D': [[1.25, ['D']]], 'CCC': [[1.2, ['CCC']]]}}
SHOP [[2.0, ['A']], [1.0, ['A']], [1.0, ['A', 'B']], [1.5, ['A', 'CCC']], [2.5, ['A', 'D']]]
SHOP [[1.5, ['A', 'CCC']]]
SHOP [[1.5, ['A', 'CCC']]]
SHOP [[1.5, ['A', 'CCC']]]
SHOP [[1.5, ['A', 'CCC']]]
SHOP [[3.0, ['A']]]
SHOP [[1.2, ['CCC']]]
SHOP_ID=1
A,CCC(1.50) = 1.50

最佳答案

如果你这样做,你可以找出错误:

在您的 solve_one_shop 方法中,在行 first_item = items[0] 之后打印字典 shop。这样做会打印出:

{'A': [[3.0, ['A']]], 'B': [[1.0, ['B']]], 'D': [[1.25, ['D ']]], 'CCC': [[1.2, ['CCC']]]}

所以,burger 不是它的键之一,因此它抛出一个 KeyError

添加这一行:2,1.25,汉堡到您的 input.csv 文件的末尾,您的代码工作正常。

在 try except block 中从 shop 字典中读取值,以处理项目可能不存在的情况。

注意:
在您的方法 build_shops 行中:

shop_id,cost,items = item_info.replace('\n', '').split(',')

虽然去掉了换行符,但并没有去掉回车符。要解决这个问题,请执行以下操作:

shop_id,cost,items = item_info.replace('\n', '').replace('\r', '').split(',')

希望这对您有所帮助。

关于python代码在一个文件上工作但在其他文件上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22646090/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com