如何声明python全局列表 [英] How to declare python global list

查看:209
本文介绍了如何声明python全局列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在声明列表时遇到一些问题.看来我无法删除列表中的所有元素.在reset_a打印输出中,它显示一个空列表,但在检查myFunction中的列表之后,前一个元素仍在列表中.如何声明全局列表并在定义的其他所有列表中使用它?

I'm having some problems declaring a list. It seems like I cannot delete all the elements in the list. At the reset_a printout, it display an empty list but after another around of checking the list in myFunction, the previous element is still in the list. How can I declare a global list and use it all over others defined?

a = []

def reset_a():
    global a
    del a[:]
    print a

def myFunction():
     global a
     #check other stuffs........... then..
     print a
     if data not in a:
         a.append(data)
         time.sleep(5)
         reset_a()

if __name__=='__main__':
     while True:
        myFunction()

我找到了一种方法来完成它.

Edited: I found a way to get it done.

global a
a = []

def reset_a():
    del a[:]
    print a

def myFunction():
     #check other stuffs...........and get 'data' then..
     print a
     if data not in a:
         a.append(data)
         time.sleep(5)
         reset_a()

if __name__=='__main__':
     while True:
        myFunction()

推荐答案

rest_a()仅在a

这篇关于如何声明python全局列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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