简体   繁体   English

根据长度连接列表中的元素 - Python

[英]Joining Elements in a List according to length - Python

I am trying to design a parser for FASTA files (without using biopython) and am having problems in the following area: I have a list of DNA sequences such as ['AAACCCGAU', 'AUUCCCCCCGGA', 'AACCCGGUU', 'AAACCCCUU'] etc.. named sequence_lines2.我正在尝试为 FASTA 文件设计一个解析器(不使用 biopython)并且在以下方面遇到问题:我有一个 DNA 序列列表,例如 ['AAACCCGAU'、'AUUCCCCCCGGA'、'AACCCGGUU'、'AAACCCCUU']等等..命名为 sequence_lines2。 My target program is: If the element in the list has any multiple of 60 characters, join it to the next element.我的目标程序是:如果列表中的元素有 60 个字符的任意倍数,则将其连接到下一个元素。 This way I can remove the line breaks in FASTA files.这样我可以删除 FASTA 文件中的换行符。 The code I have written looks like this:我编写的代码如下所示:

for el in sequence_lines2:
    if len(el) == 60:
        sequence_lines3 = "".join(el)

How can I make this work?我怎样才能使这项工作? And how to achieve the multiples of 60?又如何达到60的倍数? Thanks in advance!提前致谢!

----Edit---- If anyone's interesting in joining elements, have a look here .. ----编辑----如果有人对加入元素感兴趣,请看这里..

Try with this one liner:试试这个衬里:

result = "".join([el for el in sequence_lines2 if not len(el) % 60])

The len(el) % 60 computes the modulo of length with 60 and if the result is 0, this is a multiple of 60. len(el) % 60计算长度与 60 的模数,如果结果为 0,则为 60 的倍数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM