我有一个函数可以返回价格等于或低于指定值的餐馆。这是我目前的代码:
Restaurant = namedtuple('Restaurant', 'name cuisine phone menu')
Dish = namedtuple('Dish', 'name price calories')
r1 = Restaurant('Thai Dishes', 'Thai', '334-4433', [Dish('Mee Krob', 12.50, 500),
Dish('Larb Gai', 11.00, 450)])
r2 = Restaurant('Taillevent', 'French', '01-44-95-15-01',
[Dish('Homard Bleu', 45.00, 750),
Dish('Tournedos Rossini', 65.00, 950),
Dish("Selle d'Agneau", 60.00, 850)])
collection =[r1,r2]
def Collection_is_cheap(C, price):
result = []
if not C:
return ''
else:
for rest in C:
for dish in rest.menu:
if dish.price <= price:
result.append(rest)
return result
但是当我尝试运行它时:
print(Collection_is_cheap(collection, 28))
我得到的是一长串正确的餐馆,但重复了一遍。
[Restaurant(name='Thai Dishes', cuisine='Thai', phone='334-4433', menu=[Dish(name='Mee Krob', price=12.5, calories=500), Dish(name='Larb Gai', price=11.0, calories=450)]), Restaurant(name='Thai Dishes', cuisine='Thai', phone='334-4433', menu=[Dish(name='Mee Krob', price=12.5, calories=500), Dish(name='Larb Gai', price=11.0, calories=450)]), Restaurant(name='Pascal', cuisine='French', phone='940-752-0107', menu=[Dish(name='Escargots', price=12.95, calories=250), Dish(name='Poached salmon', price=18.5, calories=550), Dish(name='Rack of lamb', price=24.0, calories=850), Dish(name='Marjolaine cake', price=8.5, calories=950)]), Restaurant(name='Pascal', cuisine='French', phone='940-752-0107', menu=[Dish(name='Escargots', price=12.95, calories=250), Dish(name='Poached salmon', price=18.5, calories=550), Dish(name='Rack of lamb', price=24.0, calories=850), Dish(name='Marjolaine cake', price=8.5, calories=950)]), Restaurant(name='Pascal', cuisine='French', phone='940-752-0107', menu=[Dish(name='Escargots', price=12.95, calories=250), Dish(name='Poached salmon', price=18.5, calories=550), Dish(name='Rack of lamb', price=24.0, calories=850), Dish(name='Marjolaine cake', price=8.5, calories=950)]), Restaurant(name='Pascal', cuisine='French', phone='940-752-0107', menu=[Dish(name='Escargots', price=12.95, calories=250), Dish(name='Poached salmon', price=18.5, calories=550), Dish(name='Rack of lamb', price=24.0, calories=850), Dish(name='Marjolaine cake', price=8.5, calories=950)])]
对于正确的输出,它应该只打印两个餐馆一次。如何更正此问题,以便该函数仅返回:
[Restaurant(name='Thai Dishes', cuisine='Thai', phone='334-4433', menu=[Dish(name='Mee Krob', price=12.5, calories=500), Dish(name='Larb Gai', price=11.0, calories=450)]), Restaurant(name='Pascal', cuisine='French', phone='940-752-0107', menu=[Dish(name='Escargots', price=12.95, calories=250), Dish(name='Poached salmon', price=18.5, calories=550), Dish(name='Rack of lamb', price=24.0, calories=850), Dish(name='Marjolaine cake', price=8.5, calories=950)]
答案 0 :(得分:3)
当你有比赛时,只需停止在餐厅菜单上循环;使用break
:
def Collection_is_cheap(C, price):
result = []
for rest in C:
for dish in rest.menu:
if dish.price <= price:
result.append(rest)
break # stop the rest.menu loop, go to the next
return result
请注意,我删除了if not C: return ''
部分;最好不要从函数中返回不同类型的对象。
演示:
>>> def Collection_is_cheap(C, price):
... result = []
... for rest in C:
... for dish in rest.menu:
... if dish.price <= price:
... result.append(rest)
... break # stop the rest.menu loop, go to the next
... return result
...
>>> print(Collection_is_cheap(collection, 28))
[Restaurant(name='Thai Dishes', cuisine='Thai', phone='334-4433', menu=(Dish(name='Mee Krob', price=12.5, calories=500), Dish(name='Larb Gai', price=11.0, calories=450))), Restaurant(name='Pascal', cuisine='French', phone='940-752-0107', menu=(Dish(name='Escargots', price=12.95, calories=250), Dish(name='Poached salmon', price=18.5, calories=550), Dish(name='Rack of lamb', price=24.0, calories=850), Dish(name='Marjolaine cake', price=8.5, calories=950)))]
另一种方法是使用 set 而不是列表;集合只能容纳唯一对象,因此不止一次添加餐馆不会产生任何影响:
def Collection_is_cheap(C, price):
result = set()
for rest in C:
for dish in rest.menu:
if dish.price <= price:
result.add(rest)
return list(result)
为了实现这个目的,你还需要让菜单使用元组,而不是列表:
r1 = Restaurant('Thai Dishes', 'Thai', '334-4433', (
Dish('Mee Krob', 12.50, 500),
Dish('Larb Gai', 11.00, 450)))
r2 = Restaurant('Taillevent', 'French', '01-44-95-15-01', (
Dish('Homard Bleu', 45.00, 750),
Dish('Tournedos Rossini', 65.00, 950),
Dish("Selle d'Agneau", 60.00, 850)))
r3 = Restaurant('Pascal', 'French', '940-752-0107', (
Dish('Escargots', 12.95, 250),
Dish('Poached salmon', 18.50, 550),
Dish("Rack of lamb", 24.00, 850),
Dish("Marjolaine cake", 8.50, 950)))
因此它们完全不可变,需要使用集合。
使用set
仅收集唯一餐馆的另一个影响是,返回的餐馆的顺序可能会发生变化,因为集合是无序的。相反,它们以依赖于实现的顺序保存对象,这有利于有效地测试已存在的对象。
演示;对于这个简单的例子,只有两个便宜的餐馆,订单发生以匹配第一个版本返回的:
>>> def Collection_is_cheap(C, price):
... result = set()
... for rest in C:
... for dish in rest.menu:
... if dish.price <= price:
... result.add(rest)
... return list(result)
...
>>> print(Collection_is_cheap(collection, 28))
[Restaurant(name='Thai Dishes', cuisine='Thai', phone='334-4433', menu=(Dish(name='Mee Krob', price=12.5, calories=500), Dish(name='Larb Gai', price=11.0, calories=450))), Restaurant(name='Pascal', cuisine='French', phone='940-752-0107', menu=(Dish(name='Escargots', price=12.95, calories=250), Dish(name='Poached salmon', price=18.5, calories=550), Dish(name='Rack of lamb', price=24.0, calories=850), Dish(name='Marjolaine cake', price=8.5, calories=950)))]
如果您不能使用元组作为菜单序列,并且由于某种原因无法使用break
技巧,则每次都必须使用(慢速且昂贵的)列表成员资格测试:
def Collection_is_cheap(C, price):
result = []
for rest in C:
for dish in rest.menu:
if dish.price <= price and rest not in result:
result.append(rest)
return result
这是缓慢且昂贵的,因为Python将分别测试列表中的每个元素以查看rest == element
是否为真,而使用集合时,使用称为哈希表的技巧来快速检查如果一个对象已经存在,通常只需要进行一次计算检查。