返回一个以菜为关键字的词典,以及菜肴作为价值订购的次数

Pol*_*sop -2 python parallels tuples

def get_quantities(table_to_foods):
    """ (dict of {str: list of str}) -> dict of {str: int}

    The table_to_foods dict has table names as keys (e.g., 't1', 't2', and so on) and each value
    is a list of foods ordered for that table.

    Return a dictionary where each key is a food from table_to_foods and each
    value is the quantity of that food that was ordered.

    >>> get_quantities({'t1': ['Vegetarian stew', 'Poutine', 'Vegetarian stew'], 't3': ['Steak pie', 'Poutine', 'Vegetarian stew'], 't4': ['Steak pie', 'Steak pie']})
    {'Vegetarian stew': 3, 'Poutine': 2, 'Steak pie': 3}    
    """

    food_to_quantity = {}

    # Accumulate the food information here.
    # I have no idea what it should be at here.

    return food_to_quantity
Run Code Online (Sandbox Code Playgroud)

如何正确编写此代码?当我使用元组时,我会尝试它,但我不知道如何计算时间.

Jor*_*ley 6

from collections import counter
from itertools import chain
Counter(chain(*table_to_foods.values()))
Run Code Online (Sandbox Code Playgroud)

但我不确定你的老师会接受这个......