Home > Back-end >  How to locate a specific var type inside many others arrays in python?
How to locate a specific var type inside many others arrays in python?

Time:12-05

I'd like know how can I localize a specific type variable in a set of arrays, that could change its own length structure, i.e:

[[[[11.0, 16.0], [113.0, 16.0], [113.0, 41.0], [11.0, 41.0]], ("I WANNA BE LOCATED", 548967)]]

I just needed to extract the type variable that is a Str in this case:

"I WANNA BE LOCATED"

I tried use "for" loop, but it doesn't help, because possibly in my case, the string might be there in other indices. Is there another way? Maybe with numpy or some lambda?

CodePudding user response:

Here is an example of how you could use these functions to extract the string from the nested array:

# Define the nested array
arr = [[[[11.0, 16.0], [113.0, 16.0], [113.0, 41.0], [11.0, 41.0]], (1, "I WANNA BE LOCATED",)]]

# Define a function to extract the string from the nested array
def extract_string(arr):
    print(arr)
    # Iterate over the elements in the array
    for i, elem in enumerate(arr):
        # Check if the element is a string
        if isinstance(elem, str):
            # Return the string if it is a string
            return elem
        # Check if the element is a nested array
        elif isinstance(elem, list) or isinstance(elem, tuple):
            # Recursively call the function to search for the string in the nested array
            result = extract_string(elem)
            if result:
                return result

# Extract the string from the nested array
string = extract_string(arr)
print(string)

In this code, the extract_string() function recursively searches the nested array for a string. If it finds a string, it returns the string. If it finds a nested array, it recursively calls itself to search for the string in the nested array. This allows the function to search for the string in any level of the nested array.

CodePudding user response:

I'd do it recursively; this, for example, will work (provided you only have tuples and lists):

collection = [[[[11.0, 16.0], [113.0, 16.0], [113.0, 41.0], [11.0, 41.0]], ("I WANNA BE LOCATED", 548967)]]

def get_string(thing):
    if type(thing) == str:
        return thing
    if type(thing) in [list, tuple]:
        for i in thing:
            if (a := get_string(i)):
                return a
    return None

get_string(collection)
# Out[456]: 'I WANNA BE LOCATED'

CodePudding user response:

  1. Flatten the arbitrarily nested list;
  2. Filter the strings (and perhaps bytes).

Example:

from collections.abc import Iterable

li=[[[[11.0, 16.0], [113.0, 16.0], [113.0, 41.0], [11.0, 41.0]], ("I WANNA BE LOCATED", 548967)]]

def flatten(xs):
    for x in xs:
        if isinstance(x, Iterable) and not isinstance(x, (str, bytes)):
            yield from flatten(x)
        else:
            yield x

>>> [item for item in flatten(li) if isinstance(item,(str, bytes))]
['I WANNA BE LOCATED']
  • Related