Python皮|根据条件创建字典组合

dwbf0jvd  于 2022-12-01  发布在  Python
关注(0)|答案(1)|浏览(105)

我正在尝试根据某些条件创建字典组合
下面是主要的词典:

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”,颜色只能是“black”或“green”,即使有效负载[“color”]的颜色不止这些。
对于有效载荷[“name”] =“car2”,它只能是“white”颜色。
同样适用于模型也,对于中等版本,它只能有模型为“5”,对于顶级版本,它只能有“13”和“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)
lfapxunr

lfapxunr1#

您可以尝试使用输出创建一个 Dataframe ,并按如下所示对每个条件进行过滤

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

相关问题