简体   繁体   English

如何检查字典列表中键的值是否都等于 0

[英]How check if values of keys in list of dictionaries are both equal 0

I have a list of dictionaries like:我有一个字典列表,如:

dicts = [dict1,dict2,dict3,dict4]

dict1 = [{'name': 'blue', 'y': 1}, {'something else': 'red', 'y': 0}]  

dict2 = [{'name': 'green', 'y': 0}, {'name': 'purple', 'y':2}]

dicts are list of dictionaries dicts 是字典列表

How can I check if both values in dict1 first and second y value is 0 etc. If dict have both y == 0 then assign empty array如何检查 dict1 中的第一个值和第二个 y 值是否为 0 等。如果 dict 都有 y == 0 则分配空数组

I have tried also something like this but have to repeat this so many times for each dict我也尝试过这样的事情,但必须为每个 dict 重复很多次

check = 0
        for el in dict1:
            if el['y'] == 0:
                check += 1
            if check == len(dict1):
                dict1 = []
    for el in dicts:
            for y in el:
                if all(x == 0 for x in y.values()):
                    el = []

You need something like this.你需要这样的东西。

dicts=[]
new_dicts=[]
for dic in dicts:
    flag=0
    for el in dic:
        if el['y']!=0:
            flag=1
            break
    if flag==0:
        new_dict.append([])
    else:
        new_dict.append(dic)
[[] if all([i['y']==0 for i in item]) else item for item in dicts]

Take example举个例子

dict1 = [{'name': 'blue', 'y': 1}, {'something else': 'red', 'y': 0}]
dict2 = [{'name': 'green', 'y': 0}, {'name': 'purple', 'y':2}]
dict3 = [{'name': 'green', 'y': 0}, {'name': 'purple', 'y':0}]
dict4 = [{'name': 'green', 'y': 0}, {'name': 'purple', 'y':2}]
dicts = [dict1,dict2,dict3,dict4]

The output would be输出将是

[[{'name': 'blue', 'y': 1}, {'something else': 'red', 'y': 0}],
 [{'name': 'green', 'y': 0}, {'name': 'purple', 'y': 2}],
 [],
 [{'name': 'green', 'y': 0}, {'name': 'purple', 'y': 2}]]

If a one-line solution is too complex, split up the task and enumerate your dicts to replace the element you checked to have all {"y":0} :如果单行解决方案太复杂,请拆分任务并枚举您的dicts以替换您检查的所有元素{"y":0}

dict1 = [{'name': 'blue', 'y': 1}, {'something else': 'red', 'y': 0}]  
dict2 = [{'name': 'green', 'y': 0}, {'name': 'purple', 'y':2}]
dict3 = [{'name': 'green', 'y': 0}, {'name': 'purple', 'y':0}]
dict4 = [{'name': 'green', 'y': 0}, {'name': 'purple', 'y':2}]

dicts = [dict1,dict2,dict3,dict4]


for i,d in enumerate(dicts):
    zeros = all(i["y"]== 0 for i in d)   # check if all are 0
    if zeros:
        dicts[i] = []                    # if so replace element in dicts by []

print (dicts)

Outputs:输出:

[[{'name': 'blue', 'y': 1}, {'something else': 'red', 'y': 0}], 
 [{'name': 'green', 'y': 0}, {'name': 'purple', 'y': 2}], 
 [], 
 [{'name': 'green', 'y': 0}, {'name': 'purple', 'y': 2}]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果两个键都在两个单独的字典中,如何获取字典键和值? - How to get dictionary keys and values if both keys are in two separated dictionaries? 如果两个字典的键相等,则使用两个值执行操作并移动到新的字典(python) - If keys of two dictionaries are equal, perform operation using both values and move to new dict (python) 检查字典列表中的值是否在没有键的情况下是唯一的 - Check if Values from List of Dictionaries are Unique Without Keys 如何从键列表和值列表创建字典列表 - How to create a list of dictionaries from a list of keys and a list of values 从字典列表返回值列表,其中键等于某个值 - Returning a list of values from a list of dictionaries where keys equal a certain value 按字典列表中的键对值进行分组 - Group values by keys in list of dictionaries 如何在字典列表中的键中查找列表值? - How to find list values amongst keys in list of dictionaries? 如何将包含多个词典的值列表的键解压缩到列表而不覆盖? - How to unpack keys with list of values to multiple dictionaries to a list without overwriting? 检查2个列表中有多少个值相等 - Check how many Values are equal in 2 list 如何基于与键关联的值列表创建字典? - How to create dictionaries based on a list of values associated with keys?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM