Recursively generate subset of list in python
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']
python recursion
add a comment |
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']
python recursion
The original json is not consistently written ascategory1is a dict in the root list andcategory2is a dict in a nested list. Is that a typo or should be like this?
– b-fg
Nov 20 at 4:00
Yes this was intentional
– Evan Hessler
Nov 20 at 4:14
add a comment |
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']
python recursion
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']
python recursion
python recursion
asked Nov 20 at 3:51
Evan Hessler
779
779
The original json is not consistently written ascategory1is a dict in the root list andcategory2is a dict in a nested list. Is that a typo or should be like this?
– b-fg
Nov 20 at 4:00
Yes this was intentional
– Evan Hessler
Nov 20 at 4:14
add a comment |
The original json is not consistently written ascategory1is a dict in the root list andcategory2is a dict in a nested list. Is that a typo or should be like this?
– b-fg
Nov 20 at 4:00
Yes this was intentional
– Evan Hessler
Nov 20 at 4:14
The original json is not consistently written as
category1 is a dict in the root list and category2 is a dict in a nested list. Is that a typo or should be like this?– b-fg
Nov 20 at 4:00
The original json is not consistently written as
category1 is a dict in the root list and category2 is a dict in a nested list. Is that a typo or should be like this?– b-fg
Nov 20 at 4:00
Yes this was intentional
– Evan Hessler
Nov 20 at 4:14
Yes this was intentional
– Evan Hessler
Nov 20 at 4:14
add a comment |
1 Answer
1
active
oldest
votes
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_flagtrick is cool.
– slider
Nov 20 at 4:18
Thanks for the help! If anyone stumbles on this, you can do this in python 2.7.X with this: stackoverflow.com/questions/17581332/…
– Evan Hessler
Nov 20 at 4:22
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385959%2frecursively-generate-subset-of-list-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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_flagtrick is cool.
– slider
Nov 20 at 4:18
Thanks for the help! If anyone stumbles on this, you can do this in python 2.7.X with this: stackoverflow.com/questions/17581332/…
– Evan Hessler
Nov 20 at 4:22
add a comment |
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_flagtrick is cool.
– slider
Nov 20 at 4:18
Thanks for the help! If anyone stumbles on this, you can do this in python 2.7.X with this: stackoverflow.com/questions/17581332/…
– Evan Hessler
Nov 20 at 4:22
add a comment |
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']
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']
answered Nov 20 at 4:02
Ajax1234
39.9k42652
39.9k42652
The_flagtrick is cool.
– slider
Nov 20 at 4:18
Thanks for the help! If anyone stumbles on this, you can do this in python 2.7.X with this: stackoverflow.com/questions/17581332/…
– Evan Hessler
Nov 20 at 4:22
add a comment |
The_flagtrick is cool.
– slider
Nov 20 at 4:18
Thanks for the help! If anyone stumbles on this, you can do this in python 2.7.X with this: stackoverflow.com/questions/17581332/…
– Evan Hessler
Nov 20 at 4:22
The
_flag trick is cool.– slider
Nov 20 at 4:18
The
_flag trick is cool.– slider
Nov 20 at 4:18
Thanks for the help! If anyone stumbles on this, you can do this in python 2.7.X with this: stackoverflow.com/questions/17581332/…
– Evan Hessler
Nov 20 at 4:22
Thanks for the help! If anyone stumbles on this, you can do this in python 2.7.X with this: stackoverflow.com/questions/17581332/…
– Evan Hessler
Nov 20 at 4:22
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385959%2frecursively-generate-subset-of-list-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The original json is not consistently written as
category1is a dict in the root list andcategory2is a dict in a nested list. Is that a typo or should be like this?– b-fg
Nov 20 at 4:00
Yes this was intentional
– Evan Hessler
Nov 20 at 4:14