0

I am having to deal with either lists or lists of lists. I find that my current method is not very efficient as I have to define two separate methods for doing things and effectively write them out twice. Is there a more efficient way to check if incoming data is a list or list of lists and then proceed accordingly (I usually want to act on each item of incoming data and output a list or list of lists that matches structure of the incoming data). Here's an example of me dealing with it:

if not any(isinstance(item, list) for item in keys):
    userStrings = []
    for key in keys[index]:
        userStrings[index].extend(item.GetUserStrings().GetValues(key))
else:
    userStrings = [[] for i in range(len(rhObjects))]
    for index, item in enumerate(rhObjects):
        for key in keys[index]:
            userStrings[index].extend(item.GetUserStrings().GetValues(key))

What if i encounter something even more complex where its a list of lists of lists etc. This method will not work...ideas?

Thanks!

This doesn't seem to be clear enough. Let's start over. Imagine that I have a list of lists, a list, or even a list of lists of lists of objects:

_list = [["X","Y","Z"],["X","Y","Z",["X","Y","Z"]],["X","Y","Z"]]
_list2 = ["X","Y","Z"]
_list3 = [["X","Y","Z"],["X","Y","Z"]]

All I want to do is perform a simple function on each object from these lists (append "A" to each object) or create a whole new object but in a fashion that matches structure of the original input list. Example functions:

   def my_function(obj):
       return obj + "A"
   def my_function2(obj):
       return newObj

DESIRED OUTPUT:

_newList = [["XA","YA","ZA"],["XA","YA","ZA",["XA","YA","ZA"]],["XA","YA","ZA"]]
_newList2 = ["XA","YA","ZA"]
_newList3 = [["XA","YA","ZA"],["XA","YA","ZA"]]

or if used my_function2:

_newList = [["newObj","newObj","newObj"],["newObj","newObj","newObj",["newObj","newObj","newObj"]],["newObj","newObj","newObj"]]

Is there a way to make this like the user below suggested with two functions? One recursive? I am a total noob to recursion and the first approach that you saw on top was my way of checking for lists within the list, but its really not flexible. All/any help is appreciated!

1 Answer 1

2

Your logic should be in a separate method, and you should use recursion.

def process_list(_list):
    new_list = []
    for x in _list:
        if is_list(x):
            new_list.append(process_list(x))
        else:
            new_list.append(map(lambda y: y + "A",x))
    return new_list

def is_list(l):
    return type(l) == types.ListType

_list = [["X","Y","Z"],["X","Y","Z",["X","Y","Z"]],["X","Y","Z"]]
print(process_list(_list))
print(_list)

The code is just to demo the recursion. Hope you get it and construct your code likewise.

Sign up to request clarification or add additional context in comments.

3 Comments

My code is just a sketch up to show you could use recursion to inspect lists of lists. I cannot provide a code without knowing your logic. Checkout the link I provided for recursion if you want to lean about it more.
would you be able to demonstrate it on the example i added to the question? something really simple with strings but lists would vary from really simple to more complex. I just want to see how the functions need to be set up because when i tried your sketch it failed (infinite loop i guess)
This method works because strings are iterable. What if my data is a list of integers? map() fails on objects that are not iterable and I am getting an error. Is there an alternative approach to matching a data structure than map()? Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.