首页 / PYTHON / 了解Python函数
了解Python函数
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了了解Python函数,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4279字,纯文字阅读大概需要7分钟。
内容图文
![了解Python函数](/upload/InfoBanner/zyjiaocheng/662/6f0582fad0474838a12c62bfd97b676f.jpg)
我需要一些帮助来理解我要使用的功能,但是我不确定它的某些部分在做什么.我了解该功能是根据从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)
附加信息:读取= readFasta(“ smallReads.fna”,详细= 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’”?仍然对价值感到困惑.
在另一个函数中,有类似的行:如果dict中的read [-lenKeys:]:
“-”是做什么的?
def makeSuffixDict(reads, lenSuffix = 20, verbose = True):
在这里,我不了解参数:读数如何成为参数?除了从len(reads [0])中减去的值之外,在此函数的上下文中lenSuffix = 20是什么?
什么是冗长的?我已经读过一个忽略空格的“详细模式”,但我从未见过它用作参数,后来又用作变量.
解决方法:
您的问题语气使我觉得您在将程序功能(len,函数等)与原始程序员定义的内容(读取的类型,详细的内容等)混淆.
def some_function(these, are, arbitrary, parameters):
pass
此函数定义了一堆参数.除了我隐含地给予他们的价值之外,他们根本没有任何意义.例如,如果我这样做:
def reverse_string(s):
pass
s可能是字符串,对吗?在您的示例中,我们有:
def makeSuffixDict(reads, lenSuffix = 20, verbose = True):
lenKeys = len(reads[0]) - lenSuffix
...
从这两行我们可以推断出几件事:
>该函数可能会返回一个字典(从其名称)
> lenSuffix是一个int,冗长的是bool(根据其默认参数)
>读取可以索引(字符串?列表?元组?)
>读取中的项目具有长度(字符串?列表?元组?)
由于Python是动态类型的,因此到目前为止,我们只能了解该函数.其余的将通过其文档或调用方式进行解释.
就是说:让我按顺序涵盖您的所有问题:
>值[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
>据我了解,“ 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
, alsolen([1, 2, 3]) == 3
. Note thatlen(['abc']) == 1
, since it is measuring the length of the list, not the string inside it.
>为什么“读取”会自动生成列表?
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!
>(有关切片的各种问题)
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.
>如何读取为参数?
Because the function is defined as
makeSuffixDict(reads, ...)
. That’s what a parameter is.
>在此函数的上下文中,lenSuffix = 20是什么
Looks like it’s the length of the expected suffix!
>什么是冗长的?
verbose
has no meaning on its own. It’s just another parameter. Looks like the author included theverbose
flag so you could get output while the function ran. Notice all theif verbose
blocks seem to do nothing, just provide feedback to the user.
内容总结
以上是互联网集市为您收集整理的了解Python函数全部内容,希望文章能够帮你解决了解Python函数所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。