2013-04-09 69 views
0

我想在我正在編寫的程序中使用列表。基本上,它是一個充滿元組的列表,其中包含有關不同人的信息,每個人的信息(姓名,電話,地址等)都以元組形式存儲。我通過初始函數來定義這個列表,但我需要在我的交互函數以及其他函數中使用它。在python中使用不同函數的列表

我的問題是,是否有可能使用此列表而不將其定義爲全局變量?

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

我還要提到的是有解析使用輸入來執行的功能的命令。這會影響列表的使用嗎?

+0

您可以從'load_friends'返回'friends_list',並將其作爲參數傳遞給其他函數,就像您在'add_friend'中做的一樣。 – Moshe 2013-04-09 14:04:44

回答

1

你可以在第一個調用它的函數中聲明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() 
+0

感謝您的幫助!像一個待遇 – Yeto 2013-04-10 00:33:34

+1

不要使用'list'作爲變量名,即使是一個例子 – jamylak 2013-04-10 10:30:21

+0

好點,我會修改那個 – 2013-04-10 11:39:33

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

我不太明白你的問題,但這些2種方式將是你需要一個什麼樣的最後一部分。

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")) 
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的位置。

+0

所有這些getters和setters是什麼?這是Python,而不是Java,因爲[man](http://dirtsimple.org/2004/12/python-is-not-java.html)說。 – DSM 2013-04-09 16:13:01

+0

是的,你是對的@DSM。你是否建議更好的代碼直接訪問屬性?像'foo.name ='someString''。 – mrKelley 2013-04-10 17:23:35