[英]Python3 How do i use an element from a function in another function
I have a file which looks like this : 我有一个看起来像这样的文件:
OG_100000: V_bacterium_v|198260291 O_bacterium_v|391222558 O_terrae_v|182414991
and i did this function : 我做了这个功能:
def readGroupFile(groupFileName):
dict_gene_taxonomy = {}
fh = open(groupFileName,"r")
for line in fh:
liste = line.split(": ")
groupName = liste[0]
genesAsString = liste[1]
dict_taxon = {}
liste_gene = genesAsString.split()
for item in liste_gene:
taxonomy_gene = item.split("|")
taxonomy = taxonomy_gene[0]
geneId = taxonomy_gene[1]
if taxonomy in dict_taxon:
listeCorrespondantATaxonomy = dict_taxon[taxonomy]
listeCorrespondantATaxonomy.append(geneId)
else:
dict_taxon[taxonomy] = []
dict_taxon[taxonomy].append(geneId)
dict_gene_taxonomy[groupName] = dict_taxon
fh.close()
return dict_gene_taxonomy
I did this function to make dictionaries and split element in each dictionary to be able to reach only the element after the pipe("|"), called geneId. 我执行了此功能,以使字典和每个字典中的元素拆分为仅能到达pipe(“ |”)之后的元素,称为geneId。
Then i made a function to create a Post Request on a internet database with these geneID. 然后,我创建了一个函数,使用这些GeneID在Internet数据库上创建发布请求。 I'm not gonna link the whole function because it works properly when i manually add the geneId in the function's line : 我不会链接整个函数,因为当我在函数行中手动添加geneId时,它可以正常工作:
data = urllib.parse.urlencode(getPost("391222558"))
considering the fact that "391222558" is one geneId from group file, but i need to replace these numbers by every "geneId" in my readGroupFile function. 考虑到“ 391222558”是组文件中的一个GeneId,但我需要用我的readGroupFile函数中的每个“ geneId”替换这些数字。 i can't write : 我不会写:
data = urllib.parse.urlencode(getPost(geneId))
because i'm calling an element in a readGroupFile which is not defined outside this function. 因为我在readGroupFile中调用了一个未在此函数外部定义的元素。 So how can i reach all the "geneId" in my readGroupFile function to add this parameter in getPost(...) so it can work for every geneId in my group file ?? 因此,我如何在我的readGroupFile函数中访问所有“ geneId”,以在getPost(...)中添加此参数,以便它可以用于我的组文件中的每个geneId?
your problem is that you are creating only local variables , and those are deleted when the function end. 您的问题是仅创建局部变量,并且在函数结束时将其删除。
so you got couple of options: 所以你有几个选择:
the first one is define the geneid as a global variable , all you got to do is just: 第一个是将Geneid定义为全局变量,您要做的只是:
global geneid
def readGroupfile():
global geneid
you will have to define him once not in a function and then whenever you use it just write global and his name,and it will make him global variable. 您必须一次不在函数中定义他,然后每当使用它时,只需编写global和他的名字,这将使他成为全局变量。 your secend options is to do the same with your dict , that also contain this variable. 您的secend选项是对也包含此变量的dict进行相同的操作。 i hope i helped you. 我希望我能帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.