如何在 python 中保存每个 while 循环的结果?

How to save results for each while loop in python?

正如您从我的代码中看到的那样,将有 150 个 dx 值,例如 dx(1) for j=50,dx(2) for j=51---dx(150) for j=149。我想在 "foobar.xls" 文件的一列中写入所有 dx(最多 149)值。目前,我在xls文件中发现,只有dx(150)的值。所以这是我的问题。抱歉提问不当。 (我是新来的)。

import matplotlib.pyplot as plt
import numpy as np 
import xlrd
import xlwt


wb = xlrd.open_workbook('Scatter plot.xlsx')
sh1 = wb.sheet_by_name('T180')
sh2=wb.sheet_by_name("T181")
sh3=wb.sheet_by_name("Sheet1")


x= np.array([sh1.col_values(1,start_rowx=50, end_rowx=299)])
x1= np.array([sh2.col_values(1, start_rowx=48, end_rowx=200)])

print x1

j=50

while j<200:
    y=np.array([sh1.cell_value(j,1)])
    condition = [(x1<=(sh1.cell_value(j,1)+100)) & (x1>=(sh1.cell_value(j,1)-200)) ]
    dx=y-x1[condition]
    j+=1
    print dx

workbook = xlwt.Workbook() 
sheet = workbook.add_sheet("Sheet1")

i=1
for n in dx:
    sheet.write(i, 0,n)
    i=i+1

workbook.save("foobar.xls")
Its working like this way. Thanks for our nice comments and helpful mind.

j=50
i=1
while j<300:
    x=np.array([sh1.cell_value(j,1)])
    condition = [(x1<=(sh1.cell_value(j,1)+100)) & (x1>=     (sh1.cell_value(j,1)-200)) ]
    dx=x-x1[condition]
    j+=1
       for n in dx:
        sheet.write(i, 0,n)
        i+=1
     print dx