tags:

views:

89

answers:

2

Hi guys, I have a list which I have obtained from a python script. the content of the list goes something like: The content below is in a file but I loaded it into a list for comparing it to something else. But now I have to split this list such that each new list created contains the complex name with the corresponding number. Hope it is clear now :)

d.complex.1
24
25
67
123
764
d.complex.200
23
54
35
64
d.complex.302
.
.
.

I want to split this list such that, a new list is created from d.complex.1 to d.complex.2 (excluding d.complex.2) i.e.:

d.complex.1
24
25
67
123
764
end of list

>newlist
d.complex.200
23
54
35
64
endoflist

Can anyone help me out? Cheers, Chavanak

A: 

without more detailed info and assuming your "list" is in a file.

f=0
for line in open("file"):
    if "d.complex.2" in line: break # or exit
    if "d.complex.1" in line:
        f=1
    if f:
        print line.rstrip()

output

$ ./python.py
d.complex.1
24
25
67
123
764
ghostdog74
A: 
>>> mylist='d.complex.1\n24\n25\n67\n123\n764\nd.complex.200\n23\n54\n35\n64\nd.complex.302'.split("\n")
>>> res=[]
>>> for line in mylist:
...  if line.startswith("d.complex"):
...   res.append([])
...  res[-1].append(line)
... 
>>> res
[['d.complex.1', '24', '25', '67', '123', '764'], ['d.complex.200', '23', '54', '35', '64'], ['d.complex.302']]
gnibbler
A bit of modification to the above code, got it working for me.Thanks a ton gnibbler :)
forextremejunk