如何在对列表元素进行成对排序后保留原始列表的位置值(Python)?

2024-05-15 22:47:02 发布

您现在位置:Python中文网/ 问答频道 /正文

sample = ['AAAA','CGCG','TTTT','AT$T','ACAC','ATGC','AATA']
Position = [0,    1,     2,     3,      4,     5,     6]

我有与每个元素相关联的位置上面的示例。我做了几个过滤步骤,代码是here.

消除的步骤是:

#If each base is identical to itself eliminate those elements eg. AAAA, TTTT
#If there are more than 2 types of bases (i.e.' conversions'== 1 ) then eliminate those elements eg. ATGC
#Make pairs of all remaining combinations
#If a $ in the pair, then the corresponding base from the other pair is eliminated eg. (CGCG,AT$T) ==> (CGG, ATT) and (ATT, AAA)
#Remove all pairs where one of the elements has all identical bases eg. (ATT,AAA)

最后,我有一个输出,上面有不同的组合,如下所示

Final Output [['CGG','ATT'],['CGCG','ACAC'],['CGCG','AATA'],['ATT','ACC']]

我需要找到一种方法,这样我就可以得到这些对相对于原始样本的位置,如下所示

Position = [[1,3],[1,4],[1,6],[3,4]]

Tags: oftheifpositionelementsallattat
1条回答
网友
1楼 · 发布于 2024-05-15 22:47:02

可以先将列表转换为元组列表

xs = ['AAAA', 'CGCG', 'TTTT', 'AT$T', 'ACAC', 'ATGC', 'AATA']
ys = [(i, x) for i,x in enumerate(xs)]

print(ys)
=> [(0, 'AAAA'), (1, 'CGCG'), (2, 'TTTT'), (3, 'AT$T'), (4, 'ACAC'), (5, 'ATGC'), (6, 'AATA')]

然后将其作为输入列表

相关问题 更多 >