简体   繁体   English

Python3如何在另一个函数中使用一个函数中的元素

[英]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.

相关问题 使用来自另一个 function python3 的值 - Use values from another function python3 如何在 python3 中正确使用来自 BeatifulSoup4 的 find function? - How do I properly use the find function from BeatifulSoup4 in python3? 如何使用python中另一个函数的局部变量? - How do I use local variables from another function in python? 如何在python中的另一个函数中使用一个函数中的数据? - How do I use data from one function in another function in python? Python3:如何将一个函数内的一个函数调用到另一个函数,该如何做? - Python3: Calling a function within a function to another function, how? 在调用函数时如何防止GUI冻结? (PyQT4,Python3) - How do I keep GUI from freezing, while calling a function? (PyQT4, Python3) 如何将变量从一个 function 传递到另一个,并在 python 的条件语句中使用它们? - How do I pass variables from one function to another and use them in conditional statements in python? python - 如何在不使用返回的情况下从一个函数中获取信息以在python中的另一个函数中使用? - How do I take information from one function to use in another in python without using return? 我如何从一个 function 中取出字典并在另一个 python 中使用它? - How do I take a dictionary from one function and use it in another in python? 如何在Python3中使用sum函数 - How to use the sum function in Python3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM