0

我想在我正在编写的程序中使用一个列表。基本上,它是一个包含不同人信息的元组列表,每个人的信息(姓名、电话、地址等)都存储在一个元组中。我通过一个初始函数定义了这个列表,但我需要在我的交互函数以及其他函数中使用它。

我的问题是,我是否可以使用此列表而不将其定义为全局变量?

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: ')  

我还应该提到,有一个命令可以解析使用输入以执行功能。这会影响列表的使用吗?

4

4 回答 4

1

您可以list在调用它的第一个函数中声明并从那里返回它,后面的函数应该接收 thislist作为参数。

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()
于 2013-04-09T14:10:53.877 回答
0

您可以执行以下操作:

# 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()

我不太明白你问题的最后一部分,但这两种方法中的一种就是你需要的。

于 2013-04-09T14:03:40.837 回答
0

是的。在函数之间来回传递“朋友列表”作为参数。

load_friends()会成为

def load_friends(filename):
    import csv

    with open(filename, 'Ur') as f:
        return map(tuple, csv.reader(f, delimiter=","))

add_friend()很接近,但分配给new_list是不必要的,因为list.append()改变了现有的列表:

def add_friend(friend_info, friend_list):
    friend_list.append(friend_info)

就足够了。

interact()也会有friends_list争论。

def interact(friends_list):
    #interaction stuff here...

你可以这样称呼它:

interact(load_friends("myfile.csv"))
于 2013-04-09T14:12:02.600 回答
0

类对这种事情很有用,并且易于使用:

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()函数中,PersInfo对象被实例化。这可能比您想要的要多,但从长远来看,这是组织代码并保持可读性的好方法。

此外,您可以infoList直接在您正在创建的地方构建我制作的person_list.

于 2013-04-09T16:09:07.057 回答