如何有条件地替换子列表中的项目。

时间:2018-11-01 14:27:57

标签: python python-3.x bioinformatics

我正在尝试生成列表内列表。如果一个子列表元素更大,我正在循环浏览文件以更新列表。我已经编写了这段代码:

targets = open(file)

longest_UTR = []

for line in targets:

    chromosome, locus, mir, gene, transcript, UTR_length = line.strip("\n").split("\t")

    length_as_integer = int(UTR_length)

    if not any(x[:3] == [locus, mir, gene] for x in longest_UTR):

        longest_UTR.append([locus, mir, gene, transcript, length_as_integer])

    elif length_as_integer > [int(x[4]) for x in longest_UTR]: ##x[4] = previous length_as_integer

        longest_UTR.append([locus, mir, gene, transcript, length_as_integer])

print (longest_UTR)

但是,出现此错误:

elif len_as_int > (int(x[4]) for x in longest_UTR):

TypeError: '>' not supported between instances of 'int' and 'generator'

如何将x[4]转换为整数以便与length_as_integer进行比较?

谢谢

3 个答案:

答案 0 :(得分:0)

如果我做对了,请尝试将elif行替换为以下内容:

else:
    longest_UTR = [[locus, mir, gene, transcript, length_as_integer] for x in longest_UTR if x[:3] == [locus, mir, gene] and length_as_integer > int(x[4]) else x]:

您遍历所有列表,更新匹配条件的列表,如果不匹配,则不执行任何操作。

答案 1 :(得分:0)

因此,关于您的要求有一些来回的信息,但是我的最终理解是: 您正在遍历数据集。此数据集中的每个target都有一个locusmrigene以及一个UTR_length属性。对于locusmrigene的每个唯一组合,您要查找具有最大targets的所有UTR_Length吗?

鉴于您要在数据集中找到最大值,有两种方法。
1)您可以简单地将输入文件转换为pandas数据框,并按locusmrigene值分组,并返回所有带有max(UTR_Length)的值。从易于实现的角度来看,这可能是您最好的选择。但是,pandas并不总是正确的工具,并且会带来很多开销,尤其是如果您要对项目进行Dockerise。

2)如果您想使用基本的python包,我建议您使用集合和字典:

targets = open(file)
list_of_targets = []    
for line in targets:

          chromosome, locus, mir, gene, transcript, UTR_length = line.strip("\n").split("\t")
          length_as_integer = int(UTR_length)

          list_of_targets.append((chromosome, locus, mir, gene, transcript, UTR_length))

# Generate Set of unqiue locus, mri, gene (lmg) combinations
set_of_locus_mri_gene = {(i[1], i[2], i[3]) for i in list_of_targets}

# Generate dictionary of maximum lengths for each distinct lmg combo
dict_of_max_lengths = {lmg: max([targets[5] for targets in list_of_targets if 
                                    (targets[1], targets[2], targets[3]) == lmg]) for 
                                    lmg in set_of_locus_mri_gene}

# Generate dictionary with lmg keys and all targets with corresponding max length
final_output = {lmg: [target for target in list_of_targets if target[5] == max_length] for
                        lmg, max_length in dict_of_max_lengths.items()}

答案 2 :(得分:0)

由于您要替换longest_UTR变量并使名称保持良好的名称,因此可以使用字典而不是列表:

targets = open(file)
longest_UTR = {}

for line in targets: 
    chromosome, locus, mir, gene, transcript, UTR_length = line.strip("\n").split("\t")    
    length_as_integer = int(UTR_length)

    # Your condition works for initializing the dictionary because of the default value.
    if length_as_integer > longest_UTR.get("Length", -1):
        longest_UTR["Chromosome"] = chromosome
        longest_UTR["Locus"] = locus
        longest_UTR["Mir"] = mir
        longest_UTR["Gene"] = gene
        longest_UTR["Transcript"] = transcript
        longest_UTR["Length"] = length_as_integer

print (longest_UTR)

编辑:这也是使用列表的代码版本,以防万一您有兴趣看到不同之处。我个人觉得字典的清洁方法更易于阅读。

targets = open(file)
longest_UTR = [None, None, None, None, None, -1]

for line in targets: 
    chromosome, locus, mir, gene, transcript, UTR_length = line.strip("\n").split("\t")    
    length_as_integer = int(UTR_length)

    # Your condition works for initializing the list because of the default value.
    if length_as_integer > longest_UTR[5]:
        longest_UTR[0] = chromosome
        longest_UTR[1] = locus
        longest_UTR[2] = mir
        longest_UTR[3] = gene
        longest_UTR[4] = transcript
        longest_UTR[5] = length_as_integer

print (longest_UTR)