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!

ian
  • 1
  • count is getting incremented only when line.startswith('PPV') is this what you want? consider to use [enumerate function](https://docs.python.org/3/library/functions.html#enumerate) – Kurohige Nov 15 '22 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. – Woodford Nov 15 '22 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. – ian Nov 15 '22 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? – ian Nov 15 '22 at 17:10
  • Does this answer your question? [List of lists changes reflected across sublists unexpectedly](https://stackoverflow.com/questions/240178/list-of-lists-changes-reflected-across-sublists-unexpectedly) – Woodford Nov 15 '22 at 17:12
  • 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. – Woodford Nov 15 '22 at 17:16
  • @woodford I think i understand what you're saying now. im thinking i just get rid of work and assign the variables straight to the approptiate daylist attribute. Thanks so much! – ian Nov 15 '22 at 17:19
  • @woodford how could i reference a separate object? turning work into an array isnt helping and is redundant. – ian Nov 15 '22 at 17:30
  • @Woodford thank you so much! Ive never used dictionaries before and honestly didn't understand their purpose but now i do! – ian Nov 15 '22 at 18:06

1 Answers1

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?

Woodford
  • 3,746
  • 1
  • 15
  • 29