class Solution:
def displayTable(self, orders: List[List[str]]) -> List[List[str]]:
foods = set()
res = []
table_info = collections.defaultdict(lambda: collections.defaultdict(int))
for _, table, food in orders:
table_info[table][food] += 1
foods.add(food)
foods = sorted(foods)
res.append(['Table'] + foods)
for info in sorted(table_info, key=lambda x: int(x)):
temp = [info]
for food in foods:
temp.append(str(table_info.get(info).get(food, 0)))
res.append(temp)
return res
lambda x:int(x)= def f(x):
return int(x)
:前面的x表示要传入的参数,:后面表示要输出的结果
from collections import defaultdict
table_info = defaultdict(lambda: defaultdict(int))
table_info['1']['fodd']=1
print(table_info)
{'1': defaultdict(<class 'int'>, {'fodd': 1})})
嵌套字典,