简体   繁体   English

通过匹配字典值来找到列表中字典的已定义键的值

[英]Find the value of the defined key of a dict within a list, by matching the dict values

Input is simple csv file with header what looks like: 输入的是简单的csv文件,其标题如下所示:

dc,environment,type,component,fqdn,distribution,release
1,prod,web,fo,aa,ubuntu,14.04.5

It is loaded by csv.DictReader(csv) into the server_list : 它由csv.DictReader(csv)加载到server_list

def get_server(**kwargs):
    f = open("servers.dat", 'rt')
    try:
        reader = csv.DictReader(f)
        server_list = []
        for row in reader:
            server_list.append(row)
    finally:
        f.close()

List contains: 清单包含:

{'component': 'a', 'dc': '1', 'fqdn': 'aa', 'environment': 'prod', 'release': '14.04.5', 'distribution': 'ubuntu', 'type': 'web'}
{'component': 'a', 'dc': '1', 'fqdn': 'bb', 'environment': 'prod', 'release': '14.04.5', 'distribution': 'ubuntu', 'type': 'web'}
{'component': 'b', 'dc': '1', 'fqdn': 'cc', 'environment': 'prod', 'release': '12.04.5', 'distribution': 'ubuntu', 'type': 'web'}
{'component': 'a', 'dc': '1', 'fqdn': 'dd', 'environment': 'test02', 'release': '14.04.5', 'distribution': 'ubuntu', 'type': 'web'}

I would like to get value of fqdn when input will be eg dc = 1 and component = a from python function called eg get_foo(dc='1', component='a', environment=None) and defined def get_foo(**kwargs) or differently. 我想获得fqdn值,当输入将是dc = 1 and component = a时,例如dc = 1 and component = a从名为get_foo(dc='1', component='a', environment=None)defined def get_foo(**kwargs)或其他方式。 Only result is required. 仅结果是必需的。

So, expected result in this case are all these lines except 3rd. 因此,这种情况下的预期结果是除3rd之外的所有这些行。

Thank you 谢谢

More generally 更普遍

def get_foo(l, **kwargs):
    return [x['fqdn'] for x in l if all(x[i] == kwargs[i] for i in kwargs)]

This will throw a KeyError exception is you pass a keyword argument not in the dictionary x 如果您在字典x未传递关键字参数,则会抛出KeyError异常

Your question states that you have a list of dict s, something like this: 您的问题表明您有一个dict列表,如下所示:

>>> a = [{'component': 'a',
          'dc': '1',
          'distribution': 'ubuntu',
          'environment': 'prod',
          'fqdn': 'aa',
          'release': '14.04.5',
          'type': 'web'},
         {'component': 'a',
          'dc': '1',
          'distribution': 'ubuntu',
          'environment': 'prod',
          'fqdn': 'bb',
          'release': '14.04.5',
          'type': 'web'},
         {'component': 'b',
          'dc': '1',
          'distribution': 'ubuntu',
          'environment': 'prod',
          'fqdn': 'cc',
          'release': '12.04.5',
          'type': 'web'},
         {'component': 'a',
          'dc': '1',
          'distribution': 'ubuntu',
          'environment': 'test02',
          'fqdn': 'dd',
          'release': '14.04.5',
          'type': 'web'}]

You can always just filter the list of dict s using list comprehension syntax: 您总是可以使用列表理解语法来过滤dict 列表

>>> [x['fqdn'] for x in a if x['dc'] == '1' and x['component'] == 'a']
['aa', 'bb', 'dd']

To wrap it up in a function: 要将其包装在一个函数中:

def get_foo(dlist, dc='1', component='a'):
    return [dv['fqdn'] for dv in dlist
            if dv['dc'] == dc
            and dv['component'] == component]

And then: 接着:

>>> get_foo(a)
['aa', 'bb', 'dd']

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

相关问题 通过匹配字典的值,在列表中查找字典的索引 - Find the index of a dict within a list, by matching the dict's value 对于列表值的字典,找到列表中每个索引的最大值的键 - For a dict of list values, find the key with the maximum value for each index in the list 将列表值匹配为dict并返回新dict中的键/值对 - Match list values to dict and return key/value pairs in new dict 从字典列表到使用值作为键/值对的单个字典 - From list of dict to single dict using values as key/value pairs 如何通过键入列表查找字典值 - How to find dict value by Key in list 如何在Dict {key:List}中找到最小值? - How to find the minimum value in a Dict{key:List}? 如何基于Python中的匹配键将键值对添加到另一个字典列表中的现有字典列表中 - How to append key value pair to an existing list of dict from another list of dict based on matching Key in Python 用字典A值替换字典B中的键值 - Replacing key value in dict B with dict A values 如何在字典的匹配值(包含列表)包含X的字典中找到键? - How can I find the key in a dict whose matching value, which is a list, contains X? 将字典的值与 dataframe 的两列值进行匹配,并将第三列的值替换为字典的键 - Matching values of a dict with the values of two columns of a dataframe and substituting the value of a third column with the key of the dict
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM