I have 4 sets of dictionaries and 3 sets of Lists. Based on this, I want to create a nested Dictionary(target_result).
cname = {"file":"cn_file" ,"accounts":"cn_accounts" ,"inventory":"cn_inventory" ,"payroll":"cn_PayRoll"}
english = {"file":"File" ,"accounts":"Accounts" ,"inventory":"Inventory" ,"payroll":"PayRoll"}
tamil = {"file":"Koppu" ,"accounts":"Kanakiyal" ,"inventory":"Saragu" ,"payroll":"Sambalam"}
hindi = {"file":"Hindi_file","accounts":"Hindi_accounts","inventory":"Hindi_inventory","payroll":"Hindi_payroll"}
dict_item_mainkeys =["file","accounts","inventory","payroll"]
dict_item_subkeys =["cname","item1","item2"]
language_selection =["English","Tamil"]
target_result = {"file" :{"cname":"","item1":"","item2":""},
"accounts" :{"cname":"","item1":"","item2":""},
"inventory":{"cname":"","item1":"","item2":""},
"payroll" :{"cname":"","item1":"","item2":""}
}
In my case, My first Language_selection is English and the second one is "Tamil" Based on language selection, the item1 is updated with the first language option(English dictionary) and item2 is updated with the second language option(Tamil dictionary) and the name is updated with dictionary "cname". For Example :
target_result = {"file" :{"cname":"cn_file" ,"item1":"File" ,"item2":"Koppu"},
"accounts" :{"cname":"cn_accounts" ,"item1":"Accounts" ,"item2":"Kanakiyal"},
"inventory":{"cname":"cn_inventory","item1":"Inventory","item2":"Saragu"},
"payroll" :{"cname":"cn_apyroll" ,"item1":"PayRoll" ,"item2":"Sambalam"}
}
Language selection differs from user to user.
CodePudding user response:
This is difficult because you have named variables. It would be much easier if you used a dictionary instead e.g.
languages = {
"cname" : {"file":"cn_file" ,"accounts":"cn_accounts" ,"inventory":"cn_inventory" ,"payroll":"cn_PayRoll"},
"English" : {"file":"File" ,"accounts":"Accounts" ,"inventory":"Inventory" ,"payroll":"PayRoll"},
"Tamil" : {"file":"Koppu" ,"accounts":"Kanakiyal" ,"inventory":"Saragu" ,"payroll":"Sambalam"},
"Hindi" : {"file":"Hindi_file","accounts":"Hindi_accounts","inventory":"Hindi_inventory","payroll":"Hindi_payroll"}
}
Then you can reasonably easily loop over your two key lists:
dict_item_mainkeys =["file","accounts","inventory","payroll"]
dict_item_subkeys =["cname","item1","item2"]
language_selection =["English","Tamil"]
target_result = {}
for mk in dict_item_mainkeys:
target_result[mk] = {}
for i, sk in enumerate(dict_item_subkeys):
if sk in languages:
target_result[mk][sk] = languages[sk][mk]
else:
target_result[mk][sk] = languages[language_selection[i-1]][mk]
Output:
{
'file': {'cname': 'cn_file', 'item1': 'File', 'item2': 'Koppu'},
'accounts': {'cname': 'cn_accounts', 'item1': 'Accounts', 'item2': 'Kanakiyal'},
'inventory': {'cname': 'cn_inventory', 'item1': 'Inventory', 'item2': 'Saragu'},
'payroll': {'cname': 'cn_PayRoll', 'item1': 'PayRoll', 'item2': 'Sambalam'}
}
CodePudding user response:
I would start out by putting the main info in a single dictionary, such that it is easy to look up values using string keys:
initial_data = {
"cname": {
"file": "cn_file",
"accounts": "cn_accounts",
"inventory": "cn_inventory",
"payroll": "cn_PayRoll",
},
"english": {
"file": "File",
"accounts": "Accounts",
"inventory": "Inventory",
"payroll": "PayRoll",
},
"tamil": {
"file": "Koppu",
"accounts": "Kanakiyal",
"inventory": "Saragu",
"payroll": "Sambalam",
},
"hindi": {
"file": "Hindi_file",
"accounts": "Hindi_accounts",
"inventory": "Hindi_inventory",
"payroll": "Hindi_payroll",
},
}
To line up the dict_item_subkeys
with the language_selection
you can use a zip, but make sure to offset this after handling the cname value.
dict_item_mainkeys = ["file", "accounts", "inventory", "payroll"]
dict_item_subkeys = ["cname", "item1", "item2"]
language_selection = ["English", "Tamil"]
target_result = {}
for mainkey in dict_item_mainkeys:
target_result[mainkey] = {}
target_result[mainkey]["cname"] = initial_data["cname"][mainkey]
for subkey, language in zip(dict_item_subkeys[1:], language_selection):
target_result[mainkey][subkey] = initial_data[language.lower()][mainkey]
pprint(target_result)
{'accounts': {'cname': 'cn_accounts',
'item1': 'Accounts',
'item2': 'Kanakiyal'},
'file': {'cname': 'cn_file', 'item1': 'File', 'item2': 'Koppu'},
'inventory': {'cname': 'cn_inventory',
'item1': 'Inventory',
'item2': 'Saragu'},
'payroll': {'cname': 'cn_PayRoll', 'item1': 'PayRoll', 'item2': 'Sambalam'}}