0

I've written a function to create 16, 16 dimensional vectors and put them into an array, but when I append to the array, I end up with the previous entry being overwritten.

import math

empvec=[None]*16
vecset=[]

for g in range(16):
    tempvec=empvec
    for i in range(16):
        f=g%8+1
        if(bool((g)//8)):
            tempvec[i]=i*f
        else:
            tempvec[i]=i/f
    vecset.append(tempvec)

for g in range(16):
    print(vecset[g])

I have also tried creating an empty, 16 entry array and then setting the values using vecset[g]=tempvec instead of appending, which also didn't work

ALazyDope
  • 3
  • 2
  • @Nick - The issue here is with making a shallow copy of a list via assignment, not with multiplying a list. – TigerhawkT3 Nov 12 '20 at 05:21
  • @TigerhawkT3 you're right - I didn't look closely enough at the code. Thanks for correcting. – Nick Nov 12 '20 at 05:35

1 Answers1

0

You need to copy the list for each row. Your current code has all rows pointing to the same list (memory address).

Simple change needed

for g in range(16):
    tempvec=empvec[:]  #  <<< copy list
Mike67
  • 11,175
  • 2
  • 7
  • 15