嵌套while循环的正确结构

时间:2014-08-07 11:55:38

标签: python numpy dynamic-arrays

我试图用基于np.append中规定的标准选择的数据行填充2D数组。 Python似乎并没有抱怨我所做的事情,但是我认为嵌套错误并且循环卡住了。我不确定它有什么问题。我目前的想法是,我在Python中构建嵌套while循环的方式有些不对。如果有人能指出错误,我将不胜感激。

nrows = 132
scan_length = 22
fulldata = fulldatat[0:0] # The actual data array of shape (528,32768)
ch = 0
while ch <= 3:
    n = 1
   while n <= nscans:
     fulldata = np.append(fulldata, fulldatat[ch*nrows:ch*nrows+scan_length*n],axis=0)
   n += 1
ch += 1

2 个答案:

答案 0 :(得分:1)

对于这种类型的循环,

“for”比“while”更合适:

nrows = 132
scan_length = 22
fulldata = fulldatat[0:0] # The actual data array of shape (528,32768)
for ch in range(4):
   for n in range(1, nscans+1):
       fulldata = np.append(fulldata, fulldatat[ch*nrows:ch*nrows+scan_length*n],axis=0)

答案 1 :(得分:0)

你应该试试这个:

nrows = 132
scan_length = 22
fulldata = fulldatat[0:0] # The actual data array of shape (528,32768)
ch = 0
while ch <= 3:
    n = 1
    while n <= nscans:
       fulldata = np.append(fulldata, fulldatat[ch*nrows:ch*nrows+scan_length*n],axis=0)
       n += 1
    ch += 1

Code indentation需要关注。

相关问题