Python:如何返回一个字典,其中每个键都是食物,每个值都是订购的食物数量?

Python:如何返回一个字典,其中每个键都是食物,每个值都是订购的食物数量?,python,python-3.x,Python,Python 3.x,我无法导入任何内容(集合/链)。我在下面尝试了两种选择,但都失败了 这会使程序超时: 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 li

我无法导入任何内容(集合/链)。我在下面尝试了两种选择,但都失败了

这会使程序超时:

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.

    return food_to_quantity

你真的很接近。您的第二个代码片段:

for table_order in table_to_foods.itervalues():
    for menu_item in table_order:
        if menu_item in food_to_quantity:
            food_to_quantity[menu_item] += 1
        else:
            food_to_quantity[menu_item] = 1 
正在使用旧的Python2
itervalues
方法,但您使用的是Python3。您应该使用
values
,这相当于Python2.7
viewvalues
方法。改变了这一点,这个代码段应该可以工作了


您的第一个代码段有更多问题。例如,
len(table)
是表名中的字符数,而不是您感兴趣的任何内容,并且您尝试使用3个角色不明确的循环。您还将
count+=1
放在使用
count

的循环之外,您非常接近。您的第二个代码片段:

for table_order in table_to_foods.itervalues():
    for menu_item in table_order:
        if menu_item in food_to_quantity:
            food_to_quantity[menu_item] += 1
        else:
            food_to_quantity[menu_item] = 1 
正在使用旧的Python2
itervalues
方法,但您使用的是Python3。您应该使用
values
,这相当于Python2.7
viewvalues
方法。改变了这一点,这个代码段应该可以工作了

您的第一个代码段有更多问题。例如,
len(table)
是表名中的字符数,而不是您感兴趣的任何内容,并且您尝试使用3个角色不明确的循环。您还将
count+=1
放置在使用
count

的循环之外,这样做有效

for table_order in table_to_foods.itervalues():
    for menu_item in table_order:
        if menu_item in food_to_quantity:
            food_to_quantity[menu_item] += 1
        else:
            food_to_quantity[menu_item] = 1
这很有效

for table_order in table_to_foods.itervalues():
    for menu_item in table_order:
        if menu_item in food_to_quantity:
            food_to_quantity[menu_item] += 1
        else:
            food_to_quantity[menu_item] = 1

您不需要
项目列表
,如果项目列表中的项目:,您可以将
替换为
如果结果中的项目:
。如果您不允许使用这些之外的字典方法,那么这是您所能做的最好的事情。内部循环体可以使用
get
方法写在一行中:
result[item]=result.get(item,0)+1
这在编码系统中不起作用(很遗憾),但我更喜欢它。我没有足够的声誉来“正式”投票支持你,但我还是这么做了,因为我真的很感激你花了这么多时间来研究这个问题,并提供建议/反馈。你不需要
项目列表
,你可以用
如果结果中的项目:
替换
项目列表中的项目:
。如果您不允许使用这些之外的字典方法,那么这是您所能做的最好的事情。内部循环体可以使用
get
方法写在一行中:
result[item]=result.get(item,0)+1
这在编码系统中不起作用(很遗憾),但我更喜欢它。我没有足够的声誉来“正式”投票支持你,但我还是这么做了,因为我真的很感激你花了这么多时间来研究这个问题并提供建议/反馈谢谢你!一旦我把它的右值改为价值观,它就非常有效:)我很感激你花时间向我解释事情,而且我对你投了更高的票,尽管这没有表现出来,因为我没有足够的声誉谢谢你!一旦我将ITERVALUE改为values,这种方法就非常有效:)我很感激你花时间向我解释事情,我对你投了更高的票,尽管没有表现出来,因为我没有足够的声誉