如何在python中定义全局列表并将本地列表附加到它 [英] How to define a global list in python and append local list to it

查看:49
本文介绍了如何在python中定义全局列表并将本地列表附加到它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想定义一个全局列表并向其附加一个列表.我通过点击事件获取列表 (i[0]) 并将其附加到 mnum_list.现在我想创建一个全局列表并将该 mnum_list 附加到它.知道如何做到这一点吗?

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

推荐答案

global 语句中不需要赋值,只要:

There is no need in global statement if there is no assignment, just:

def foo(x):
    sublist = range(x)
    glist.append(sublist)

以及在扩展的情况下:

def foo(x):
    sublist = range(x)
    glist.extend(sublist)

这篇关于如何在python中定义全局列表并将本地列表附加到它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆