简体   繁体   中英

Combine two dict into one at different levels of indent

I have python3 code how to combine two dictionaries 'd','dconn' in one, if they are in different levels indent. I newbie in python3, help me please.

code approximately looks like that:

for ip in fp.read().splitlines():
    d={}
    if p.poll():
        d.update({ip : '1'}) 
    else:
        d.update({ip : '0'})

    for keys, value in d.items():
        print(keys, value)


dconn = {}
if connection.username == 'REPORT': 
    dconn.update({connection.username : '0'}) 
else:
    dconn.update({connection.username : '1'}) 

for keys, value in dconn.items():
    print(keys, value)

You need to make d Global and then concatenate both dictionaries like so:

newDict = dict(d.items() + dconn.items())

or to add one dictionary into another do:

dict.update(dict2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM