我正在尝试创建基于某些条件的字典组合
下面是主字典:
payload = {
"type": ["sedan","suv"],
"name": ["car1","car2"],
"color": ["black","white","green"],
"version": ["mid","top"],
"model": ["2","5","13"],
}
下面是条件:
color = {
"car1": ["black","green"],
"car2":["white"]
}
model = {
"mid":["5"],
"top":["13","5"]
}
对于有效载荷["name"] = "car1"颜色只能是"黑色"。或";green"即使有效负载["color"]有超过这些颜色。
对于有效负载["name"] = "car2",它只能是"white"颜色。
model也是一样,对于中版本,它只能有model为"5"顶级版本只能有"13"one_answers"5"。
下面是预期的输出:
[
{'type': 'sedan', 'name': 'car1', 'color': 'black', 'version': 'mid', 'model': '5'},
{'type': 'sedan', 'name': 'car1', 'color': 'black', 'version': 'top', 'model': '5'},
{'type': 'sedan', 'name': 'car1', 'color': 'black', 'version': 'top', 'model': '13'},
{'type': 'sedan', 'name': 'car1', 'color': 'green', 'version': 'mid', 'model': '5'},
{'type': 'sedan', 'name': 'car1', 'color': 'green', 'version': 'top', 'model': '5'},
{'type': 'sedan', 'name': 'car1', 'color': 'green', 'version': 'top', 'model': '13'},
{'type': 'suv', 'name': 'car1', 'color': 'black', 'version': 'top', 'model': '5'},
{'type': 'suv', 'name': 'car1', 'color': 'black', 'version': 'top', 'model': '13'},
{'type': 'suv', 'name': 'car1', 'color': 'black', 'version': 'mid', 'model': '5'},
{'type': 'suv', 'name': 'car1', 'color': 'green', 'version': 'top', 'model': '5'},
{'type': 'suv', 'name': 'car1', 'color': 'green', 'version': 'top', 'model': '13'},
{'type': 'suv', 'name': 'car1', 'color': 'green', 'version': 'mid', 'model': '5'},
{'type': 'sedan', 'name': 'car2', 'color': 'white', 'version': 'mid', 'model': '5'},
{'type': 'sedan', 'name': 'car2', 'color': 'white', 'version': 'top', 'model': '5'},
{'type': 'sedan', 'name': 'car2', 'color': 'white', 'version': 'top', 'model': '13'},
{'type': 'suv', 'name': 'car2', 'color': 'white', 'version': 'mid', 'model': '5'},
{'type': 'suv', 'name': 'car2', 'color': 'white', 'version': 'top', 'model': '5'},
{'type': 'suv', 'name': 'car2', 'color': 'white', 'version': 'top', 'model': '13'}
]
下面是我的代码,它带来了所有的组合,我怎么能添加条件检查。有人能帮忙吗?
import itertools
payload = {
"type": ["sedan","suv"],
"name": ["car1","car2"],
"color": ["black","white","green"],
"version": ["mid","top"],
"model": ["2","5","13"],
}
color = {
"car1": ["black","green"],
"car2":["white"]
}
model = {
"mid":["5"],
"top":["13","5"]
}
output = [dict(zip(payload.keys(), a)) for a in itertools.product(*payload.values())]
print(output)
您可以尝试创建一个包含输出的数据框,并为每个条件对其进行筛选,如下所示
import itertools
import pandas as pd
payload = {
"type": ["sedan","suv"],
"name": ["car1","car2"],
"color": ["black","white","green"],
"version": ["mid","top"],
"model": ["2","5","13"],
}
color = {
"car1": ["black","green"],
"car2":["white"]
}
model = {
"mid":["5"],
"top":["13","5"]
}
output = [dict(zip(payload.keys(), a)) for a in itertools.product(*payload.values())]
print(output)
df = pd.DataFrame(output)
# filter for car1 color
df = df.loc[~((df.name == 'car1') & ~(df.color.isin(color['car1']))),:]
# filter for car2 color
df = df.loc[~((df.name == 'car2') & ~(df.color.isin(color['car2']))),:]
#filter for mid model
df = df.loc[~((df.version == 'mid') & ~(df.model.isin(model['mid']))),:]
#filter for topmodel
df = df.loc[~((df.version == 'top') & ~(df.model.isin(model['top']))),:]
df