issue with updating list every iteration of a loop - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: issue with updating list every iteration of a loop (/thread-13723.html) |
issue with updating list every iteration of a loop - ftrillaudp - Oct-28-2018 Dear all, I have an issue when I want to update the content of lists. I have two loops, a for loop where I initiate two lists and an inner while loop where I filled up the lists. During the first iteration of the external for loop, it works fine, then during the second iteration, the lists in the inner while loop are empty. I am attaching the code related to the issue, the lists are listOfResultsCCD and listOfResultsDetector. The tow lists are filled up calling two external functions found in user module. I cannot upload all the code because of its size. The error message is the following: $ python main.py Number of samples 0 CCD+Detector: Iter: 26, Er: 9.1e-06 Number of samples 1 CCD+Detector: Iter: 0, Er: 9.1e-06 Traceback (most recent call last): File "main.py", line 244, in <module> listTcc.append(listOfResultsDetector[-6]) IndexError: list index out of range The list at the second iteration is actually empty []. Here is the piece of code. ### MAIN ### listTcc = list() listTcf3 = list() listTcf2 = list() listTcf1 = list() listTbx0 = list() listQdetector = list() listTfl = list() listTsb = list() listTccd = list() listQccdPackage = list() er = 100; epsi = 0.00001; iter_max = 1000; for i in range(0,numberOfSamples-1): Para_ccd[1] = Wccdsb[i] Para_ccd[2] = Wsbfl[i] Para_ccd[3] = Wflsp[i] Para_ccd[4] = Wsbsp[i] Para_ccd[5] = Wspbx[i] Para_det[5] = Wbx0cf1[i] Para_det[6] = Wcf3cc[i] print("Number of samples", i) count = 0 Tbx = Para[16] ### Initial value of T_bx # lists not updated during second iteration of for loop listOfResultsCCD = list() # reinitialization every "for" iteration listOfResultsDetector = list() # reinitialization every "for" iteration while ((er > epsi) and (count < iter_max)): Tbx_old = Tbx Para_ccd[-1] = Tbx listOfResultsCCD= ccdPackagesModule.func_CCDPackage(Para_ccd) Tsp = listOfResultsCCD[-4] Para_det[-1] = Tsp listOfResultsDetector = detectorModule.func_detector(Para_det) Tbx = listOfResultsDetector[-2] er = abs((Tbx-Tbx_old)/Tbx_old) count = count+1 #print("\n***************************************") #print("Main loop: CCD and detector") #print("CCD+Detector: Iter: {:2d}, Er: {:1.2g}".format(count, er)) print("CCD+Detector: Iter: {:2d}, Er: {:1.2g}".format(count, er)) listTcc.append(listOfResultsDetector[-6]) listTcf3.append(listOfResultsDetector[-5]) listTcf2.append(listOfResultsDetector[-4]) listTcf1.append(listOfResultsDetector[-3]) listTbx0.append(listOfResultsDetector[-2]) listQdetector.append(listOfResultsDetector[-1]) listTfl.append(listOfResultsCCD[-4]) listTsb.append(listOfResultsCCD[-3]) listTccd.append(listOfResultsCCD[-2]) listQccdPackage.append(listOfResultsCCD[-1])Best regards, Ftrillaudp RE: issue with updating list every iteration of a loop - ichabod801 - Oct-28-2018 Please use python tags when posting code. I put them in for you this time. See the BBCode link in my signature below for instructions. This looks more like an assumptions problem than a loop problem. It looks like detectorModule.func_detector on line 37 is just returning a shorter list than you expect. I would look to see what exactly is being returned there. RE: issue with updating list every iteration of a loop - ftrillaudp - Oct-29-2018 Thanks for the tags, I am new to this. I found the error and can close the post. line 30, I am missing the definition of "er", I added it and it works fine. Best, Ftrillaudp |