Python return list from function

后端 未结 8 955
面向向阳花
面向向阳花 2020-12-13 08:27

I have a function that parses a file into a list. I\'m trying to return that list so I can use it in other functions.

def splitNet():
    network = []
    f         


        
8条回答
  •  星月不相逢
    2020-12-13 09:08

    If you want to return an item or list from a definition, you could define it before hand and use it as a variable during the initial writing of said definition. Unless it has to be defined within the definition. In this case you won't need to write in a return command at the end.

    network = []
    
    def splitNet(network):
        for line in open("/home/tom/Dropbox/CN/Python/CW2/network.txt","r").readlines():
            line = line.replace("\r\n", "")
            line = string.split(line, ',')
            line = map(int, line)
            network.append(line)
    
    print network # Will print the list you've appended. But it is now a usable object. 
    

提交回复
热议问题