Python构建动态if条件或/和

2024-04-23 20:02:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下示例,其中过滤JSON格式和打印数据:

arr = [
    {
        "EffectiveDate": "2018-05-01T00:00:00Z",
        "IncludedQuantity": 0.0,
        "MeterCategory": "VM",
        "Unit": "1 GB/Month",
        "MeterName": "P4 Disks"
    },
    {
        "EffectiveDate": "2018-03-14T00:00:00Z",
        "IncludedQuantity": 0.0,
        "MeterCategory": "Storage",
        "MeterName": "P4 Disks"
    },
    {
        "EffectiveDate": "2017-04-01T00:00:00Z",
        "IncludedQuantity": 0.0,
        "MeterCategory": "VM",
        "Unit": "1 Hour",
        "MeterName": "P4 Disks"
    }
]

def get_data(getarr):
    for data in getarr:
        if data['MeterCategory'] == "VM"\
            and data['MeterName'] == "P4 Disks"\
                and data['Unit'] == "1 GB/Month":
            print(data)

get_data(arr)

我想在函数get_data中动态指定“AND”条件。 假设我想在以下条件下运行函数get_data:

例1。仅按“MeterCategory”搜索:

if data['MeterCategory'] == "VM":

例2。按“MeterCategory”和“MeterName”搜索:

  if data['MeterCategory'] == "VM"\
           and data['MeterName'] == "P4 Disks":

一种方法是使用语句组合定义不同的函数,但如果我想动态控制和运算符,我发现只有这种方法:

def get_data(getarr, filter_type):
    filters = None
    r1 = "obj['MeterName'] == 'P4 Disks'"
    r2 = "obj['MeterName'] == 'P4 Disks' and obj['MeterCategory'] == 'Storage'"

    if (filter_type == "filter1"): filters = r1
    if (filter_type == "filter2"): filters = r2

    data = [obj for obj in getarr if(eval(filters))]
    print(data)

get_data(arr, 'filter2')

你能给我一些建议吗。谢谢


Tags: andobjdatagetifvmfiltersarr
1条回答
网友
1楼 · 发布于 2024-04-23 20:02:40

您可以将条件作为键和值的字典传递。然后在你的函数中,只要在它们上面循环,如果你的任何条件都不满足,那么就打破循环,因为这是逻辑,所有条件都必须为真。因此,如果任何一个失败了,就打断并停止检查其他的

假设所有条件都为真,循环将成功完成,然后进入循环的其他部分,这意味着我们可以打印数据,因为我们成功地通过了所有条件

arr = [
    {
        "EffectiveDate": "2018-05-01T00:00:00Z",
        "IncludedQuantity": 0.0,
        "MeterCategory": "VM",
        "Unit": "1 GB/Month",
        "MeterName": "P4 Disks"
    },
    {
        "EffectiveDate": "2018-03-14T00:00:00Z",
        "IncludedQuantity": 0.0,
        "MeterCategory": "Storage",
        "MeterName": "P4 Disks"
    },
    {
        "EffectiveDate": "2017-04-01T00:00:00Z",
        "IncludedQuantity": 0.0,
        "MeterCategory": "VM",
        "Unit": "1 Hour",
        "MeterName": "P4 Disks"
    }
]


def get_data(getarr, conditions):
    return_data = []
    for data in getarr:
        for key, value in conditions.items():
            if key in data and data[key] != value:
                break
        else:
            return_data.append(data)
    return return_data

print(get_data(arr, {"MeterCategory": "VM", "Unit": "1 GB/Month"}))
print(get_data(arr, {"MeterCategory": "VM"}))
print(get_data(arr, {"MeterCategory": "Storage"}))

输出

[{'EffectiveDate': '2018-05-01T00:00:00Z', 'IncludedQuantity': 0.0, 'MeterCategory': 'VM', 'Unit': '1 GB/Month', 'MeterName': 'P4 Disks'}]
[{'EffectiveDate': '2018-05-01T00:00:00Z', 'IncludedQuantity': 0.0, 'MeterCategory': 'VM', 'Unit': '1 GB/Month', 'MeterName': 'P4 Disks'}, {'EffectiveDate': '2017-04-01T00:00:00Z', 'IncludedQuantity': 0.0, 'MeterCategory': 'VM', 'Unit': '1 Hour', 'MeterName': 'P4 Disks'}]
[{'EffectiveDate': '2018-03-14T00:00:00Z', 'IncludedQuantity': 0.0, 'MeterCategory': 'Storage', 'MeterName': 'P4 Disks'}]

相关问题 更多 >