So this is what I tried to do.
vectorized = [0] * length
for i,key in enumerate(foo_dict.keys()):
vector = vectorized
vector[i] = 1
print vector
vector = vectorized
print vectorized
So what I was hoping was for example the length is 4. So i create a 4 dimension vector:
vectorized=[0,0,0,0]
now, depending on the index of dictionary (which is also of length 4 in this case) create a vector with value 1 while rest has zero
so vector = [1, 0,0,0] , [0,1,0,0] and so on..
Now instead what is happening is:
vector = [1,0,0,0],[1,1,0,0] .. and finally [1,1,1,1]
even vectorized is now
[1,1,1,1]
Whats wrong I am doing. and how do i achieve what i want to achieve. Basically I am trying to create unit vectors. Thanks
This line (these lines, really):
vector = vectorized
copies the list reference. You need to do a shallow copy of the sequence contents.
vector = vectorized[:]
You are creating a single list and then giving it several different names. Remember that a = b doesn't create a new object. It just means that a and b are both names for the same thing.
Try this instead:
for ...:
vector = [0] * length
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With