Skip to main content
 首页 » 编程设计

Python加速重建列表的代码

2025年05月04日15mate10pro

我一直在努力做的是重建一些列表,例如:

[42351, 4253, 1264, 5311, 3651]  # The first number in a list is an ID 
[42352, 4254, 1244, 1246, 5311, 1264, 3651] 
[42353, 1254, 1264] 

变成这样的格式:

# ID \t 1 \t the_second_number_in_a_list \t ID \t 2 \t the_third_number_in_a_list \t ID \t 3 \t the_forth_number_in_a_list ... 
42352   1   4254    42352   2   1244    42352   3   1246    42352   4   5311    42352   5   1264    42352   6   3651 
42353   1   1254    42353   2   1264 
42351   1   4253    42351   2   1264    42351   3   5311    42351   4   3651 

我的想法是创建一个具有所需格式的中间字典:

list_dic = {42352: [42352, 1, 4254, 42352, 2, 1244, 42352, 3, 1246, 42352, 4, 5311, 42352, 5, 1264, 42352, 6, 3651], 42353: [42353, 1, 1254, 42353, 2, 1264], 42351: [42351, 1, 4253, 42351, 2, 1264, 42351, 3, 5311, 42351, 4, 3651]} 

然后将其保存到以制表符分隔的txt文件中。

但是,我意识到在现实中我可能有数十万个列表,而且我的方法会很慢并且计算量大。 我正在寻找建议来加速我的代码并减少整个过程所需的内存。谢谢。


附上我的代码:

seq1 = [42351, 4253, 1264, 5311, 3651] 
seq2 = [42352, 4254, 1244, 1246, 5311, 1264, 3651] 
seq3 = [42353, 1254, 1264] 
 
# First, group all information into a single list 
seq_list = [seq1, seq2, seq3] 
 
# Second, construct a dictionary to store all information 
list_dic = {}  
for each_seq in seq_list: 
    j = 1 
    list_dic[each_seq[0]] = [] 
    for each_item in each_seq[1:]: 
        list_dic[each_seq[0]].append(each_seq[0]) 
        list_dic[each_seq[0]].append(j) 
        list_dic[each_seq[0]].append(each_item) 
        j += 1 
 
# Third, save the information into a txt file    
text_file = open("Output.txt", "w") 
for each_id in list_dic: 
    line = '\t'.join(str(each_num) for each_num in list_dic[each_id]) 
    text_file.write(line+'\n') 
text_file.close() 

请您参考如下方法:

from itertools import chain,count,cycle 
with open("out.txt","wb") as f: 
    for eachlist in alllists: 
        merged = zip(cycle([eachlist[0],]),count(1),eachlist[1:]) 
        f.write( "\t".join( map(str,chain.from_iterable(merged)) ) ) 
        f.write("\n") 

据我所知,没有任何理由创建中间字典

(也就是说您现有的解决方案似乎也很可行(尽管可能有点慢)

对于@SirParselot

>>> seq1 = [42351, 4253, 1264, 5311, 3651] 
>>> seq2 = [42352, 4254, 1244, 1246, 5311, 1264, 3651] 
>>> seq3 = [42353, 1254, 1264] 
>>> alllists = [seq1, seq2, seq3] 
>>> for eachlist in alllists: 
...     merged = zip(cycle([eachlist[0],]),count(1),eachlist[1:]) 
...     print "\t".join( map(str,chain.from_iterable(merged)) ) 
... 
42351   1       4253    42351   2       1264    42351   3       5311    42351    4       3651 
42352   1       4254    42352   2       1244    42352   3       1246    42352    4       5311    42352   5       1264    42352   6       3651 
42353   1       1254    42353   2       1264