在python中的不同函数中使用列表
我想在我正在编写的程序中使用列表。基本上,它是一个充满元组的列表,其中包含关于不同人员的信息,每个人的信息(姓名、电话、地址等)存储在元组中。我通过一个初始函数来定义这个列表,但是我需要在我的交互函数和其他函数中使用它 我的问题是,我是否可以使用此列表而不将其定义为全局变量在python中的不同函数中使用列表,python,list,function,global-variables,Python,List,Function,Global Variables,我想在我正在编写的程序中使用列表。基本上,它是一个充满元组的列表,其中包含关于不同人员的信息,每个人的信息(姓名、电话、地址等)存储在元组中。我通过一个初始函数来定义这个列表,但是我需要在我的交互函数和其他函数中使用它 我的问题是,我是否可以使用此列表而不将其定义为全局变量 def load_friends(filename): """imports filename as a list of tuples using the import command""" import c
def load_friends(filename):
"""imports filename as a list of tuples using the import command"""
import csv
with open(filename, 'Ur')as filename:
friends_list = list(tuple(x) for x in csv.reader(filename, delimiter=','))
def add_friend(friend_info, friends_list):
"""appends the friend_info tupple to the list friends_list"""
new_list = friends_list.append(friends_info)
def interact():
"""interaction function: accepts user input commands"""
while True:
command = raw_input('Command: ')
我还应该提到,有一个命令可以解析使用输入来执行函数。这会影响列表的使用吗?您可以执行以下操作:
# you can define the list here, it's global but doesn't require the keyword
my_list_globally = []
def func1(the_list):
pass
def func2(the_list):
pass
def func3(the_list):
pass
# you can use a hub function to pass the list into things that need it
def main():
my_list = []
func1(my_list)
func2(my_list)
func3(my_list)
if __name__ == "__main__":
main()
我不太明白你问题的最后一部分,但这两种方法之一就是你所需要的。你可以在调用它的第一个函数中声明
list
,然后从那里返回,后面的函数应该接收这个list
作为参数
def func1():
my_list=[]
"""Do stuff
"""
return list
def func2(my_list):
"""Do stuff with my_list
"""
return
def func3(my_list):
"""Do stuff with my_list
"""
return
def main():
"""First we retrieve the list from func1,
func2/3 get it passed to them as an argument
"""
foo=func1
func2(foo)
func3(foo)
if __name__ == "__main__":
main()
对。在函数之间来回传递“朋友列表”作为参数
def interact(friends_list):
#interaction stuff here...
load\u friends()
将成为
def load_friends(filename):
import csv
with open(filename, 'Ur') as f:
return map(tuple, csv.reader(f, delimiter=","))
add\u friend()
已关闭,但不需要将该赋值给new\u list
,因为list.append()
会使现有列表发生变化:
def add_friend(friend_info, friend_list):
friend_list.append(friend_info)
就够了
interactive()
还将有一个friends\u列表
参数
def interact(friends_list):
#interaction stuff here...
你可以这样称呼它:
interact(load_friends("myfile.csv"))
类对于这类事情非常有用,并且易于使用:
class PersInfo:
def setName(self, name):
self._name = name
def getName(self):
return self._name
def setNumber(self, number):
self._phNumber = number
def getNumber(self):
return self._phNumber
def setAddr(self, address):
self._address = address
def getAddr(self)
return self._address
def main():
# Read in data from your CSV Here
infoList = ([])
for person in person_list: # Assuming person is a tuple here
foo = PersInfo()
foo.setName(person[index1])
foo.setNumber(person[index2])
foo.setAddr(person[index3])
infoList.append(foo)
# To access the info...
for person in infoList:
print(person.getName())
print(person.getNumber())
print(person.getAddr())
你最终的名单是“全球”的,有点像。它位于main()
函数中,在这里实例化perinfo
对象。这可能超出了您的期望,但从长远来看,这是一种组织代码并保持可读性的好方法
此外,您还可以在创建
person\u list
的地方直接构建infoList
,您可以从load\u friends
返回friends\u list
并将其作为参数传递给其他函数,就像在add\u friends
中一样。这些getter和setter是怎么回事?这是Python,不是Java,正如前面所说的。是的,你是对的@DSM。您是否建议更好的代码直接访问属性?比如foo.name='someString'
。不要使用list
作为变量名,即使是作为一个例子