Python Forum
issue with updating list every iteration of a loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
issue with updating list every iteration of a loop
#1
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
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List Comprehension Issue johnywhy 5 440 Jan-14-2024, 07:58 AM
Last Post: Pedroski55
  Updating Code And Having Issue With Keys Xileron 8 1,086 May-25-2023, 11:14 PM
Last Post: DigiGod
  Inconsistent loop iteration behavior JonWayn 2 954 Dec-10-2022, 06:49 AM
Last Post: JonWayn
  Python List Issue Aggie64 5 1,564 Jun-30-2022, 09:15 PM
Last Post: Aggie64
  Updating nested dict list keys tbaror 2 1,243 Feb-09-2022, 09:37 AM
Last Post: tbaror
  List Iteration Help javeeva 2 1,145 Feb-07-2022, 11:23 PM
Last Post: snippsat
  List to table issue robdineen 2 1,435 Nov-07-2021, 09:31 PM
Last Post: robdineen
  Looping through nested elements and updating the original list Alex_James 3 2,070 Aug-19-2021, 12:05 PM
Last Post: Alex_James
  saving each iteration of a loop sgcgrif3 3 6,642 Jul-27-2021, 01:02 PM
Last Post: DeaD_EyE
  Calculator code issue using list kirt6405 4 2,203 Jun-11-2021, 10:13 PM
Last Post: topfox

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020