1418. 点菜展示表





class Solution(object):
    def displayTable(self, orders):
        """
        :type orders: List[List[str]]
        :rtype: List[List[str]]
        """
        # 返回值
        ans = []
        # 第一趟遍历,listFood统计菜品名称,mydict按桌号统计菜品
        listFood = []
        mydict = {}
        for item in orders:
            table = item[1]
            food = item[2]
            if food not in listFood:
                listFood.append(food)
            if table in mydict.keys():
                mydict[table].append(food)
            else:
                mydict[table] = [food]
        # 菜品按字母升序排
        listFood.sort()
        listFood = ["Table"] + listFood
        # 桌号按升序排
        mylist = sorted(mydict.items(), key=lambda x: int(x[0]), reverse=False)
        ans.append(listFood)
        # 遍历mydict,
        for k, v in mylist:
            temp = [str(k)]
            for food in listFood[1:]:
                if food in v:
                    temp.append(str(v.count(food)))
                else:
                    temp.append(str(0))
            ans.append(temp)
        return ans
原文地址:https://www.cnblogs.com/panweiwei/p/14025044.html