首页 > 解决方案 > 在 Python 的 if 语句中创建值数组

问题描述

我想根据条件创建一个值数组。

data_total = [
    {"modele": "DS",
     "cote_actual": 10000},
    {"modele": "DS",
     "cote_actual": 12000},
    {"modele": "204",
     "cote_actual": 10000}]

for total in data_total:
    model = total["modele"]
    cote_actual = total["cote_actual"]
    model_ds = []

    if model == "DS":
        model_cote = cote_actual
        print(model_cote)
        model_ds.append(model_cote)
        print(model_ds)

我想要的输出:

"modele_ds" = [10000, 12000]

我弄乱了循环,我没有我需要的输入。我知道我必须附加或填充数组。

标签: python

解决方案


[ x['cote_actual'] for x in data_total if x['modele'] == 'DS‘]

推荐阅读