gpt4 book ai didi

python - 了解 Python 函数

转载 作者:行者123 更新时间:2023-11-28 16:32:21 30 4
gpt4 key购买 nike

我需要一些帮助来理解我想使用的函数,但我不完全确定它的某些部分是做什么的。我知道该函数是通过读取 Fasta 文件来创建字典。据我了解,这应该为最终扩展重叠群(重叠的 dna 序列)生成前和后缀词典。代码:

def makeSuffixDict(reads, lenSuffix = 20, verbose = True):
lenKeys = len(reads[0]) - lenSuffix
dict = {}
multipleKeys = []
i = 1
for read in reads:
if read[0:lenKeys] in dict:
multipleKeys.append(read[0:lenKeys])
else:
dict[read[0:lenKeys]] = read[lenKeys:]
if verbose:
print("\rChecking suffix", i, "of", len(reads), end = "", flush = True)
i += 1
for key in set(multipleKeys):
del(dict[key])
if verbose:
print("\nCreated", len(dict), "suffixes with length", lenSuffix, \
"from", len(reads), "Reads. (", len(reads) - len(dict), \
"unambigous)")
return(dict)

附加信息:reads = readFasta("smallReads.fna", verbose = True)

函数是这样调用的:

if __name__ == "__main__":
reads = readFasta("smallReads.fna", verbose = True)
suffixDicts = makeSuffixDicts(reads, 10)

smallReads.fna 文件包含碱基串 (Dna):

"> read 1

TTATGAATATTACGCAATGGACGTCCAAGGTACAGCGTATTTGTACGCTA

"> read 2

AACTGCTATCTTTCTTGTCCACTCGAAAATCCATAACGTAGCCCATAACG

"> read 3

TCAGTTATCCTATATACTGGATCCCGACTTTAATCGGCGTCGGAATTACT

以下是我不明白的部分:

lenKeys = len(reads[0]) - lenSuffix

值 [0] 是什么意思?据我了解,“len”返回列表中的元素数。为什么“阅读”会自动成为一个列表?编辑:似乎可以将 Fasta 文件声明为列表。有人可以证实吗?

if read[0:lenKeys] in dict:

这是否意味着“从 0 到‘lenKeys’”?仍然对值(value)感到困惑。在另一个函数中有类似的行:if read[-lenKeys:] in dict:“-”是做什么的?

def makeSuffixDict(reads, lenSuffix = 20, verbose = True):

这里的参数我不明白:reads怎么能是参数呢?除了从 len(reads[0]) 中减去的值之外,此函数上下文中的 lenSuffix = 20 是什么?什么是冗长?我读过有关忽略空格的“详细模式”的信息,但我从未见过它被用作参数,后来又被用作变量。

最佳答案

你问题的语气让我觉得你混淆了程序特性(len、函数等)和原始程序员定义的东西( 的类型)阅读冗长等)。

def some_function(these, are, arbitrary, parameters):
pass

这个函数定义了一堆参数。除了我暗中赋予它们的值(value)外,它们没有任何意义。例如,如果我这样做:

def reverse_string(s):
pass

s 应该是字符串吧?在您的示例中,我们有:

def makeSuffixDict(reads, lenSuffix = 20, verbose = True):
lenKeys = len(reads[0]) - lenSuffix
...

从这两行我们可以推断出一些事情:

  • 该函数将可能返回一个字典(根据其名称)
  • lenSuffix 是一个 int,而 verbose 是一个 bool(来自它们的默认参数)
  • reads 可以被索引(字符串?列表?元组?)
  • reads 中的项目有长度(字符串?列表?元组?)

由于 Python 是动态类型的,所以这是目前我们所能知道的关于函数的信息。其余的将通过其文档或调用方式进行解释。

就是说:让我按顺序回答您所有的问题:

  1. 值 [0] 是什么意思?

some_object[0] is grabbing the first item in a container. [1,2,3][0] == 1, "Hello, World!"[0] == "H". This is called indexing, and is governed by the __getitem__ magic method

  1. 据我所知,“len”返回列表中元素的数量。

len is a built-in function that returns the length of an object. It is governed by the __len__ magic method. len('abc') == 3, also len([1, 2, 3]) == 3. Note that len(['abc']) == 1, since it is measuring the length of the list, not the string inside it.

  1. 为什么“阅读”会自动成为一个列表?

reads is a parameter. It is whatever the calling scope passes to it. It does appear that it expects a list, but that's not a hard and fast rule!

  1. (关于切片的各种问题)

Slicing is doing some_container[start_idx : end_idx [ : step_size]]. It does pretty much what you'd expect: "0123456"[0:3] == "012". Slice indexes are considered to be zero-indexed and lay between the elements, so [0:1] is identical to [0], except that slices return lists, not individual objects (so 'abc'[0] == 'a' but 'abc'[0:1] == ['a']). If you omit either start or end index, it is treated as the beginning or end of the string respectively. I won't go into step size here.

Negative indexes count from the back, so '0123456'[-3:] == '456'. Note that [-0]is not the last value,[-1]is. This is contrasted with[0]` being the first value.

  1. reads如何成为参数?

Because the function is defined as makeSuffixDict(reads, ...). That's what a parameter is.

  1. 在此函数的上下文中 lenSuffix = 20 是什么

Looks like it's the length of the expected suffix!

  1. 什么是verbose

verbose has no meaning on its own. It's just another parameter. Looks like the author included the verbose flag so you could get output while the function ran. Notice all the if verbose blocks seem to do nothing, just provide feedback to the user.

关于python - 了解 Python 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30584108/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com