我有一个函数,可以返回价格等于或低于指定值的餐厅。这是我当前的代码:

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)]

最佳答案

比赛结束后,停止在餐厅菜单上循环播放;为此使用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)))]


另一种方法是使用集合而不是列表。集只能容纳唯一的对象,因此多次添加餐厅不会产生任何效果:

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是否为真,而使用set的技巧称为哈希表,用于快速检查对象是否已经存在,通常只需要花费一些时间即可一次计算检查。

关于python - 遍历namedtuple列表,选择餐厅,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31634825/

10-09 12:35