这是我的代码:
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个名单; listx
,listy
和listz
。我生成随机数到listx
。如果ready==0
(总是)我调用函数(function(r,s,q))
。如果满足条件(len==4 or 8)
,则列表中的项目将附加到listy。
此时,我想将这些数字从listy
添加到listz
并再次将listy
留空。我应该从listy
移除从listz
传输到listx
的商品。
有谁知道如何解决这个问题?
答案 0 :(得分:0)
我想这可能是你想要的。代码更改以# 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