With d
as your input, iterate through your input list with.
Since there are two elements in every sublists, hold the position and id in the iteration variable as p
and id
Example, you are processing list ['Director - Product Manager - Project Manager - Staff', 5090],
To get the title of each position, you can split your the position separated by -
and strip the leading and trailing white spaces. Eg.,
>>> d[3][0]
'Director - Product Manager - Project Manager - Staff'
>>> map(str.strip,d[3][0].split('-'))
['Director', 'Product Manager', 'Project Manager', 'Staff']
The output dict, along with the immediately previous position of Staff
is passed to the recursive search method and it fetches the all matches of the finding value, i.e. Project Manager
and returns a list. Fetch the last match.
>>> recursive_search([data,],'Product Manager')[-1]
{'children': [{'children': [{'id': 5090, 'title': 'Staff'}, {'id': 5087, 'title': 'Staff 2'}], 'id': 9080, 'title': 'Project Manager'}, {'id': 9099, 'title': 'Project Manager 2'}], 'id': 9894, 'title': 'Product Manager'}
You need to append the new id to the children
key of the above result!
Combining all the above,
d=[['Director', 9010],['Director - Product Manager', 9894],['Director - Product Manager - Project Manager', 9080],['Director - Product Manager - Project Manager - Staff', 5090],['Director - Product Manager - Project Manager - Staff 2', 5087],['Director - Product Manager - Project Manager 2', 9099],]
from pprint import pprint
def recursive_search(items, key):
found = []
for item in items:
if isinstance(item, list):
found += recursive_search(item, key)
elif isinstance(item, dict):
if key in item.values():
found.append(item)
found += recursive_search(item.values(), key)
return found
data={}
for p,id in d:
desig = map(str.strip,p.split('-'))
if len(desig)>1:
res = recursive_search([data,],desig[-2])[-1]
if res:
res['children']=res.get('children',[])
res['children'].append({'id':id,'title':desig[-1]})
else:
data = {'id':id,'title':p}
pprint.pprint(data)
Output:
{'children': [{'children': [{'children': [{'id': 5090, 'title': 'Staff'},
{'id': 5087,
'title': 'Staff 2'}],
'id': 9080,
'title': 'Project Manager'},
{'id': 9099, 'title': 'Project Manager 2'}],
'id': 9894,
'title': 'Product Manager'}],
'id': 9010,
'title': 'Director'}
Ref: The recursive_search function used here is the slightly modified version of searching through a dict mentioned here