Skip to content Skip to sidebar Skip to footer

Recursive Function For Extract Elements From Deep Nested Lists/tuples

I want to write a function that extracts elements from deep nested tuples and lists, say I have something like this l = ('THIS', [('THAT', ['a', 'b']), 'c', ('THAT', ['d', 'e', 'f

Solution 1:

You're doing terms = [] at the top of the function, so of course every time you recursively call the function, you're doing that terms=[] again.

The quickest solution is to write a simple wrapper:

def_extract(List):
    global terms
    for i in word:
        iftype(i) isnotstr:
            _extract(i)
        else:
            if i isnot"THIS"and i isnot"THAT":
                terms.append(i)
    return terms

defextract(List):
    global terms
    terms = []
    return _extract(List)

One more thing: You shouldn't use is to test for string equality (except in very, very special cases). That tests that they're the same string object in memory. It will happen to work here, at least in CPython (because both "THIS" strings are constants in the same module—and even if they weren't, they'd get interned)—but that's not something you want to rely on. Use ==, which tests that they both mean the same string, whether or not they're actually the identical object.

Testing types for identity is useful a little more often, but still not usually what you want. In fact, you usually don't even want to test types for equality. You don't often have subclasses of str—but if you did, you'd probably want to treat them as str (since that's the whole point of subtyping). And this is even more important for types that you do subclass from more often.

If you don't completely understand all of that, the simple guideline is to just never use is unless you know you have a good reason to.

So, change this:

if i isnot"THIS"and i isnot"THAT":

… to this:

ifi!="THIS"andi!="THAT":

Or, maybe even better (definitely better if you had, say, four strings to check instead of two), use a set membership test instead of anding together multiple tests:

if i not in {"THIS", "THAT"}:

And likewise, change this:

iftype(i) isnotstr:

… to this:

ifnotisinstance(i, str):

But while we're being all functional here, why not use a closure to eliminate the global?

defextract(List)
    terms = []
    def_extract(List):
        nonlocal terms
        for i in word:
            ifnotisinstance(i, str):
                _extract(i)
            else:
                if i notin {"THIS", "THAT"}:
                    terms.append(i)
        return terms
    return _extract(List)

This isn't the way I'd solve this problem (wim's answer is probably what I'd do if given this spec and told to solve it with recursion), but this has the virtue of preserving the spirit of (and most of the implementation of) your existing design.

Solution 2:

It will be good to separate the concerns of "flattening" and "filtering". Decoupled code is easier to write and easier to test. So let's first write a "flattener" using recursion:

from collections import Iterable

defflatten(collection):
    for x in collection:
        ifisinstance(x, Iterable) andnotisinstance(x, str):
            yieldfrom flatten(x)
        else:
            yield x

Then extract and blacklist:

defextract(data, exclude=()):
    yieldfrom (x for x in flatten(data) if x notin exclude)

L = ('THIS', [('THAT', ['a', 'b']), 'c', ('THAT', ['d', 'e', 'f'])])
print(*extract(L, exclude={'THIS', 'THAT'}))

Solution 3:

Assuming that the first element of each tuple can be disregarded, and we should recurse with list that is the second element, we can do this:

defextract(node):
    ifisinstance(node, tuple):
        return extract(node[1])
    ifisinstance(node, list):
        return [item for sublist in [extract(elem) for elem in node] for item in sublist]
    return node

The list comprehension is a little dense, here's the same with loops:

defextract(node):
    ifisinstance(node, tuple):
        return extract(node[1])
    ifisinstance(node, list):
        result = []
        for item in node:
            for sublist in extract(item):
                for elem in sublist:
                    result.append(elem)
        return result
    return node

Solution 4:

This iterative function should do the trick alongside the .extend() list operator.

def func(lst):
    new_lst = []
    for i in lst:
        if i != 'THAT' and i != 'THIS':
            iftype(i) == list or type(i) == tuple: 
                new_lst.extend(func(i))
            else: new_lst.append(i)
    return new_lst

l = ('THIS', [('THAT', ['a', 'b']), 'c', ('THAT', ['dk', 'e', 'f'])])
print(func(l))

['a', 'b', 'c', 'dk', 'e', 'f']

Post a Comment for "Recursive Function For Extract Elements From Deep Nested Lists/tuples"