检查字典键是否存在列表元素,如果存在,则创建值列表,否则创建"None" [英] Check dictionary keys for presence of list elements, create list of values if present, 'None' otherwise

查看:58
本文介绍了检查字典键是否存在列表元素,如果存在,则创建值列表,否则创建"None"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请原谅繁琐的标题,但我找不到表达问题的更优雅的方法.我可以在找到壁橱的问题,但这并不能完全解决我的问题.

Forgive the convoluted title, but I couldn't find a more elegant way to express the problem. The closet question I could locate can be found here, but it doesn't quite get me there.

假设许多字典的长度不同:

Assume a number of dictionaries of varying lengths:

dict_1 = {'A':4, 'C':5}
dict_2 = {'A':1, 'B':2, 'C':3}
dict_3 = {'B':6}

这些词典的共同点是它们都共享此列表上的键:

The common denominator for these dictionaries is that they all share the keys on this list:

my_keys= ['A','B','C']

但是有些缺少一个或多个键.

but some are missing one or more of the keys.

目的是创建一个列表列表,其中每个列表元素都是每个字典中所有现有值的列表,或者"None"(不存在特定键).它们的键本身在所有词典中都是相同的,可以忽略.

The intent is to create a list of lists, where each list element is a list of all the existing values in each dictionary, or 'None' where a specific key isn't present. They keys themselves, being identical across all dictionaries, can be disregarded.

因此,在这种情况下,预期输出为:

So in this case, the expected output is:

    final_list = 
    [[4,"None",5],
    [1,2,3],
    ["None",6,"None"]]

我不确定该如何处理.您可以从 my_keys 中的每个元素开始,然后针对每个字典中的每个键检查其是否存在;如果相关字典具有键,则将该键的值附加到临时列表中;否则,将附加无".一旦所有 my_keys 都经过迭代,临时列表将追加到最终列表中,并且循环将再次开始.

I'm not sure exactly how to approach it. You could start with each element in my_keys, and check its presence against each key in each dictionary; if the relevant dict has the key, the value of that key is appended to a temporary list; otherwise, 'None' is appended. Once the my_keys are all iterated over, the temp list is appended to a final list and the cycle start again.

至少对我来说,说起来容易做起来难.我尝试了很多事情(我不会费心去发布,因为它们甚至还差得很远).所以我想知道是否有一种优雅的方法来解决这个问题.

To me at least, it's easier said than done. I tried quite a few things (which I won't bother to post because they didn't get even close). So I was wondering if there is an elegant approach to the problem.

推荐答案

dict.get 可以返回默认值(例如, None ).如果我们以您的示例为例:

dict.get can return a default value (for example, None). If we take your examples:

dict_1 = {'A':4, 'C':5}
dict_2 = {'A':1, 'B':2, 'C':3}
dict_3 = {'B':6}
my_keys= ['A','B','C']

然后 dict_1.get('B',None)是确保我们获得默认 None 值的方法.我们可以以相同的方式遍历所有键:

Then dict_1.get('B', None) is the way to make sure we get a default None value. We can loop across all keys the same way:

def dict_to_list(d, keys):
    return [d.get(key, None) for key in keys]

示例:

>>> dict_to_list(dict_1, my_keys)
[4, None, 5]
>>> dict_to_list(dict_2, my_keys)
[1, 2, 3]
>>> dict_to_list(dict_3, my_keys)
[None, 6, None]

是默认参数,即使未明确指定,因此 dict_1.get('B') dict_1一样有效.get('B',None)

None is the default argument even if it's not explicitly specified, so dict_1.get('B') would work just as well as dict_1.get('B', None)

这篇关于检查字典键是否存在列表元素,如果存在,则创建值列表,否则创建"None"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆