繁体   English   中英

如何在函数中使一个默认的 emtpy 列表等于另一个?

[英]How to make one default emtpy list equal another in a function?

我正在尝试利用现有列表将其值添加/复制/等于我拥有的另一个空默认列表。 这样做的目的是让我有一个默认列表,我可以在以后提出反对意见。

Amazon_Books_Old = ['Mary','Bob','Chris','Robert']
Active_List = []
def preview_button_execute():
    if variable.get() == "Amazon Books" and varChoice.get() == False:
        Active_List == Amazon_Books_Old
        Preview_Window()
#This executes the function defined above
Preview_Button = Button(Preview_Export_Label,text='Preview',command=preview_button_execute)
Preview_Button.pack(fill=X)
#This operates on the Active_List which should be updated with the new list data
label0=Label(Pre_Win,text="")
label0.grid(column=2,row=1)
label0['text'] = Active_List[0]

除了让列表Active_List等于Amazon_Books_Old或通过其他列表复制方法包含其值Amazon_Books_Old ,我希望此代码块执行的所有操作都有效。 list.copy(old_list)和其他方法似乎也不起作用。 对于上下文, .get()调用基于 Tkinter 小部件,与我遇到的问题无关。

附加信息:建议的其他方法,例如 Active_List = Amazon_Books_Old.copy() 导致 label0['text'] = Active_List[0] IndexError: list index out of range。

我想到了:

def preview_button_execute():
if variable.get() == "Amazon Books" and varChoice.get() == False:
    global Active_List
    Active_List = Amazon_Books_Old.copy()
    Preview_Window()

确实,我不是最好的程序员。

我在发布之前尝试过的 Active_List = Amazon_Books_Old.copy()(并且被评论者正确建议)可能确实有效,但没有像我想要的那样在函数之外更新 Active_List。

我一直避免使用 Global 语句,因为有人告诉我不要乱用它们。 (?)

感谢那些参与这次学习体验的人。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM