2010-03-02 217 views
0

我有一个我从python脚本获得的列表。该列表的内容如下所示: 下面的内容在一个文件中,但我将它加载到一个列表中,以便将它与其他内容进行比较。但现在我必须拆分这个列表,以便每个创建的新列表都包含具有相应编号的复杂名称。希望这是清楚了吧:)在Python中分割列表

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

我想分裂这个名单,使得一个新的列表是从d.complex.1创建d.complex.2(不含d.complex.2),即:

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

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

任何人都可以帮我吗? 干杯, Chavanak

+0

的例子d.complex.1我想你需要澄清你的问题 – Mark 2010-03-02 13:39:01

+0

这真的不清楚只是副本,是这样的列表,字符串,从线文件? – 2010-03-02 13:59:32

+0

@chavanak:请不要评论你自己的问题。这是你的问题。您可以更改问题以使其更好。请更新您的问题并删除您的评论。 – 2010-03-02 15:51:37

回答

0
>>> 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']] 
+0

对上面的代码进行了一些修改,让它对我有用。 感谢吨gnibbler :) – forextremejunk 2010-03-02 14:12:26

0

没有更详细的信息,并假设您的“列表”在一个文件中。

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() 

输出

$ ./python.py 
d.complex.1 
24 
25 
67 
123 
764