2016-11-16 78 views
-6

我最近开始使用Python 3.5.2编程(我大约三年前学过C++,但从那时起我还没有使用它),我无法理解'.append()'何时使用append?

也许问题是我不是英语母语的人。

有人可以向我解释这个概念吗?

编辑:谢谢。我不能让这个代码工作。基本上,我希望用户输入日,月,年并将它们保存到GDO中。我的错误是什么?

from tkinter import * 


root = Tk() 
root.title("Calendar") 
root.geometry("300x300") 

GDO1 = ['Day', 'Month', 'Year'] 
GDO = [] 
for w in range (3): 

    en = Entry(root) 
    lab = Label(root, text = GDO1[w]) 
    lab.grid(row=w+1, column=0, sticky = W) 
    en.grid(row=w+1, column=1, sticky = W) 
    GDO.append(en) 

buttonGDO = Button (root, text="Submit", command=GDO.append(en) and print (GDO)) 
buttonGDO.grid(row=4) 


root.mainloop 
+5

这真的看起来一个典型的案例RTFM ... –

回答

3

你有例如列表[1,2,3] 如果要添加其他元素使用追加:

list = [1, 2, 3] 
list.append(4) 
2

追加功能追加对象到现有列表。

参见文档:list.append

编辑: 在你的具体的例子,这个问题是不是追加。 mainloop是一个函数调用,所以你需要调用它这样,用括号:

root.mainloop()

2

追加是非常简单的,它只是增加了,或者追加,一个值的列表。

>>> list = ['one', 'two', 'three'] 
>>> list 
['one', 'two', 'three'] 
>>> list.append('four') 
>>> list 
['one', 'two', 'three', 'four'] 
5
consider if you have List = [1,2,3,4] 
#append function - Adds an item to the end of the list. 
>>>L = [1,2,3,4] 
>>>L.append(5) 
>>>print(L) 
>>>[1,2,3,4,5]