I have a input request which consists of multiple dictionaries with similar keys and values as dictionaries.
Here's the input I have.
req = {"main1":
{"x": {"a":220},"y": {"b":66}},
"main2":
{"x": {"c":"1000","d":"copper"},
"y": {"c":"1200","d":"Copper"}}}
Output I need:
{'cable1': {'a': 220, 'c': '1000', 'd': 'copper'}, 'cable2': {'b': 66, 'c': '1200', 'd': 'Copper'}}
Here's the try i made:
actual_req = []
for attr1, attr2 in req.items():
for j, k in attr2.items():
actual_req.append(k)
actual_req[0].update(actual_req[2])
actual_req[1].update(actual_req[3])
data = {'cable1': actual_req[0], 'cable2': actual_req[1]}
print(data)
I just need the more generic way of writing in short. It should also handle input with
req = {"main1":
{"x": {"a":220},"y": {"b":66}},
"main2":
{"x": {"c":"1000","d":"cooper"},
"y": {"c":"1200","d":"Copper"}},
"main3":
{"x": {"e":20},"y": {"f":6}}}
Just update final dictionary as you go through values of original dictionary:
req = {"main1":
{"x": {"a":220},"y": {"b":66}},
"main2":
{"x": {"c":"1000","d":"copper"},
"y": {"c":"1200","d":"Copper"}}}
final_dict = {'cable1': {}, 'cable2': {}}
for v in req.values():
final_dict['cable1'].update(v['x'])
final_dict['cable2'].update(v['y'])
Final dict would look like this:
{'cable1': {'a': 220, 'c': '1000', 'd': 'copper'}, 'cable2': {'b': 66, 'c': '1200', 'd': 'Copper'}}
Related
Lets say I have this dict:
dictos = {
"a": {
"b": {
"c": 3,
"d": 4,
"e": [],
"f": [{"g": 5}, 'test', {"h": 6, "i": 7}]
}
}
}
And lets say I want to delete "c": 3 pair. What I am doing:
import dpath.util
path = "a/b/c"
dpath.util.delete(dictos, path)
It is working great. The output is:
{
'a': {
'b': {
'd': 4,
'e': [],
'f': [{'g': 5}, 'test', {'h': 6, 'i': 7}]
}
}
}
The problem is when I am trying to delete key:value pair inside the list.
Lets say I want to delete "h":6. So when doing:
path = "a/b/f[2]/h"
dpath.util.delete(dictos, path)
I am getting:
dpath.exceptions.PathNotFound: Could not find a/b/f[2]/h to delete
it.
So the question basically is how to delete items from nested dicts that are in a list?
It seems the library expects the same separator to be used for all segments i.e. use a/b/f/2/h
path = "a/b/f/2/h"
dpath.util.delete(dictos, path)
print(dictos)
Result:
{'a': {'b': {'d': 4, 'e': [], 'f': [{'g': 5}, 'test', {'i': 7}]}}}
I have a nested dictionary that looks like below,
{
'product_list.show_date': "May '21",
'product_list.new_users':
{
'product_list.product':
{
'A': None,
'B': 377,
'C': None,
'D': 67,
'E': None,
'F': 1,
'G': None
}
}
}
And I want to clear it out in a way that parent keys are not there. So basically, I want a dictionary that is not nested. Like below,
{
'product_list.show_date': "May '21",
'A': None,
'B': 377,
'C': None,
'D': 67,
'E': None,
'F': 1,
'G': None
}
I am using the recursive function to do this, but it's not 100% correct.
Here's my code,
def clear_nd(d, nested_dict):
for key in nested_dict:
if type(nested_dict[key]) != dict:
d[key] = nested_dict[key]
elif type(nested_dict[key]) == dict:
nested_dict = nested_dict[key]
clear_nd(d, nested_dict)
return d
d = {}
clear_nd(d, nested_dict)
For below example,
nested_dict = {
'product_list.show_date': "May '21",
'product_list.new_users': {
'product_list.product': {
'A': None,
'B': 377,
'C': None,
'D': 67,
'E': None,
'F': 1,
'G': None
},
'prod.product': {
'Alk': None,
'Bay': 377,
'Lent': None,
'R': 67,
'Ter': None,
'Wi': 1,
'e': None
}
},
'duct_list.new_users': {
'pdust.product': {
'H': None,
'y': 377,
'nt': None,
'C': 67,
'sfer': None,
's': 1,
'le': None
}
}
}
Does Pandas or any other library has a way to do this. Structure of the nested dictionary is dynamic so we won't know how deep it is. And Keys will also change, so we won't able to know beforehand what are the keys in the dictionary. Any help will be appreciated. Thanks!!
If you allow the lower level tag labels to take prefixes of higher level tag labels, you can use the Pandas function pandas.json_normalize, which handles nested dict and turn it into a flat table Pandas dataframe.
Then, use pandas.DataFrame.to_dict to turn the Pandas dataframe to a dict. For example,
import pandas as pd
d = {
'product_list.show_date': "May '21",
'product_list.new_users':
{
'product_list.product':
{
'A': None,
'B': 377,
'C': None,
'D': 67,
'E': None,
'F': 1,
'G': None
}
}
}
pd.json_normalize(d).to_dict('records')[0]
Result:
{'product_list.show_date': "May '21",
'product_list.new_users.product_list.product.A': None,
'product_list.new_users.product_list.product.B': 377,
'product_list.new_users.product_list.product.C': None,
'product_list.new_users.product_list.product.D': 67,
'product_list.new_users.product_list.product.E': None,
'product_list.new_users.product_list.product.F': 1,
'product_list.new_users.product_list.product.G': None}
I have a list of dictionary as follows.
mylist = [ {"0": ["code1", "code5"], "1" ["code8", "code7", "code2"]},
{"1": ["code2", "code3"], "2" ["code4", "code5", "code7"], "3": ["code1", "code10"]},
{"0": ["code8", "code5", "code1"], "2" ["code7", "code5", "code2"]} ]
Now, I want to calculate the codes count for each key in the dictionary. For example "0": ["code1", "code5"] and "0": ["code8", "code5"] should give: mydict_for_0 = {"code1": 1, "code5": 2, "code8": 1}
So, for the above mylist the output should be;
mydict_for_0 = {"code1": 2, "code5": 2, "code8": 1}
mydict_for_1 = {"code2": 2, "code3": 1, "code7": 1, "code8": 1}
mydict_for_2 = {"code4": 1, "code5": 2, "code7": 2, {"code2": 1}
mydict_for_3 = {"code1": 1, "code10": 1}
Please help me to do this using python!
Try with defaultdict, Counter from collections module, find all same key's value list, extend them into one list, save into a defaultdict(list):
from collections import defaultdict, Counter
new_dict = defaultdict(list)
for e in mylist:
for key,value in e.items():
new_dict[key].extend(value)
new_dict will be:
defaultdict(list,
{'0': ['code1', 'code5', 'code8', 'code5', 'code1'],
'1': ['code8', 'code7', 'code2', 'code2', 'code3'],
'2': ['code4', 'code5', 'code7', 'code7', 'code5', 'code2'],
'3': ['code1', 'code10']})
After that, loop all items to pass the values list into Counter, to count the occurrences of list:
result = {}
for key,value in new_dict.items():
result['mydict_for_'+key] = dict(Counter(value))
result will be:
{'mydict_for_0': {'code1': 2, 'code5': 2, 'code8': 1},
'mydict_for_1': {'code2': 2, 'code3': 1, 'code7': 1, 'code8': 1},
'mydict_for_2': {'code2': 1, 'code4': 1, 'code5': 2, 'code7': 2},
'mydict_for_3': {'code1': 1, 'code10': 1}}
This might be the solution
final_result = []
for i in mylist:
current_list = mylist[i]
d = {}
for key in current_list:
try:
d[m]+=1
except KeyError as e:
d.update({m: 1})
final_result.append(d)
for i in final_result:
print(i)
I have a list Final_Bioteck whose first 5 enteries looks like this:
['PBYI', 'DRRX', 'FBIO', 'CAPR', 'KDMN']
I'm trying to create a nested dictionary with the first layer being the letters of the alphabet and the second layer being an element from the above list.
I'm trying to create an indexed dictionary.
I attempted to do so with the code below:
from string import ascii_uppercase
Bioteck_dict ={}
for letter in ascii_uppercase:
for stock in Final_Bioteck:
if stock.startswith(letter):
try:
Bioteck_dict[letter].update(stock)
except KeyError:
Bioteck_dict[letter] = stock
However, I'm getting the following error:
'str' object has no attribute 'update'
Desired output is something like this:
Bioteck_dict
{A:
B:
C: 'CAPR':{}
D: 'DRRX':{}
E:
F:
or even this:
{A:
B:
C: 'CAPR'
D: 'DRRX'
E:
F: 'FBIO':
}
.update() is a method for dicts, and you're calling it on a string. This is a way of doing what you appear to want:
Final_Bioteck = ['PBYI', 'DRRX', 'FBIO', 'CAPR', 'KDMN']
Bioteck_dict = {}
for stock in Final_Bioteck:
Bioteck_dict.setdefault(stock[0], {})[stock] = {}
Following the update to your question, it seems like you want letters that have no associated stocks to nonetheless be explicitly represented in the dict as empty containers. It's questionable whether you really need that (most things you would subsequently want to do will involve just iterating over what is in the dict) but you can get that effect in one of two ways:
EITHER:
Final_Bioteck = ['PBYI', 'DRRX', 'FBIO', 'CAPR', 'KDMN']
from collections import defaultdict
Bioteck_dict = defaultdict(dict) # now you can look up any letter (or any key at all) and receive the empty dict, even if there was no entry there before
for stock in Final_Bioteck:
Bioteck_dict[stock[0]][stock] = {}
OR:
Final_Bioteck = ['PBYI', 'DRRX', 'FBIO', 'CAPR', 'KDMN']
Bioteck_dict = {letter:{} for letter in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'}
for stock in Final_Bioteck:
Bioteck_dict[stock[0]][stock] = {}
The problem is that you're assigning each dict value as a string, not a dict. It needs to start off as a dict so you can update it. 'FBIO'.update(blah) makes no sense. Additionally, you probably want a dict of lists, rather than a dict of dicts—what would the key be, after all?
from collections import defaultdict
Bioteck_dict = defaultdict(list) # Or `set`, if unique and unordered.
for stock in Final_Bioteck:
Bioteck_dict[stock[0]].append(stock)
The result is:
{'C': ['CAPR'], 'D': ['DRRX'], 'F': ['FBIO'], 'K': ['KDMN'], 'P': ['PBYI']}
This one is a bit more akin to your logic:
from string import ascii_uppercase
stocks = ['PBYI', 'DRRX', 'FBIO', 'CAPR', 'KDMN']
d = {}
for letter in ascii_uppercase:
d[letter] = {}
for stock in stocks:
if stock.startswith(letter):
d[letter][stock] = {}
print d
That returns:
{'A': {}, 'C': {'CAPR': {}}, 'B': {}, 'E': {}, 'D': {'DRRX': {}}, 'G': {}, 'F': {'FBIO': {}}, 'I': {}, 'H': {}, 'K': {'KDMN': {}}, 'J': {}, 'M': {}, 'L': {}, 'O': {}, 'N': {}, 'Q': {}, 'P': {'PBYI': {}}, 'S': {}, 'R': {}, 'U': {}, 'T': {}, 'W': {}, 'V': {}, 'Y': {}, 'X': {}, 'Z': {}}
Using dict comprehension, if unique records.
l = ['PBYI', 'DRRX', 'FBIO', 'CAPR', 'KDMN']
d = { i[0]:i for i in l}
pprint(d)
Output:
{'C': 'CAPR', 'D': 'DRRX', 'F': 'FBIO', 'K': 'KDMN', 'P': 'PBYI'}
Use defaultdict if multiple records.
l = ['PBYI', 'DRRX', 'FBIO', 'CAPR', 'KDMN', 'DUMMY','CAT','COLD']
from collections import defaultdict
d = defaultdict(list)
for i in l:
d[i[0]].append(i)
pprint(d)
Output:
defaultdict(<class 'list'>,
{'C': ['CAPR', 'CAT', 'COLD'],
'D': ['DRRX', 'DUMMY'],
'F': ['FBIO'],
'K': ['KDMN'],
'P': ['PBYI']})
heres my dict:
{
1: { 'A': u'Eggs',
'B': 1400,
'C': u'Jibber',
'D': u'355'},
2: { 'A': u'Avocados',
'B': 1000,
'C': u'Jabber',
'D': u'356'},
..}
Template.mustache
{{#each}}
<li><strong>{{A}}</strong></li>
<li><strong>{{B}}</strong></li>
...
{{/each}}
{{#empty}}
<p>The list is empty.</p>
{{/empty}}
If the variable is called data, how do I iterate over its items if they are only identifiable by index?