【发布时间】:2022-01-12 13:30:28
【问题描述】:
我有这段代码,是根据这里的一些帖子拼凑而成的。它采用 FASTA 文件(包含 DNA 序列的文件)并查找分子量在给定重量范围内的序列。为此,它使用由先前构建的函数 seq_ID_and_weight 生成的字典,该函数(顾名思义)输出文件中序列的 ID 及其分子量的最小值和最大值(序列可能不明确,因此有很多可能的权重)。
下面的函数做了我需要它做的事情,但我不确定如何做。
def find_sequence(file_name, min_weight, max_weight):
ID_list=[] # Initialize a list to store seq IDs
with open (file_name) as file:
dictionary = (seq_ID_and_weight(file_name))
for k,v in dictionary.items(): # This function lets you traverse the dictionary
for i in range(min(2,len(v))):
if v[i]>min_weight and v[i]<max_weight: # If value is within given range, append the sequence_id to list.
ID_list.append(k)
break
return ID_list
直到“for i in range”行为止,我都理解。我知道那行是因为我必须处理具有两个值的键以及只有一个值的键。但是 min 函数有什么作用呢?为什么我使用 i 作为变量?
对不起,如果这是一个愚蠢的问题,但我是 Python 新手。
【问题讨论】:
-
考虑
min(2, len(v))的结果。如果len(v)小于2,则结果为len(v)。否则为 2。然后将该值传递给range。
标签: python dictionary fasta