问题
I want to define a global list and append a list to it. I am getting a list (i[0]) by some on click event and appended that to mnum_list. Now i want create a global list and append that mnum_list to it. Any idea how to do this?
def OnClick(self, event):
name = event.GetEventObject().GetLabelText()
cursor= self.conn.execute("SELECT * FROM ELEMENT where SYMBOL==?", (name,))
elements = cursor.fetchall()
print elements
cursor= self.conn.execute("SELECT ATOMIC_NUMBER FROM ELEMENT where SYMBOL = ?", (name,))
numbers = cursor.fetchone()[0]
print numbers
atomicnumber = numbers
cursor= self.conn.execute("SELECT MOL_NUMBER FROM LINK where ELEMENT_NUMBER = ?", (atomicnumber,))
mnumbers = cursor.fetchall()
print mnumbers
mnum_list = []
for i in mnumbers:
mnum_list.append(i[0])
print mnum_list
回答1:
There is no need in global
statement if there is no assignment, just:
def foo(x):
sublist = range(x)
glist.append(sublist)
and in case of extension:
def foo(x):
sublist = range(x)
glist.extend(sublist)
回答2:
You can declare it on the file/module level like: my_global_list = list()
and when you want to append to it inside a function you can use the global keyword. The global keyword tells python to look for the global variable.
global my_global_list
my_global_list.append()
来源:https://stackoverflow.com/questions/44645513/how-to-define-a-global-list-in-python-and-append-local-list-to-it