ive been attempting this problem for a while and I cant seem to get the issue out.
listy2=['fun','super','Stop']
listy=['great','life','Stop','toys','nothing','Stop']
listpool=[listy,listy2]
current=[]
total=[]
nested=True
for i in listpool:
for line in enumerate(i):
if (line[0]==0):
current.append(line[1])
#skipping first line
continue
if (line[1].strip()!="") and (nested==True):
current.append(line[1])
nested=False
continue
if (line[1].strip()=="Stop"):
nested=True
continue
total.append(current)
#current[:] = []
print total
The program should output a list of lists.
[[first element of list, nested things],[first element of list, nested things]]
Nested things are like:
hello
blah blah
Stop
the nested thing in this case would be hello
The lists can have multiple nested things.
cool
blah blah
Stop
good
blah blah
Stop
the nested thing in this example would be cool and good, (as you can tell, the things in the middle do no matter)
In my code it SHOULD output
[['fun','super'],['great','life','toys']]
however it does not.
Sorry if I cannot explain this question well, as English is not my first language but I am getting used to it. If you have any questions or comments please comment here. Sorry if I have done something stupid. It would also be nice if you could explain my error, but that would not be necessary.
Thanks.
I don’t quite understand everything you’re trying to do here, but the simplest answer is that you’re not clearing the ‘current’ list in between outer loop iterations.
Try it like this:
This is (slightly) more idiomatic python: