I have a json file that looks something like the following:
[
{
"category1":"0120391123123"
},
[
{
"subcategory":"0120391123123"
},
[
{
"subsubcategory":"019301948109"
},
[
{
"subsubsubcategory":"013904123908"
},
[
{
"subsubsubsubcategory":"019341823908"
}
]
]
]
],
[
{
"subcategory2":"0934810923801"
},
[
{
"subsubcategory2":"09341829308123"
}
]
],
[
{
"category2":"1309183912309"
},
[
{
"subcategory":"10293182094"
}
]
]
]
I also have a list of categories that I would like to find in the original list. If the category exists in categoriesToFind, I would also like to find all subcategories and return those as well.
categoriesToFind = ['019301948109', '1309183912309']
finalCategories = []
def findCategories(currentList, isFirstIteration):
for x in currentList:
if type(x) is dict and (next(iter(x.values())) in categoriesToFind or not isFirstIteration):
finalCategories.append(next(iter(x.values())))
if len(currentList) < currentList.index(x) + 1:
findCategories(currentList[currentList.index(x) + 1], False)
findCategories(data, True)
I would want finalCategories to contain the following:
['019301948109', '013904123908', '019341823908', '1309183912309', '10293182094']
You can use recursion with a generator:
categoriesToFind = ['019301948109', '1309183912309']
d = [{'category1': '0120391123123'}, [{'subcategory': '0120391123123'}, [{'subsubcategory': '019301948109'}, [{'subsubsubcategory': '013904123908'}, [{'subsubsubsubcategory': '019341823908'}]]]], [{'subcategory2': '0934810923801'}, [{'subsubcategory2': '09341829308123'}]], [{'category2': '1309183912309'}, [{'subcategory': '10293182094'}]]]
def get_subcategories(_d, _flag):
flag = None
for i in _d:
if isinstance(i, dict):
_val = list(i.values())[0]
if _val in categoriesToFind or _flag:
yield _val
flag = True
else:
yield from get_subcategories(i, _flag or flag)
print(list(get_subcategories(d, False)))
Output:
['019301948109', '013904123908', '019341823908', '1309183912309', '10293182094']
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.