0

I'm trying to manipulate text from a word file however when I save it to an array of classes, all the indexes are being overwritten instead of the one particular index I intend to change.

for line in modified:
  
  if line.startswith('Date'):
    output.append(line)
    list2=line.split(' ')
    work.date=list2[1]
   # print(work.date)
  if line.startswith('PPV'):            #list1[2]=l,[3]=t,[4]=v
    output.append(line)
    list1=line.split(' ')
    work.lpv=list1[2]
   # print("l is ",list1[2],work.lpv)
    work.tpv=list1[3]
   # print("t is ",list1[3],work.tpv)
    work.vpv=list1[4]
   # print("v is ",list1[4],work.vpv)
    daylist[count]=work  
    #print("l2 is ",list1[2],work.lpv)
    #print("daylist", count, "saved")
    
    print(count,daylist[count].date)  #this displays the correct value at the propper index but all other indexs have also been changed to this value
    count+=1
    

Im trying to save a class which holds a string and a few floats to an array but cannot seem to get it to save to each index properly as it is read from the file. ive tried messing with the scope and list initialization but cant seem to figure it out. Any input would be appreciated, Thanks!

9
  • count is getting incremented only when line.startswith('PPV') is this what you want? consider to use enumerate function Commented Nov 15, 2022 at 17:03
  • daylist[count]=work : This assigns the same object (work) to each element in daylist so natually they'll all have the same value. Looks like you want to assign a modified copy of work instead. Commented Nov 15, 2022 at 17:06
  • yes that should be fine there is a date and corresponding ppv values to each date that should be saved to a class of each index at the array. when printing the count it displays the desired index. thats why im so confused as to how all indexes are being overwritten. Commented Nov 15, 2022 at 17:07
  • woodford, wouldnt daylist[count]=work only assign work to the index of count? work should change for every loop and then be assigned to the index "count". How would i go about a modified copy? Commented Nov 15, 2022 at 17:10
  • Does this answer your question? List of lists changes reflected across sublists unexpectedly Commented Nov 15, 2022 at 17:12

1 Answer 1

0

Every index in the daylist array references the same work object. When you change an attribute of work (e.g. work.date) it's reflected in all references to that single object. You want each index to reference a separate, independent object but that's not what the code is doing.

Try something like this where work is a dictionary:

for line in modified:
    work = {}  # <-- this makes the name "work" refer to a new, empty dict
    if line.startswith('Date'):
        output.append(line)
        list2=line.split(' ')
        work["date"] = list2[1]
    elif line.startswith('PPV'):
        output.append(line)
        list1=line.split(' ')
        work["lpv"] = list1[2]
        # ...

    print(count, daylist[count]["date"])
    count += 1

Here's a helpful link on how names reference objects: How does Python referencing work?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.