如何将列表附加到另一个列表中的项目,并在模拟中将这些项目留空?

How can I append lists with items from another list and leave these empty in a simulation?

这是我的代码:

import random
listx = []
ready = 0
listy = []
listz = []

#function

def function(r,s,q):
    listy=[]
    if len(listx)==4 or len(listx)==8:
        listy.append((listx, t))
        print "y", listy

# here it starts:

for t in range(1,25): 

    randomnumber = random.uniform(0.0, 1.0)

    if randomnumber <= 0.5:
        listx.append((t))   
        print "x", listx

    if ready == 0: #condition, lets say: ready is always 0
        function(5,6,8) #this function generates listy from input of listx

    if listy != 0: #if listy is not empy anymore, fill listz with items of listy
            listz.append(listy)
            del listy
            print "z", listz

我有 3 个列表; listxlistylistz。我生成随机数 listx。如果 ready==0(总是)我调用函数 (function(r,s,q))。如果满足条件 (len==4 or 8),列表中的项目将附加到 listy。

此时,我想将这些数字从 listy 添加到 listz 并再次将 listy 留空。我从 listy 运送到 listz 的物品应该从 listx 中移除。

有人知道怎么解决吗?

我想这可能是你想要的。 # UPPERCASE COMMENTS.

表示对代码的更改
import random
listx = []
ready = 0
listy = []
listz = []

#function

def function(r,s,q):
    global listy  #### ADDED

    listy=[]
    if len(listx) in (4, 8):  # STREAMLINED
        listy.append((listx, t))
        print "y", listy

# here it starts:

for t in range(1,25):

    randomnumber = random.uniform(0.0, 1.0)

    if randomnumber <= 0.5:
        listx.append((t))
        print "x", listx

    if ready == 0: # condition, lets say: ready is always 0
        function(5,6,8) # this function generates listy from input of listx

    #### REWRITTEN
    if listy: # not empty?
        listz += listy[0][0]  # copy numbers from listy to listz
        del listx[:len(listy[0][0])]  # remove numbers transported from listx
        del listy  # empty listy
        print "z", listz