Search for a value in a nested dictionary python - python

Search for a value and get the parent dictionary names (keys):
Dictionary = {dict1:{
'part1': {
'.wbxml': 'application/vnd.wap.wbxml',
'.rl': 'application/resource-lists+xml',
},
'part2':
{'.wsdl': 'application/wsdl+xml',
'.rs': 'application/rls-services+xml',
'.xop': 'application/xop+xml',
'.svg': 'image/svg+xml',
},
'part3':{...}, ...
dict2:{
'part1': { '.dotx': 'application/vnd.openxmlformats-..'
'.zaz': 'application/vnd.zzazz.deck+xml',
'.xer': 'application/patch-ops-error+xml',}
},
'part2':{...},
'part3':{...},...
},...
In above dictionary I need to search values like: "image/svg+xml". Where, none of the values are repeated in the dictionary. How to search the "image/svg+xml"? so that it should return the parent keys in a dictionary { dict1:"part2" }.
Please note: Solutions should work unmodified for both Python 2.7 and Python 3.3.

Here's a simple recursive version:
def getpath(nested_dict, value, prepath=()):
for k, v in nested_dict.items():
path = prepath + (k,)
if v == value: # found value
return path
elif hasattr(v, 'items'): # v is a dict
p = getpath(v, value, path) # recursive call
if p is not None:
return p
Example:
print(getpath(dictionary, 'image/svg+xml'))
# -> ('dict1', 'part2', '.svg')
To yield multiple paths (Python 3 only solution):
def find_paths(nested_dict, value, prepath=()):
for k, v in nested_dict.items():
path = prepath + (k,)
if v == value: # found value
yield path
elif hasattr(v, 'items'): # v is a dict
yield from find_paths(v, value, path)
print(*find_paths(dictionary, 'image/svg+xml'))

This is an iterative traversal of your nested dicts that additionally keeps track of all the keys leading up to a particular point. Therefore as soon as you find the correct value inside your dicts, you also already have the keys needed to get to that value.
The code below will run as-is if you put it in a .py file. The find_mime_type(...) function returns the sequence of keys that will get you from the original dictionary to the value you want. The demo() function shows how to use it.
d = {'dict1':
{'part1':
{'.wbxml': 'application/vnd.wap.wbxml',
'.rl': 'application/resource-lists+xml'},
'part2':
{'.wsdl': 'application/wsdl+xml',
'.rs': 'application/rls-services+xml',
'.xop': 'application/xop+xml',
'.svg': 'image/svg+xml'}},
'dict2':
{'part1':
{'.dotx': 'application/vnd.openxmlformats-..',
'.zaz': 'application/vnd.zzazz.deck+xml',
'.xer': 'application/patch-ops-error+xml'}}}
def demo():
mime_type = 'image/svg+xml'
try:
key_chain = find_mime_type(d, mime_type)
except KeyError:
print ('Could not find this mime type: {0}'.format(mime_type))
exit()
print ('Found {0} mime type here: {1}'.format(mime_type, key_chain))
nested = d
for key in key_chain:
nested = nested[key]
print ('Confirmation lookup: {0}'.format(nested))
def find_mime_type(d, mime_type):
reverse_linked_q = list()
reverse_linked_q.append((list(), d))
while reverse_linked_q:
this_key_chain, this_v = reverse_linked_q.pop()
# finish search if found the mime type
if this_v == mime_type:
return this_key_chain
# not found. keep searching
# queue dicts for checking / ignore anything that's not a dict
try:
items = this_v.items()
except AttributeError:
continue # this was not a nested dict. ignore it
for k, v in items:
reverse_linked_q.append((this_key_chain + [k], v))
# if we haven't returned by this point, we've exhausted all the contents
raise KeyError
if __name__ == '__main__':
demo()
Output:
Found image/svg+xml mime type here: ['dict1', 'part2', '.svg']
Confirmation lookup: image/svg+xml

Here is a solution that works for a complex data structure of nested lists and dicts
import pprint
def search(d, search_pattern, prev_datapoint_path=''):
output = []
current_datapoint = d
current_datapoint_path = prev_datapoint_path
if type(current_datapoint) is dict:
for dkey in current_datapoint:
if search_pattern in str(dkey):
c = current_datapoint_path
c+="['"+dkey+"']"
output.append(c)
c = current_datapoint_path
c+="['"+dkey+"']"
for i in search(current_datapoint[dkey], search_pattern, c):
output.append(i)
elif type(current_datapoint) is list:
for i in range(0, len(current_datapoint)):
if search_pattern in str(i):
c = current_datapoint_path
c += "[" + str(i) + "]"
output.append(i)
c = current_datapoint_path
c+="["+ str(i) +"]"
for i in search(current_datapoint[i], search_pattern, c):
output.append(i)
elif search_pattern in str(current_datapoint):
c = current_datapoint_path
output.append(c)
output = filter(None, output)
return list(output)
if __name__ == "__main__":
d = {'dict1':
{'part1':
{'.wbxml': 'application/vnd.wap.wbxml',
'.rl': 'application/resource-lists+xml'},
'part2':
{'.wsdl': 'application/wsdl+xml',
'.rs': 'application/rls-services+xml',
'.xop': 'application/xop+xml',
'.svg': 'image/svg+xml'}},
'dict2':
{'part1':
{'.dotx': 'application/vnd.openxmlformats-..',
'.zaz': 'application/vnd.zzazz.deck+xml',
'.xer': 'application/patch-ops-error+xml'}}}
d2 = {
"items":
{
"item":
[
{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters":
{
"batter":
[
{"id": "1001", "type": "Regular"},
{"id": "1002", "type": "Chocolate"},
{"id": "1003", "type": "Blueberry"},
{"id": "1004", "type": "Devil's Food"}
]
},
"topping":
[
{"id": "5001", "type": "None"},
{"id": "5002", "type": "Glazed"},
{"id": "5005", "type": "Sugar"},
{"id": "5007", "type": "Powdered Sugar"},
{"id": "5006", "type": "Chocolate with Sprinkles"},
{"id": "5003", "type": "Chocolate"},
{"id": "5004", "type": "Maple"}
]
},
...
]
}
}
pprint.pprint(search(d,'svg+xml','d'))
>> ["d['dict1']['part2']['.svg']"]
pprint.pprint(search(d2,'500','d2'))
>> ["d2['items']['item'][0]['topping'][0]['id']",
"d2['items']['item'][0]['topping'][1]['id']",
"d2['items']['item'][0]['topping'][2]['id']",
"d2['items']['item'][0]['topping'][3]['id']",
"d2['items']['item'][0]['topping'][4]['id']",
"d2['items']['item'][0]['topping'][5]['id']",
"d2['items']['item'][0]['topping'][6]['id']"]

Here are two similar quick and dirty ways of doing this type of operation. The function find_parent_dict1 uses list comprehension but if you are uncomfortable with that then find_parent_dict2 uses the infamous nested for loops.
Dictionary = {'dict1':{'part1':{'.wbxml':'1','.rl':'2'},'part2':{'.wbdl':'3','.rs':'4'}},'dict2':{'part3':{'.wbxml':'5','.rl':'6'},'part4':{'.wbdl':'1','.rs':'10'}}}
value = '3'
def find_parent_dict1(Dictionary):
for key1 in Dictionary.keys():
item = {key1:key2 for key2 in Dictionary[key1].keys() if value in Dictionary[key1][key2].values()}
if len(item)>0:
return item
find_parent_dict1(Dictionary)
def find_parent_dict2(Dictionary):
for key1 in Dictionary.keys():
for key2 in Dictionary[key1].keys():
if value in Dictionary[key1][key2].values():
print {key1:key2}
find_parent_dict2(Dictionary)

Traverses a nested dict looking for a particular value. When success is achieved the full key path to the value is printed. I left all the comments and print statements for pedagogical purposes (this isn't production code!)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 24 17:16:46 2022
#author: wellington
"""
class Tree(dict):
"""
allows autovivification as in Perl hashes
"""
def __missing__(self, key):
value = self[key] = type(self)()
return value
# tracking the key sequence when seeking the target
key_list = Tree()
# dict storing the target success result
success = Tree()
# example nested dict of dicts and lists
E = {
'AA':
{
'BB':
{'CC':
{
'DD':
{
'ZZ':'YY',
'WW':'PP'
},
'QQ':
{
'RR':'SS'
},
},
'II':
{
'JJ':'KK'
},
'LL':['MM', 'GG', 'TT']
}
}
}
def find_keys_from_value(data, target):
"""
recursive function -
given a value it returns all the keys in the path to that value within
the dict "data"
there are many paths and many false routes
at the end of a given path if success has not been achieved
the function discards keys to get back to the next possible path junction
"""
print(f"the number of keys in the local dict is {len(data)}")
key_counter = 0
for key in data:
key_counter += 1
# if target has been located stop iterating through keys
if success[target] == 1:
break
else:
# eliminate prior key from path that did not lead to success
if key_counter > 1:
k_list.pop()
# add key to new path
k_list.append(key)
print(f"printing k_list after append{k_list}")
# if target located set success[target] = 1 and exit
if key == target or data[key] == target:
key_list[target] = k_list
success[target] = 1
break
# if the target has not been located check to see if the value
# associated with the new key is a dict and if so return to the
# recursive function with the new dict as "data"
elif isinstance(data[key], dict):
print(f"\nvalue is dict\n {data[key]}")
find_keys_from_value(data[key], target)
# check to see if the value associated with the new key is a list
elif isinstance(data[key], list):
# print("\nv is list\n")
# search through the list
for i in data[key]:
# check to see if the list element is a dict
# and if so return to the recursive function with
# the new dict as "data
if isinstance(i, dict):
find_keys_from_value(i, target)
# check to see if each list element is the target
elif i == target:
print(f"list entry {i} is target")
success[target] = 1
key_list[target] = k_list
elif i != target:
print(f"list entry {i} is not target")
print(f"printing k_list before pop_b {k_list}")
print(f"popping off key_b {key}")
# so if value is not a key and not a list and not the target then
# discard the key from the key list
elif data[key] != target:
print(f"value {data[key]} is not target")
print(f"printing k_list before removing key_before {k_list}")
print(f"removing key_c {key}")
k_list.remove(key)
# select target values
values = ["PP", "SS", "KK", "TT"]
success = {}
for target in values:
print(f"\nlooking for target {target}")
success[target] = 0
k_list = []
find_keys_from_value(E, target)
print(f"\nprinting key_list for target {target}")
print(f"{key_list[target]}\n")
print("\n****************\n\n")

Related

2 Python nested Dictionaries, current data in one the other will fill with the data from the previous minute displays. I need to diff int data

I'm really new to Python so I hope this makes sense.
These are a sample of 2 dictionaries. What I cannot work out is how to subtract the current entries from the previous entries where the nested dictionary ie "Name2" matches in the previous dictionary.
Also I cannot introduce or use extra libraries.
previous = {}
c = []
c.append("Date:07Nov22,Name:Name1,Type:InterTerm,C_Time:12.45.09,C_001:2873,C_002:2832,P_002:98.5,C_003:41,P_003:1.4,C_005:1,P_005:0.0,C_010:2873,C_011:8,P_011:0.2,C_012:9,P_012:0.3")
c.append("Date:07Nov22,Name:Name2,Type:InterTerm,C_Time:12.45.09,C_001:18981,C_002:18683,P_002:98.4,C_003:298,P_003:1.5,C_005:47,P_005:0.2,C_010:18981,C_011:39,P_011:0.2,C_012:86,P_012:0.4")
c.append("Date:07Nov22,Name:Name1,Type:InterTerm,C_Time:12.49.09,C_001:3145,C_002:3102,P_002:98.6,C_003:43,P_003:1.3,C_005:1,P_005:0.0,C_007:1,P_007:0.0,C_010:3145,C_011:12,P_011:0.3,C_012:13,P_012:0.4")
c.append("Date:07Nov22,Name:Name2,Type:InterTerm,C_Time:12.49.09,C_001:20742,C_002:20415,P_002:98.4,C_003:327,P_003:1.5,C_005:54,P_005:0.2,C_007:1,P_007:0.0,C_010:20742,C_011:42,P_011:0.2,C_012:96,P_012:0.4")
c.append("Date:07Nov22,Name:Name1,Type:InterTerm,C_Time:12.52.30,C_001:3357,C_002:3310,P_002:98.5,C_003:47,P_003:1.4,C_005:2,P_005:0.0,C_007:2,P_007:0.0,C_010:3357,C_011:13,P_011:0.3,C_012:15,P_012:0.4")
c.append("Date:07Nov22,Name:Name2,Type:InterTerm,C_Time:12.52.30,C_001:22176,C_002:21823,P_002:98.4,C_003:353,P_003:1.5,C_005:58,P_005:0.2,C_007:1,P_007:0.0,C_010:22176,C_011:44,P_011:0.1,C_012:102,P_012:0.4")
def setCMD():
for cmd in c:
print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
print("$$$ $$$")
print("$$$ THIS IS THIS THE START $$$")
print("$$$ $$$")
print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
print("Using Command : ",cmd)
zw = dict(item.split(':') for item in cmd.split(','))
print("THIS IS ZW ",zw)
modForInter(zw)
def calcDiff(d1,d2):
print("In Function calDiff")
for x in d2:
for y in d1:
if x==y:
#print(d1[y],d2[x])
for i in d1[y]:
for i in d2[x]:
d1[y][i]=d1[y][i]-d2[x][i]
#print(d1[y][i])
break
return d1[y]
def modForInter(zw):
print("In Mod modForInter")
global previous
nameDict = {}
nameDict["Name"] = zw["Name"]
print("This is nameDict",nameDict)
zw1={ zw["Name"] }
print(zw1)
# List for extraction.
list1=["Date","Name","Type","C_Time",\
"C_001","","C_002","P_002","C_003","P_003","C_005","P_005",\
"C_007","P_007"]
# List for prev
list2=[
"C_001","C_002","C_003","C_005","C_007"]
current = {key:value for (key,value) in zw.items() if key in list1 }
zw = {k: int(v) if isinstance(v, str) and v.isdigit() else v for k, v in zw.items()}
current = {
key1 : {key:value for (key,value) in zw.items() if key in list1 } for key1 in zw1
}
print("current created")
result = {
key1 : {key:value for (key,value) in zw.items() if key in list1 } for key1 in zw1
}
number_of_elements = sum(len(v) for v in current.values())
if number_of_elements <= 4:
print("Number of elements is less than 4")
return
else:
print("Number of elements is greater than 4")
y=(str(zw1).replace("{'","").replace("'}",""))
print(y)
if "C_001" in previous[y]:
print("previous is NOT empty")
print("This is previous_dict :",previous)
x=calcDiff(current,previous)
print("This is X",x)
else:
print("previous_dict is empty and will be populated")
previous.update({
key1 : {key:value for (key,value) in zw.items() if key in list2 } for key1 in zw1
})
print("THIS IS PREVIOUS_DICT : ",previous)
del(current)
del(zw)
print("THIS IS RESULT",result)
setCMD()
Notice that C_007 was added and was not in the previous dictionary and Name2 is gone also.
The calcDiff() function seems to be incorrect. I tried running your code but couldn't get your expected output. Hence I modified your code as follows:
current = {'Name2': {'Date': '07Nov2022', 'Name': 'Name2', 'Type': 'stats', 'Time': '12.49.09', 'C_001': 20742, 'C_002': 20415, 'P_002': '98.4', 'C_003': 327, 'P_003': '1.5', 'C_005': 54, 'P_005': '0.2', 'C_007': 1, 'P_007': '0.0'}}
previous = {'Name1': {'C_001': 3145, 'C_002': 3102, 'C_003': 43, 'C_005': 1, 'C_007': 1}, 'Name2': {'C_001': 18981, 'C_002': 18683, 'C_003': 298, 'C_005': 47}}
def calcDiff(d1,d2):
print("In Function calDiff")
for x in d2:
for y in d1:
if x==y:
#print(d1[y],d2[x])
for i in d1[y]:
for i in d2[x]:
d1[y][i]=d1[y][i]-d2[x][i]
#print(d1[y][i])
break
return d1[y]
if "C_001" in previous['Name2']:
print("previous is NOT empty")
print("This is previous_dict :",previous)
x=calcDiff(current,previous)
print(x)
I was able to get the result without creating another directory.
{'Date': '07Nov2022', 'Name': 'Name2', 'Type': 'stats', 'Time': '12.49.09', 'C_001': 1761, 'C_002': 1732, 'P_002': '98.4', 'C_003': 29, 'P_003': '1.5', 'C_005': 7, 'P_005': '0.2', 'C_007': 1, 'P_007': '0.0'}
Output:
With the previous answer I was able to complete it.
previous = {}
c = []
c.append("Date:07Nov22,Name:Name1,Type:InterTerm,C_Time:12.45.09,C_001:2873,C_002:2832,P_002:98.5,C_003:41,P_003:1.4,C_005:1,P_005:0.0,C_010:2873,C_011:8,P_011:0.2,C_012:9,P_012:0.3")
c.append("Date:07Nov22,Name:Name2,Type:InterTerm,C_Time:12.45.09,C_001:18981,C_002:18683,P_002:98.4,C_003:298,P_003:1.5,C_005:47,P_005:0.2,C_010:18981,C_011:39,P_011:0.2,C_012:86,P_012:0.4")
c.append("Date:07Nov22,Name:Name1,Type:InterTerm,C_Time:12.49.09,C_001:3145,C_002:3102,P_002:98.6,C_003:43,P_003:1.3,C_005:1,P_005:0.0,C_007:1,P_007:0.0,C_010:3145,C_011:12,P_011:0.3,C_012:13,P_012:0.4")
c.append("Date:07Nov22,Name:Name2,Type:InterTerm,C_Time:12.49.09,C_001:20742,C_002:20415,P_002:98.4,C_003:327,P_003:1.5,C_005:54,P_005:0.2,C_007:1,P_007:0.0,C_010:20742,C_011:42,P_011:0.2,C_012:96,P_012:0.4")
c.append("Date:07Nov22,Name:Name1,Type:InterTerm,C_Time:12.52.30,C_001:3357,C_002:3310,P_002:98.5,C_003:47,P_003:1.4,C_005:2,P_005:0.0,C_007:2,P_007:0.0,C_010:3357,C_011:13,P_011:0.3,C_012:15,P_012:0.4")
c.append("Date:07Nov22,Name:Name2,Type:InterTerm,C_Time:12.52.30,C_001:22176,C_002:21823,P_002:98.4,C_003:353,P_003:1.5,C_005:58,P_005:0.2,C_007:1,P_007:0.0,C_010:22176,C_011:44,P_011:0.1,C_012:102,P_012:0.4")
def setCMD():
for cmd in c:
print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
print("$$$ $$$")
print("$$$ THIS IS THIS THE START $$$")
print("$$$ $$$")
print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
zw = dict(item.split(':') for item in cmd.split(','))
modForInter(zw)
def calcDiff(d1,d2):
print("In Function calDiff")
for x in d2:
for y in d1:
if x==y:
#print(d1[y],d2[x])
for i in d1[y]:
for i in d2[x]:
d1[y][i]=d1[y][i]-d2[x][i]
#print(d1[y][i])
break
return d1[y]
def modForInter(zw):
print("In Mod modForInter")
global previous
nameDict = {}
nameDict["Name"] = zw["Name"]
zw1={ zw["Name"] }
# List for extraction.
list1=["Date","Name","Type","C_Time",\
"C_001","","C_002","P_002","C_003","P_003","C_005","P_005",\
"C_007","P_007"]
# List for prev
list2=[
"C_001","C_002","C_003","C_005","C_007"]
zw = {k: int(v) if isinstance(v, str) and v.isdigit() else v for k, v in zw.items()}
current = {key1 : {key:value for (key,value) in zw.items() if key in list1 } for key1 in zw1 }
result = {}
number_of_elements = sum(len(v) for v in current.values())
if number_of_elements <= 4:
print("Number of elements is less than 4")
return
else:
print("Number of elements is greater than 4")
y=(str(zw1).replace("{'","").replace("'}",""))
print(y)
try:
YES = previous[y]['C_001']
result=calcDiff(current,previous)
except KeyError:
print('The specified key does NOT exist')
previous.update({key1 : {key:value for (key,value) in zw.items() if key in list2 } for key1 in zw1 })
del(current)
del(zw)
print("THIS IS RESULT",result)
setCMD()

Nested and escaped JSON payload to flattened dictionary - python

I'm looking for any suggestions to resolve an issue I'm facing. It might seem as a simple problem, but after a few days trying to find an answer - I think it is not anymore.
I'm receiving data (StringType) in a following JSON-like format, and there is a requirement to turn it into flat key-value pair dictionary. Here is a payload sample:
s = """{"status": "active", "name": "{\"first\": \"John\", \"last\": \"Smith\"}", "street_address": "100 \"Y\" Street"}"""
and the desired output should look like this:
{'status': 'active', 'name_first': 'John', 'name_last': 'Smith', 'street_address': '100 "Y" Street'}
The issue is I can't find a way to turn original string (s) into a dictionary. If I can achieve that the flattening part is working perfectly fine.
import json
import collections
import ast
#############################################################
# Flatten complex structure into a flat dictionary
#############################################################
def flatten_dictionary(dictionary, parent_key=False, separator='_', value_to_str=True):
"""
Turn a nested complex json into a flattened dictionary
:param dictionary: The dictionary to flatten
:param parent_key: The string to prepend to dictionary's keys
:param separator: The string used to separate flattened keys
:param value_to_str: Force all returned values to string type
:return: A flattened dictionary
"""
items = []
for key, value in dictionary.items():
new_key = str(parent_key) + separator + key if parent_key else key
try:
value = json.loads(value)
except BaseException:
value = value
if isinstance(value, collections.MutableMapping):
if not value.items():
items.append((new_key,None))
else:
items.extend(flatten_dictionary(value, new_key, separator).items())
elif isinstance(value, list):
if len(value):
for k, v in enumerate(value):
items.extend(flatten_dictionary({str(k): (str(v) if value_to_str else v)}, new_key).items())
else:
items.append((new_key,None))
else:
items.append((new_key, (str(value) if value_to_str else value)))
return dict(items)
# Data sample; sting and dictionary
s = """{"status": "active", "name": "{\"first\": \"John\", \"last\": \"Smith\"}", "street_address": "100 \"Y\" Street"}"""
d = {"status": "active", "name": "{\"first\": \"John\", \"last\": \"Smith\"}", "street_address": "100 \"Y\" Street"}
# Works for dictionary type
print(flatten_dictionary(d))
# Doesn't work for string type, for any of the below methods
e = eval(s)
# a = ast.literal_eval(s)
# j = json.loads(s)
Try:
import json
import re
def jsonify(s):
s = s.replace('"{','{').replace('}"','}')
s = re.sub(r'street_address":\s+"(.+)"(.+)"(.+)"', r'street_address": "\1\2\3"',s)
return json.loads(s)
If you must keep the quotes around Y, try:
def jsonify(s):
s = s.replace('"{','{').replace('}"','}')
search = re.search(r'street_address":\s+"(.+)"(.+)"(.+)"',s)
if search:
s = re.sub(r'street_address":\s+"(.+)"(.+)"(.+)"', r'street_address": "\1\2\3"',s)
dict_version = json.loads(s)
dict_version['street_address'] = dict_version['street_address'].replace(search.group(2),'"'+search.group(2)+'"')
return dict_version
A more generalized attempt:
def jsonify(s):
pattern = r'(?<=[,}])\s*"(.[^\{\}:,]+?)":\s+"([^\{\}:,]+?)"([^\{\}:,]+?)"([^\{\}:,]+?)"([,\}])'
s = s.replace('"{','{').replace('}"','}')
search = re.search(pattern,s)
matches = []
if search:
matches = re.findall(pattern,s)
s = re.sub(pattern, r'"\1": "\2\3\4"\5',s)
dict_version = json.loads(s)
for match in matches:
dict_version[match[0]] = dict_version[match[0]].replace(match[2],'"'+match[2]+'"')
return dict_version

Extract specific dictionaries from a list of nested dictionaries to a temp list

I have been struggling to extract sub dictionaries where the value is "0" from a list of dictionaries and add them to temporary dictionaries.
I tried this:
new_users = [{'user1':{'book1':'0', 'book2':'4', 'book3':'1'}},{'user2':{'book1':'5', 'book2':'1', 'book3':'0'}}]
def approachA():
for data in new_users: # new_users is a list of nested dictionaries
if data == '0':
print("found 0")
keys = data.keys()
for key in keys:
if key == '0':
key.pop() # tried to deleted delete the elements at first
It did not work for some reason, and I have been trying to do it for 2 hours so please do not ask questions not related to the problem.
This is a simple version of what I am trying to do:
[{'user1':{'book1':'0', 'book2':'4', 'book3':'1'}},{'user2':{'book1':'5', 'book2':'1', 'book3':'0'}}] -> [{'user1':{'book1':'0'}}, {'user2':{'book3':'0'}}]
So basically the keys with value "0" get copied to a temp list of dictionaries.
As I mentioned in a comment I'm not sure this will solve your problem, but based on the sample transformation you gave, here's one way to achieve it:
LIST_IN = [{"name": {"book1": "0", "book2": "1", "book3": "0"}}]
def proccess(list_in):
result = []
for this_dict in list_in:
for key in this_dict["name"]:
if this_dict["name"][key] == "0":
result.append({key: this_dict["name"][key]})
return result
print(proccess(LIST_IN))
This should accomplish the "sample" snippet. Please give some additional details if this is not what you were trying.
def solve_nested(nested_dict: list) -> list:
result = list()
for dictionary in nested_dict:
for k, v in dictionary.items():
for _k, _v in v.items():
if _v == '0':
result.append({_k: _v})
return result
if __name__ == '__main__':
print(solve_nested([{"name": {"book1": "0", "book2": "1", "book3": "0"}}]))
If you're interested in recursion, the below example ought to get you going:
# d_xy => x = nest level
# y = dictionary item in nest level x
d = {'d_00' : {'d_10' : {'d_20' : 0,
'd_21' : 43,
'd_22' : 12},
'd_11' : 4,
'd_12' : 0,
'd_13' : 1},
'd_01' : 0,
'd_02' : {'d_14' : {'d_23' : 0,
'd_24' : 1,
'd_25' : 0},
'd_15' : 4,
'd_16' : {'d_26' : 0,
'd_27' : {'d_30' : 3,
'd_31' : 0,
'd_32' : {'d_40' : 0},
'd_33' : 0},
'd_28' : 1},
'd_17' : 0}}
important_items = []
def get_0_key_values(dic):
for key in dic:
if type(dic[key]) == dict:
get_0_key_values(dic[key])
else:
if dic[key] == 0:
important_items.append({key : dic[key]})
get_0_key_values(d)
print(important_items)

Overriding a value in a iteritems loop

I've created a function for iterating through a multiple level dictionary, and execute a second function ssocr that needs four arguments: coord, background, foreground, type (they are the value of my keys). This is my dictionary which is taken from a json file.
document json
def parse_image(self, d):
bg = d['background']
fg = d['foreground']
results = {}
for k, v in d['boxes'].iteritems():
if 'foreground' in d['boxes']:
myfg = d['boxes']['foreground']
else:
myfg = fg
if k != 'players_home' and k != 'players_opponent':
results[k] = MyAgonism.ssocr(v['coord'], bg, myfg, v['type'])
results['players_home'] = {}
for k, v in d['boxes']['players_home'].iteritems():
if 'foreground' in d['boxes']['players_home']:
myfg = d['boxes']['players_home']['foreground']
else:
myfg = fg
if k != 'background' and k != 'foreground':
for k2, v2 in d['boxes']['players_home'][k].iteritems():
if k2 != 'fouls':
results['players_home'][k] = {}
results['players_home'][k][k2] = MyAgonism.ssocr(v2['coord'], bg, myfg, v2['type'])
return results
In the last iteritems I get the correct value just for the name key. The score key doesn't appear. It looks like name override score in my results['players_home'] dictionary
output: ... "player4": {"name": 9}, "player5": {"name": 24} ...
I would like something like ... "player4": {"name": 9, "score": value}, "player5": {"name": 24, "score": value} ...
What am I doing wrong? Here is the full code just in case: Full Code
This might be the/a problem:
if k2 != 'fouls':
results['players_home'][k] = {}
In your loop, each time that k2 is not 'fouls', you create a new empty dict and store that in results['players_home']. That means that any entry previously stored there is no longer accessible.

Map path list to files structure

I have a list of paths and contents similar to that:
paths = [
("/test/file1.txt", "content1"),
("/test/file2.txt", "content2"),
("/file3.txt", "content3"),
("/test1/test2/test3/file5.txt", "content5"),
("/test2/file4.txt", "content4")
]
I would transform this path list to:
structure = {
"file3.txt": "content3"
"test": {
"file1.txt": "content1",
"file2.txt": "content2"
},
"test2": {
"file4.txt": "content4"
}
}
Is there any simple solution to that problem ?
Since the file path can be of an arbitrary depth, we need something scalable.
Here is a recursive approach - splitting the path recursively until we get to the root /:
import os
paths = [
("/test/file1.txt", "content1"),
("/test/file2.txt", "content2"),
("/file3.txt", "content3"),
("/test1/test2/test3/file5.txt", "content5"),
("/test2/file4.txt", "content4")
]
def deepupdate(original, update):
for key, value in original.items():
if key not in update:
update[key] = value
elif isinstance(value, dict):
deepupdate(value, update[key])
return update
def traverse(key, value):
directory = os.path.dirname(key)
filename = os.path.basename(key)
if directory == "/":
return value if isinstance(value, dict) else {filename: value}
else:
path, directory = os.path.split(directory)
return traverse(path, {directory: {filename: value}})
result = {}
for key, value in paths:
result = deepupdate(result, traverse(key, value))
print(result)
Using deepupdate() function suggested here.
It prints:
{'file3.txt': 'content3',
'test': {'file1.txt': 'content1', 'file2.txt': 'content2'},
'test1': {'test2': {'test3': {'file5.txt': 'content5'}}},
'test2': {'file4.txt': 'content4'}}
I think .setdefault() be ok:
paths = [
("/test/file1.txt", "content1"),
("/test/file2.txt", "content2"),
("/file3.txt", "content3"),
("/test2/file4.txt", "content4")
]
dirs = {}
for p in paths:
current = dirs
ps = p[0].split('/')
for d in ps[:-1]:
if d:
current = current.setdefault(d, {})
current[ps[-1]] = p[1]
print(dirs)
Try using recursivity:
paths = [
("/test/file1.txt", "content1"),
("/test/file2.txt", "content2"),
("/file3.txt", "content3"),
("/test2/file4.txt", "content4"),
('/test1/test2/test3/file.txt', 'content'),
('/test10/test20/test30/test40/file.txt', 'content100')
]
def create_structure(elems,count,mylen,p_1,var):
if mylen<=2:
var[elems[count]] = p_1
return
create_structure(elems,count+1,mylen-1,p_1,var.setdefault(elems[count],{}))
structure = {}
for p in paths:
elems = p[0].split('/')
create_structure(elems,1,len(elems),p[1],structure)
print structure

Categories

Resources