mercredi 8 février 2017

How to select the list I just iteratively created

Vote count: 0

I need to read chars from a txt file and iteratively create lists to put them into and put those lists in a list of lists. When there is a { in the txt file I need to start a new list, and when there is a } I need to revert to a previous list. Basically, I am trying to build my own json interpreter because I'm dealing with some shonky json responses in another project, but the exercise is a good one anyway and I need to understand more basic Python stuff.

I have hit a problem because I don't know syntax that allows me to iteratively create lists and refer to them by name or index in the list of lists so I can select them and append/concatenate them.

for i in filedata:
    if i == leftbrace:
        #create new list in listoflists
        listoflists.append([])
        #add 1 to listindex to shift to new list in listoflists
        listindex = (listindex + 1)
    elif i == rightbrace:
        #minus 1 from listindex to shift to previous list in listoflists
        listindex = (listindex - 1)
    else:
        #add i to the new list at list index listindex in listoflists
        list[:listindex].append(i)

print (listoflists)

asked 27 secs ago

Let's block ads! (Why?)



How to select the list I just iteratively created

Aucun commentaire:

Enregistrer un commentaire