Issue
How can I access the int value in a list that looks like this?
occurence_list = [["sleep", ["morning_meds", 2], ["watching_tv", 3]], ["morning_meds", ["sleep", 1], ["watching_tv", 3]]]
Solution
There's not much to go on based on your example. You would need to know the structure of the array to make a more informed decision. However, if you want to generalise the issue, saying that you have an arbirarily nested array, and you want to extract all integers, you could do this:
def get_ints(nested_list):
for item in nested_list:
if isinstance(item, list):
yield from get_ints(item)
if isinstance(item, int):
yield item
ints = list(get_ints(occurence_list))
Output:
[2, 3, 1, 3]
Answered By - tituszban
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.