Currently I have a list that I have read from a file and it looks similar to...

file = [S,D,S,D,S,D,N,S,D,S,D,N]

What I was wondering is there a way to loop through the list so that I get multiple lists.

For Example....

List1 = [S,D,S,D,S,D,N]
List2 = [S,D,S,D,N]

If someone could help it would be great!

Thanks

Recommended Answers

All 9 Replies

After you append N to the list you should start/create a new list.

Do you want to break the list into 2 lists at a set index?

file = ['S','D','S','D','S','D','N','S','D','S','D','N']
break_index = 6
list1 = file[:break_index+1]
list2 = file[break_index+1:]
print list1
print list2
 
"""
result -->
['S', 'D', 'S', 'D', 'S', 'D', 'N']
['S', 'D', 'S', 'D', 'N']
"""

I suggest a list of lists. It's pretty straightforward, but this being Friday afternoon and all:

S = 'S'
D = 'D'
N = 'N'
data = [S,D,S,D,S,D,N,S,D,S,D,N]
lists = reduce(lambda x,y: (eval(["x[-1].append(y)","(x[-1].append(y),x.append([]))"][y==N]),x)[1], data, [[]])[:-1]
print lists

Produces:

[['S', 'D', 'S', 'D', 'S', 'D', 'N'], ['S', 'D', 'S', 'D', 'N']]

, and generalizes to longer lists.

vegaseat -- There is not a set index.

BearofNH -- I see this works but could you explain how this works because I need to modify it to get it to work exactly like I need it.

Sorry I didnt give this information before but I guess I thought I might be able to figure it out rather easily but I think I was wrong there.
The S, D, and N are just the first letters of the items in the list.

If more information is required please let me know.

Sorry again for the late information and thanks for all the help!

I see this works but could you explain how this works because I need to modify it to get it to work exactly like I need it.

Hmmm, welll, ahhh, you probably don't want to modify my code as presented. It is not the most straightforward approach.

First thing to do: don't use file as a variable, as that overwrites the Python builtin. I use data below.

I think what you want is the list index() method. If your data looks like: data = [S,D,S,D,S,D,N,S,D,S,D,N] . Then data.index(N) locates the first N in the data, and returns its index, in this case 6. You [1] stash that slice (0 thru 7, to include the N) into a new list-of-lists, [2] remove that from your data, and [3] iterate over the remainder. Something like this:

data    = [4, 'es', 51, 'q', 'dqrs', -33, 'q', 67, 'q']
newdata = []   # Grow this into a list of lists broken at 'q'
while  len(data) > 0:              # As long as there's more
    firstq = data.index('q')        # Find next 'q'
    if firstq < 0:                        # No more data?
        break    # Done
    newdata.append(data[:firstq+1])        # Add next list
    del (data[:firstq+1])                # Remove from data

print newdata

[[4, 'es', 51, 'q'], ['dqrs', -33, 'q'], [67, 'q']] Here 'q' plays the role of N, the point where you divide up your lists. This code successively locates the next 'q' and appends a list up to and including the 'q' to newdata, then deletes it from data.

Kind of simplistic, but easier to modify.

BearofNH -- Thanks this helped and worked!!!!

data    = [4, 'es', 51, 'q', 'dqrs', -33, 'q', 67, 'q']
newdata = []   # Grow this into a list of lists broken at 'q'
while  len(data) > 0:              # As long as there's more
    firstq = data.index('q')        # Find next 'q'
    if firstq < 0:                        # No more data?
        break    # Done
    newdata.append(data[:firstq+1])        # Add next list
    del (data[:firstq+1])                # Remove from data

print newdata

Okay...I modified this code rather easily to get to this point

while  len(data) > 0:                     # As long as there's more
        firstN = data.index(Ndata[x])             # Find next
        if firstN < 0:                            # No more data?
            break                                     # Done
        newdata.append(data[:firstN+1])           # Add next list
        del (data[:firstN+1])                     # Remove from data
        x += 1

Ndata =

newdata = [['S"O_MRun"\n', 'D\n', 'S"O_MDir"\n', 'D\n', 'S"I_Ps1"\n', 'D\n', 'N"StraighConv1"\n'], ['S"O_MRun"\n', 'D\n', 'S"O_MDir"\n', 'D\n', 'S"I_Ps1"\n', 'D\n', 'N"StraighConv2"\n'], ['S"O_MRun"\n', 'D\n', 'S"O_MDir"\n', 'D\n', 'S"I_Ps1"\n', 'D\n', 'N"StraighConv3"\n'], ['S"O_MRun"\n', 'D\n', 'S"O_MDir"\n', 'D\n', 'S"I_Ps1"\n', 'D\n', 'N"StraighConv4"\n'], ['N"PLC"\n'], ['S"O_RotFw"\n', 'D\n', 'S"O_RbME"\n', 'D\n', 'S"O_RotRv"\n', 'D\n', 'S"I_Rot_Home"\n', 'D\n', 'S"I_Rot_Advance"\n', 'D\n', 'S"I_Ps1_Rb"\n', 'D\n', 'S"I_Ps2_Rb"\n', 'D\n', 'N"TurnTable1"\n'], ['S"O_RotFw"\n', 'D\n', 'S"O_RbME"\n', 'D\n', 'S"O_RotRv"\n', 'D\n', 'S"I_Rot_Home"\n', 'D\n', 'S"I_Rot_Advance"\n', 'D\n', 'S"I_Ps1_Rb"\n', 'D\n', 'S"I_Ps2_Rb"\n', 'D\n', 'N"TurnTable2"\n'], ['S"O_RotFw"\n', 'D\n', 'S"O_RbME"\n', 'D\n', 'S"O_RotRv"\n', 'D\n', 'S"I_Rot_Home"\n', 'D\n', 'S"I_Rot_Advance"\n', 'D\n', 'S"I_Ps1_Rb"\n', 'D\n', 'S"I_Ps2_Rb"\n', 'D\n', 'N"TurnTable3"\n']]

So now if I want to say take....

newdata[0] = [

...and make it like this....

newdata[0] = [['N"StraighConv1"\n', 'S"O_MRun"\n', 'D\n'], ['N"StraighConv1"\n', 'S"O_MDir"\n', 'D\n'], ['N"StraighConv1"\n', 'S"I_Ps1\n', 'D\n']]

...how would I go about that?

I have tried many ways but can not figure it out, mostly I ended up getting stuck in an infinate loop. Please help me once more!!!

Thanks for everything!!

Assuming well-formed input (which in fact you should check for and handle somehow) the basic riff looks like this:

lnd = (len(newdata[0]) - 1) / 2
newlist = []
for x in range(lnd):
    newlist.append([newdata[0][-1], newdata[0][2*x], newdata[0][2*x+1]])
newdata[0] = newlist
newdata[0]

[['N"StraighConv1"\n', 'S"O_MRun"\n', 'D\n'], ['N"StraighConv1"\n', 'S"O_MDir"\n', 'D\n'], ['N"StraighConv1"\n', 'S"I_Ps1"\n', 'D\n']]

If you want to convert the entire list, not just element 0, then you can package the above under an iterator over the full list. The following code creates a new list instead of overwriting the old one element-by-element only because I whipped it up off the cuff.

newlist = []
for element in newdata:
    lel   = (len(element) - 1) / 2
    newel = []
    for x in range(lel):
        newel.append([element[-1], element[2*x], element[2*x+1]])
    newlist.append(newel)

This could be dead wrong (my eyes glaze over looking at newlist) but it might work.

BearofNH -- Thanks a lot!! You have been a great help through this!! Again thanks!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.