Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我是否为以下问题陈述使用嵌套列表?但如何使用?_Python_List_Nested Lists - Fatal编程技术网

Python 我是否为以下问题陈述使用嵌套列表?但如何使用?

Python 我是否为以下问题陈述使用嵌套列表?但如何使用?,python,list,nested-lists,Python,List,Nested Lists,我应该使用什么数据结构来解决以下问题?一份清单——但如何 送餐服务有3种不同价位的餐食和3位顾客。目前,有一个订单交付列表,每个索引代表一个客户(因此,客户1订购了两顿饭)。我该如何计算每位顾客订购的餐食类型的总收入 meal_types = [happy meal, kids meal, lunch combo] deliveries_ordered = [[2,1,3],[3,1,2],[1,1,3]] meal_price = [1.99, 2.99, 3.99] 也许这就是你想要的 m

我应该使用什么数据结构来解决以下问题?一份清单——但如何

送餐服务有3种不同价位的餐食和3位顾客。目前,有一个订单交付列表,每个索引代表一个客户(因此,客户1订购了两顿饭)。我该如何计算每位顾客订购的餐食类型的总收入

meal_types = [happy meal, kids meal, lunch combo]
deliveries_ordered = [[2,1,3],[3,1,2],[1,1,3]]
meal_price = [1.99, 2.99, 3.99]

也许这就是你想要的

meal_types = ['happy meal', 'kids meal', 'lunch combo']
deliveries_ordered = [[2,1,3],[3,1,2],[1,1,3]]
meal_price = [1.99, 2.99, 3.99]
"""
To grab the inner item in a list, we use deliveries_ordered[0][0] which
returns to 2.
deliveries_ordered = [[[0][0],[0][1],[0][2]],[[1][0],[1][1],[1][2]],[[2][0],[2][1],[2][2]]]
"""

for i in range(len(deliveries_ordered)):
    for j in range(len(deliveries_ordered[i])): # Returns len >> Number of Items in the list.
        x = meal_price[i] * deliveries_ordered[i][j]    # Meal price * Number of orders of meal types.
        print("Customer {} ordered {} {}(s). Price: {}".format(j+1,deliveries_ordered[i][j], meal_types[i], x))
输出:

Customer 1 ordered 2 happy meal(s). Price: 3.98
Customer 2 ordered 1 happy meal(s). Price: 1.99
Customer 3 ordered 3 happy meal(s). Price: 5.97
Customer 1 ordered 3 kids meal(s). Price: 8.97
Customer 2 ordered 1 kids meal(s). Price: 2.99
Customer 3 ordered 2 kids meal(s). Price: 5.98
Customer 1 ordered 1 lunch combo(s). Price: 3.99
Customer 2 ordered 1 lunch combo(s). Price: 3.99
Customer 3 ordered 3 lunch combo(s). Price: 11.97

如何遍历列表中的项目?此外,您还缺少关于如何计算每顿饭价格的部分。这似乎有点像家庭作业问题。你能告诉我们每位顾客都点了什么样的饭菜吗?每种食物的价格是多少?更新的问题-这有意义吗?哇!!我想这就是我在寻找的答案!现在,你能解释一下嵌套循环部分吗:什么是交货顺序[i][j]?