从字典中列出所有组合

时间:2014-05-31 10:07:39

标签: python json dictionary combinations itertools

我有一个从这个结构的JSON文件收到的字典:

{
    "First dish": {
        "Soup": {
            "Cabbage": 100,
            "Basis": {
                "Meat": 100,
                "Fish": 80
            }
        },
        "Salad": {
            "Main ingredients": {
                 "Tomato": 150,
                 "Сucumber": 100
            }
        }
    },
    "Wine": 150
}

这道菜由葡萄酒,汤或沙拉组成。我想从字典中收到不同成分组合的所有组合列表:

[[{'Cabbage': 100}, {'Meat': 80}, {'Wine': 150}],
[{'Tomato': 100}, {'Wine': 150}],
[{'Cabbage': 100}, {'Fish': 80}, {'Wine': 150}],
[{'Сucumber': 100}, {'Wine': 150}]]

如何在Python上完成?

1 个答案:

答案 0 :(得分:0)

@ghoso我担心这个问题不清楚 - 所描述的词典在汤下有Basis,在沙拉下有main ingrediants,而cabbage看起来像Basis的兄弟一个完整的数据结构沙拉:)

您可以使用itertools.permutations查找可迭代的所有可能排列但是为此,您应该正确定义数据结构。祝你好运