-2

I asked a while ago about how to make a list of sublists (and of even more sublists) from a string given delimiters here. How to process a string into layer of sublists

Now, I need to join them back together and I'm not sure how. I have tried to look here Python : Recursively flatten a list and Flatten a list in python

However, neither of these answers work in my case because chain splits my string (single item "lists") into characters, and then therefore cannot join with "\n", and reduce does not concatenate str and list objects.

I will probably need to walk through the sublists (and their sublists) at some point. Is there a way to iterate through each level of sublist? (I can leave this as a separate question, one step at a time.. but just wondering whether this process is making sense or should I try an entire new method. I do think logically this makes the most sense, I'm just having trouble navigating it.)

Thanks.

Community
  • 1
  • 1
chemelnucfin
  • 411
  • 1
  • 4
  • 9
  • 2
    Please give an exact example of your input and desired output. From your previous question it looks like your input is `[a, a, a, [b, a, a, [b, a, c], a, [b, a, a, c], a, c], a]` (with the letters being strings not variables) and your desired output is `'aaabaabacabaacaca'`, is that right? – agf May 15 '12 at 00:16
  • 1
    possible duplicate of [Flatten (an irregular) list of lists in Python](http://stackoverflow.com/q/2158395/). Whether you need to join strings with newlines, empty strings or what-have-you doesn't make for a different question. Consider two pieces of code that differ only in the values used for computation. That you can unify them by abstracting into a function demonstrates they are in essence the same algorithm. Q&As are analogous. – outis May 15 '12 at 01:27
  • @outis Thanks for the link, I think I have missed that. The two I linked did not work with the single string item. I will test the solution out and delete this question if it works. – chemelnucfin May 15 '12 at 01:59
  • @chemelnucfin You can't delete this question since I gave an upvoted answer to it. It's perfectly fine to post a question where you couldn't get the existing answers to other, similar questions to work. I showed how to use the existing answers, rather than writing new code, so no effort was wasted. – agf May 15 '12 at 02:12
  • The question is poorly asked: "How to flatten a list of list with variable lengths into a single list" doesn't describe the requirements, since then one of the links OP found already would have resolved the matter. – Karl Knechtel Sep 06 '22 at 03:17

1 Answers1

1

I'm going to assume what I said in the comment is correct unless you say otherwise.

From the posts you linked, you have:

import collections

def flatten(l):
    for el in l:
        if isinstance(el, collections.Iterable) and not isinstance(el, basestring):
            for sub in flatten(el):
                yield sub
        else:
            yield el

to recursively flatten an irregular list of lists, and

def go(it):
    for x in it:
        if x == 'b':
            yield [x] + list(go(it))
        else:
            yield x
            if x == 'c':
                break

to create that irregular list of lists from an iterator.

The inverse of go is just flatten(output_of_go) so the inverse of go(iter(string)) is ''.join(flatten(output_of_go)). You can see that with this test code:

lst = "aaabaabacabaacaca"
assert ''.join(flatten(go(iter(lst)))) == lst
Community
  • 1
  • 1
agf
  • 171,228
  • 44
  • 289
  • 238
  • Why not `if hasattr(el, '__iter__'): ...`? – akaRem May 15 '12 at 02:04
  • @akaRem I didn't write it, it's a quote from the post he linked (as I state), but it's because it's not that simple -- it would work if it supports `__getitem__(0)`, `__getitem__(1)`, etc. or `next()`, not just if it supports `__iter__()`. Using the `collections` Abstract Base Classes is the canonical method for new version of Python. – agf May 15 '12 at 02:10
  • Ok. I see. I mean since `str` doesn't have `__iter__` but `list` and `tuple` nave, this check seemse to be more convenient (I use it in such cases). You have a great rating. It was interesting to hear your view. – akaRem May 15 '12 at 02:37