简体   繁体   中英

store the output of dictionary given from the forloop in python

I want to save (store) the output of the dictionary iterating with the help of for loop.

a_known_nodes = ['10.10.10.10', '100.10.10.10']

node_tree = {
    'device_type': 'texas',
    'ip': ' ',
    'username': 'lab',
    'password': 'lab',
}

a_s1_[i] = {}
for i in a_known_nodes:
    node_tree['ip'] = i
    print(a_s1_[i])

Expected output:

a_s1_1 = {'device_type': 'texas', 'ip': '100.10.10.10', 'username': 'lab', 'password': 'lab'}
a_s1_2 = {'device_type': 'texas', 'ip': '10.10.10.10', 'username': 'lab', 'password': 'lab'}

Appreciate your help!

I think you have to have the variables as a dict like this

a_known_nodes = ['10.10.10.10', '100.10.10.10']

node_tree = { 'device_type': 'texas', 'ip': ' ', 'username': 'lab', 'password': 'lab', }

result = {}
for i, node in enumerate(a_known_nodes):
    clone = dict(node_tree)
    clone['ip'] = node
    result[f"a_s1_{i}"] = clone

print(result)

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