0

所以我有一个子列表

子列表的第一个值是 ID,第二个值是索引。

最终,我试图按照索引的顺序为每个 ID 组装字符串。

raw_IDs = ['TCONS_0040771;1','TCONS_0040771;2','TCONS_0040771;3','TCONS_00040772;1','TCONS_00040772;2','TCONS_00040773;1','TCONS_00040773;2','TCONS_00040773;3','TCONS_00040773;4']

IDs = [['TCONS_0040771',1],['TCONS_0040771',2],['TCONS_0040771',3],['TCONS_00040772',1],['TCONS_00040772',2],['TCONS_00040773',1],['TCONS_00040773',2],['TCONS_00040773',3],['TCONS_00040773',4]]

我有每个值的序列字典,所以......

sequences = []

for k in raw_IDs:
    sequences.append(D_ID_seq[k])
print sequences

sequences = ['AAA','AAB','AAAB','AAAA','BAA','BBA','BBB','CCC','DDD']  

我正在尝试根据 ID、TCONS_xxx 值组装序列

desired_output = ['AAAAABAAAB','AAAABAA','BBABBBCCCDDD']

示例:ID 中的前 3 个元素都具有相同的 ID 'TCONS_0040771'。但是,它们具有不同的指数,范围为 1-3。对于索引 1-2 的“TCONS_0040772”和索引 1-4 的“TCONS_0040773”重复此操作。

所需的输出是从字典值收集的所有字符串的组合,该字典值附加到基于相应 ID 的称为“序列”的列表中

注意***我正在考虑创建一个while循环,但当我有时尝试它们时,它们会变得非常混乱并最终运行无限时间。

任何帮助将不胜感激

4

2 回答 2

0
# This assumes raw_IDs[] and sequences[] have been defined
IDs = [id.split(';') for id in raw_IDs]

prev_id = None
desired_output = []
for id in IDs.keys()
    if id != prev_id:
        if prev_id:
            desired_output.append(output)
        output = ''
    output += sequences.pop(0)
if output:
    desired_output.append(output)
于 2013-06-10T21:10:13.527 回答
0

使用这些数据:

IDs = [['TCONS_0040771', 1], ['TCONS_0040771', 2], ['TCONS_0040771', 3],
       ['TCONS_00040772', 1], ['TCONS_00040772', 2], ['TCONS_00040773', 1],
       ['TCONS_00040773', 2], ['TCONS_00040773', 3], ['TCONS_00040773', 4]]
sequences = ['AAA','AAB','AAAB','AAAA','BAA','BBA','BBB','CCC','DDD']

这段代码:

last_id = IDs[0][0]
res = [sequences[0]]
for index, (id_, _) in enumerate(IDs[1:], 1):
    if id_ == last_id:
        res[-1] += sequences[index]
    else:
        res.append(sequences[index])
    last_id = id_

给这个res

['AAAAABAAAB', 'AAAABAA', 'BBABBBCCCDDD']
于 2013-06-10T21:26:40.970 回答