Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将2个目录组合成一个目录_Python_List_Dictionary - Fatal编程技术网

Python 将2个目录组合成一个目录

Python 将2个目录组合成一个目录,python,list,dictionary,Python,List,Dictionary,我有两张单子,上面写着: 详细资料: [ { 'rds_name': u'emsclassicldap', 'rds_type': u'db.m3.medium'}, { 'rds_name': u'fra01-devops-KPX', 'rds_type': u'db.t2.medium'}, { 'rds_name': u'prodreplica', 'rds_type': u'db.t2.medium'} ] cloudwatch_详细信息: [ {

我有两张单子,上面写着:

详细资料:

[   {   'rds_name': u'emsclassicldap', 'rds_type': u'db.m3.medium'},
    {   'rds_name': u'fra01-devops-KPX', 'rds_type': u'db.t2.medium'},
    {   'rds_name': u'prodreplica', 'rds_type': u'db.t2.medium'}
]
cloudwatch_详细信息:

[   {   'alarm_name': u'emsclassicldap_db_connections', 'alarm_threshold': 380.0},
    {   'alarm_name': u'fra01-devops-KPX_db_connection',
        'alarm_threshold': 266.0},
    {   'alarm_name': u'prodreplica_db_connections',
        'alarm_threshold': 266.0},
]
alarm\u name
实际上有
rds\u name
作为其子串;我需要根据这个条件将这两个列表合并成一个,这样最终的结果应该是

[
    {   'rds_name': u'emsclassicldap', 'rds_type': u'db.m3.medium','alarm_name': u'classicldap_db_connections', 'alarm_threshold': 380.0}
    .
    .
    So on
    . 
    .
]
我正在编写一个简单的def来组合:

def combine_rds_cloudwatch(rds_detail,cloudwatch_detail):
    print rds_detail,cloudwatch_detail

    for rds in rds_detail:
        for alarm in cloudwatch_detail:
            if ????????????
不确定如何执行此操作,

将为您提供另一本词典,其中包含两个词典中的项目:

>>> d1 = {'a': 1, 'b': 2}
>>> d2 = {'c': 3, 'd': 4}
>>> dict(d1, **d2)  # => dict({'a':1,'b':2}, c=3, d=4)
{'a': 1, 'c': 3, 'b': 2, 'd': 4}
您可以使用(两个for子句)制作产品,并使用
if
子句对其进行过滤:

def combine_rds_cloudwatch(rds_detail,cloudwatch_detail):
    return [dict(rds, **alarm) for rds in rds_detail
                               for alarm in cloudwatch_detail
                               if rds['rds_name'] in alarm['alarm_name']]
                          # OR if alarm['alarm_name'].startswith(rds['rds_name'])
并且,将为您提供另一本词典,其中包含两个词典中的项目:

>>> d1 = {'a': 1, 'b': 2}
>>> d2 = {'c': 3, 'd': 4}
>>> dict(d1, **d2)  # => dict({'a':1,'b':2}, c=3, d=4)
{'a': 1, 'c': 3, 'b': 2, 'd': 4}
您可以使用(两个for子句)制作产品,并使用
if
子句对其进行过滤:

def combine_rds_cloudwatch(rds_detail,cloudwatch_detail):
    return [dict(rds, **alarm) for rds in rds_detail
                               for alarm in cloudwatch_detail
                               if rds['rds_name'] in alarm['alarm_name']]
                          # OR if alarm['alarm_name'].startswith(rds['rds_name'])

您可以使用
startswith
检查一个字符串是否以另一个字符串开头:

from copy import deepcopy
result = []
for rds in rds_detail:
    temp = deepcopy(rds)
    for cw in cloudwatch_detail:
        # replace the condition with rds['rds_name'] in cw['alarm_name'] if the condition 
        # is substring
        if cw['alarm_name'].startswith(rds['rds_name']):
            temp.update(cw)
    result.append(temp)

result
#[{'alarm_name': 'emsclassicldap_db_connections',
#  'alarm_threshold': 380.0,
#  'rds_name': 'emsclassicldap',
#  'rds_type': 'db.m3.medium'},
# {'alarm_name': 'fra01-devops-KPX_db_connection',
#  'alarm_threshold': 266.0,
#  'rds_name': 'fra01-devops-KPX',
#  'rds_type': 'db.t2.medium'},
# {'alarm_name': 'prodreplica_db_connections',
#  'alarm_threshold': 266.0,
#  'rds_name': 'prodreplica',
#  'rds_type': 'db.t2.medium'}]

您可以使用
startswith
检查一个字符串是否以另一个字符串开头:

from copy import deepcopy
result = []
for rds in rds_detail:
    temp = deepcopy(rds)
    for cw in cloudwatch_detail:
        # replace the condition with rds['rds_name'] in cw['alarm_name'] if the condition 
        # is substring
        if cw['alarm_name'].startswith(rds['rds_name']):
            temp.update(cw)
    result.append(temp)

result
#[{'alarm_name': 'emsclassicldap_db_connections',
#  'alarm_threshold': 380.0,
#  'rds_name': 'emsclassicldap',
#  'rds_type': 'db.m3.medium'},
# {'alarm_name': 'fra01-devops-KPX_db_connection',
#  'alarm_threshold': 266.0,
#  'rds_name': 'fra01-devops-KPX',
#  'rds_type': 'db.t2.medium'},
# {'alarm_name': 'prodreplica_db_connections',
#  'alarm_threshold': 266.0,
#  'rds_name': 'prodreplica',
#  'rds_type': 'db.t2.medium'}]

更通用的方法

rds_list = [{'rds_name': u'emsclassicldap', 'rds_type': u'db.m3.medium'},
            {'rds_name': u'fra01-devops-KPX', 'rds_type': u'db.t2.medium'},
            {'rds_name': u'goldenprodreplica', 'rds_type': u'db.t2.medium'}
            ]

cloudwatch_list = [{'alarm_name': u'emsclassicldap_db_connections', 'alarm_threshold': 380.0},
                   {'alarm_name': u'fra01-devops-KPX_db_connection', 'alarm_threshold': 266.0},
                   {'alarm_name': u'goldenprodreplica_db_connections', 'alarm_threshold': 266.0},
                   ]


def merge_two_dicts(x, y):
    """Given two dicts, merge them into a new dict as a shallow copy.
       More info here http://stackoverflow.com/questions/38987/how-to-merge-two-python-dictionaries-in-a-single-expression?rq=1
    """
    z = x.copy()
    z.update(y)
    return z


def combine_list(prefix_key_list, prefix_key, keys_list, key):
    combined_list = []

    for short in prefix_key_list:
        for long in keys_list:
            if long[key].startswith(short[prefix_key]):
                result = merge_two_dicts(long, short)
                combined_list.append(result)

    return combined_list


print(combine_list(rds_list, 'rds_name', cloudwatch_list, 'alarm_name'))

更通用的方法

rds_list = [{'rds_name': u'emsclassicldap', 'rds_type': u'db.m3.medium'},
            {'rds_name': u'fra01-devops-KPX', 'rds_type': u'db.t2.medium'},
            {'rds_name': u'goldenprodreplica', 'rds_type': u'db.t2.medium'}
            ]

cloudwatch_list = [{'alarm_name': u'emsclassicldap_db_connections', 'alarm_threshold': 380.0},
                   {'alarm_name': u'fra01-devops-KPX_db_connection', 'alarm_threshold': 266.0},
                   {'alarm_name': u'goldenprodreplica_db_connections', 'alarm_threshold': 266.0},
                   ]


def merge_two_dicts(x, y):
    """Given two dicts, merge them into a new dict as a shallow copy.
       More info here http://stackoverflow.com/questions/38987/how-to-merge-two-python-dictionaries-in-a-single-expression?rq=1
    """
    z = x.copy()
    z.update(y)
    return z


def combine_list(prefix_key_list, prefix_key, keys_list, key):
    combined_list = []

    for short in prefix_key_list:
        for long in keys_list:
            if long[key].startswith(short[prefix_key]):
                result = merge_two_dicts(long, short)
                combined_list.append(result)

    return combined_list


print(combine_list(rds_list, 'rds_name', cloudwatch_list, 'alarm_name'))
这个

将给予

[{'alarm_name': u'emsclassicldap_db_connections', 
  'alarm_threshold': 380.0,
  'rds_name': u'emsclassicldap',
  'rds_type': u'db.m3.medium'},
 {'alarm_name': u'fra01-devops-KPX_db_connection',
  'alarm_threshold': 266.0,
  'rds_name': u'fra01-devops-KPX',
  'rds_type': u'db.t2.medium'},
 {'alarm_name': u'prodreplica_db_connections',
  'alarm_threshold': 266.0,
  'rds_name': u'prodreplica',
  'rds_type': u'db.t2.medium'}]
输出是否正确

如果您知道两个列表中元素的顺序,您也可以这样做

pprint([dict(x.items() + y.items()) for x, y in zip(rds_detail, cloudwatch_detail)])
这个

将给予

[{'alarm_name': u'emsclassicldap_db_connections', 
  'alarm_threshold': 380.0,
  'rds_name': u'emsclassicldap',
  'rds_type': u'db.m3.medium'},
 {'alarm_name': u'fra01-devops-KPX_db_connection',
  'alarm_threshold': 266.0,
  'rds_name': u'fra01-devops-KPX',
  'rds_type': u'db.t2.medium'},
 {'alarm_name': u'prodreplica_db_connections',
  'alarm_threshold': 266.0,
  'rds_name': u'prodreplica',
  'rds_type': u'db.t2.medium'}]
输出是否正确

如果您知道两个列表中元素的顺序,您也可以这样做

pprint([dict(x.items() + y.items()) for x, y in zip(rds_detail, cloudwatch_detail)])

我将迭代列表,获取
rds\u name
的值,并在列表2中使用
[索引]['alarm\u name']
进行dict更新并合并到新列表中。您可以使用
in
操作符搜索子字符串,例如,如果rds['rds\u name']在alarm\u name']中使用
if rds['rds\u name']来搜索if语句。您需要返回一个新的词典列表。尝试试用
dict.items()
,它允许您迭代字典条目。我将迭代列表,获取
rds\u name
的值,并在列表2中使用
[索引]['alarm\u name']
执行dict更新并合并到新列表中。您可以使用
in
操作符搜索子字符串,例如,如果rds报警['alarm\u name']中的['rds\u name']:
用于该if语句。您需要返回新的词典列表。尝试使用
dict.items()进行试验
,它允许您迭代字典的条目。但它是否检查我提到的条件?rds\u name是alarm\u name中的子字符串,它必须检查first@NishantSingh,我错过了。我相应地更新了答案。我理解,你能打开列表理解吗(我需要理解,这里还是新的)@尼桑辛格,我添加了一个到教程的链接(列表理解)。我希望它能帮助您理解。给定的列表理解基本上类似于带有if语句的嵌套for循环。但它是否检查我提到的条件?rds_name是alarm_name中的子字符串,它必须检查first@NishantSingh,我错过了。我相应地更新了答案。我明白了,你能打开清单吗ion(我需要理解它,这里还是新的)@NishantSingh,我添加了教程的链接(列表理解)。我希望这有助于您理解。给定的列表理解基本上类似于带if语句的嵌套for循环。这是在cw中更新的?我需要返回一个seprate列表,,,那么我应该在for循环上方打开一个新列表并附加吗?这是在cw中更新的?我需要返回一个seprate列表,,,那么我应该只打开一个for循环和append上方的新列表?