How do I filter the list by selecting combinations of unique characters in the elements &lp…

微信扫一扫,分享到朋友圈

How do I filter the list by selecting combinations of unique characters in the elements &lp…

I have the the following pairs stored in the following list

sample = [[CGCG,ATAT],[CGCG,CATC],[ATAT,TATA]]

Each pairwise comparison can have only two unique combinations of characters, if not then those pairwise comparisons are eliminated. eg,

In sample[1]
    C       C
    G       A
    C       T
    G       C

Look a the corresponding elements in both sub-lists, CC, GA, CT, GC.

Here, there are more than two types of pairs (CC), (GA), (CT) and (GC). So this pairwise comparison cannot occur.

Every comparison can have only 2 combinations out of (AA, GG,CC,TT, AT,TA,AC,CA,AG,GA,GC,CG,GT,TG,CT,TC) … basically all possible combinations of ACGT where order matters.

In the above example, more than 2 such combinations are found.

However,

In sample[0]
    C       A
    G       T
    C       A
    G       T

There are only 2 unique combinations: CA and GT

Thus, the only pairs, that remain are:

output = [[CGCG,ATAT],[ATAT,TATA]]

I would prefer if the code was in traditional for-loop format and not comprehensions

This is a small part of the question listed here. This portion of the question is re-asked, as the answer provided earlier provided incorrect output.

def filter_sample(sample):
    filtered_sample = []

    for s1, s2 in sample:
        pairs = {pair for pair in zip(s1, s2)}
        if len(pairs) <= 2:
            filtered_sample.append([s1, s2])

    return filtered_sample

Running this

sample = [["CGCG","ATAT"],["CGCG","CATC"],["ATAT","TATA"]]
filter_sample(sample)

Returns this

[['CGCG', 'ATAT'], ['ATAT', 'TATA']]

微信扫一扫,分享到朋友圈

How do I filter the list by selecting combinations of unique characters in the elements &lp…

阿里巴巴45亿投资汇通达 农村淘宝的潜力会超过城市吗?

上一篇

SpringMVC 教程 - Handler Method

下一篇

你也可能喜欢

How do I filter the list by selecting combinations of unique characters in the elements &lp…

长按储存图像,分享给朋友