Reputation: 3
So I am trying to reverse an array from a .txt file without using the reverse function. here is what I have.
numbers = read() #creates numbers array out of the txt file
numbersrev = numbers #blank array for reverse
numLength = len(numbers) #measures the length of the array
print(numbers)
print("Array length of numbers: ", numLength)
i = numLength
i = i-1 #since the array starts at 0 instead of 1
k = 0
for k in range(8):
numbersrev[k] = numbers[i]
print ("The ", i," element of numbers is the ", k," element of numbersrev")
i -= 1
k += 1
print(numbersrev)
This is what I get after debugging on vscode:
[2, 4, 9, 11, 8, 3, 2, 5, 10]
Array length of numbers: 9
The 8 element of numbers is the 0 element of numbersrev
The 7 element of numbers is the 1 element of numbersrev
The 6 element of numbers is the 2 element of numbersrev
The 5 element of numbers is the 3 element of numbersrev
The 4 element of numbers is the 4 element of numbersrev
The 3 element of numbers is the 5 element of numbersrev
The 2 element of numbers is the 6 element of numbersrev
The 1 element of numbers is the 7 element of numbersrev
[10, 5, 2, 3, 8, 3, 2, 5, 10]
The top array is the original and the bottom array is the supposed reversal array
I cannot for the life of me find out why it stops changing the numbersrev array halfway through. Anybody know what the cause could be?
Upvotes: 0
Views: 538
Reputation: 1
the problem you are facing is because of this line
numbersrev = numbers #blank array for reverse
In this line you are not creating an empty array, you are saving the reference of numbers
array in a new variable named numbersrev
. This means that when you make an operation in array numbersrev
you are changing also the values of numbers
. To avoid this kind of problems you have two options:
Make a copy of the array with slices
In this way you copy the values of the array, not the reference of the array. This means the changes you make to the new array will not change the original array.
numbersrev = numbers[:]
Create an empty array and use append instead of assignation
This change is a little different from what you did, but basically instead of creating a copy of the array, you create a new array which will be filled in the for loop, something like:
numbers.rev = []
...
for k in range(8):
numbersrev.append(numbers[i])
So with the first option and also changing some things in the k index we have a code like this:
numbers = [2, 4, 9, 11, 8, 3, 2, 5, 10] # you can change for read
numbersrev = numbers[:] #copy array elements
numLength = len(numbers) #array length
print(numbers)
print("Array length of numbers: ", numLength)
i = numLength - 1
# you don't need to initialize k because it's initialized and incremented with range in the loop
for k in range(numLength):
numbersrev[k] = numbers[i]
print ("The ", i," element of numbers is the ", k," element of numbersrev")
i -= 1
print(numbersrev)
Hope this would help you solve the problem, just like a note you could solve this problem in many ways: slicing (numbers[::-1]
), list comprehension and some others. All the ways are valid so just in case you want to explore more.
Upvotes: 0
Reputation: 51
Okay, a few things...
so:
for k in range(8):
...
i -= 1
k += 1
should be:
for k in range(8):
...
i -= 1
No need to manually increment k.
Lists in python are very different from arrays in a language like C. Lists are mutable, and are passed by reference by default. so when you try to make an empty array:
numbersrev = numbers #blank array for reverse
you are actually referencing the same 'list' from both numbers
AND numbersrev
What you should have done is numbersrev = []
Then in your for loop, simply append to numbersrev rather than assign.
for k in range(numLength):
numbersrev.append(numbers[i])
print ("The ", i," element of numbers is the ", k," element of numbersrev")
i -= 1
you could/should reference the length of numbers rather than a hardcoded value in your for loop, but how you have it will still work (assuming you ONLY ever get 8 numbers)
for k in range(numLength):
...
numbers = read() #creates numbers array out of the txt file
numbersrev = [] #blank array for reverse
numLength = len(numbers) #measures the length of the array
print(numbers)
print("Array length of numbers: ", numLength)
i = numLength
i = i-1 #since the array starts at 0 instead of 1
for k in range(numLength):
numbersrev.append(numbers[i])
print ("The ", i," element of numbers is the ", k," element of numbersrev")
i -= 1
print(numbersrev)
Upvotes: 1
Reputation: 10489
What you've done here is assign the reversed array as the normal array with this line:
numbersrev = numbers #blank array for reverse
What you're actually doing with that loop is this:
numbers[0] = numbers[9] # [10, 2, 3, 4, 5, 6, 7, 8, 9, 10]
numbers[1] = numbers[8] # [10, 9, 3, 4, 5, 6, 7, 8, 9, 10]
numbers[2] = numbers[7] # [10, 9, 8, 4, 5, 6, 7, 8, 9, 10]
numbers[3] = numbers[6] # [10, 9, 8, 7, 5, 6, 7, 8, 9, 10]
numbers[4] = numbers[5] # [10, 9, 8, 7, 6, 6, 7, 8, 9, 10]
numbers[5] = numbers[4] # [10, 9, 8, 7, 6, 6, 7, 8, 9, 10]
numbers[6] = numbers[3] # [10, 9, 8, 7, 6, 6, 7, 8, 9, 10]
numbers[7] = numbers[2] # [10, 9, 8, 7, 6, 6, 7, 8, 9, 10]
numbers[8] = numbers[1] # [10, 9, 8, 7, 6, 6, 7, 8, 9, 10]
numbers[9] = numbers[0] # [10, 9, 8, 7, 6, 6, 7, 8, 9, 10]
...
If you assign the variable like this:
numLength = len(numbers) #measures the length of the array
numbersrev = [0]*numLength #blank array for reverse
You'll get the correct answer since the reverse list is no longer pointing to the normal list.
Upvotes: 0
Reputation: 634
numbersrev = numbers
sets numbersrev
to point to the same list as numbers
, meaning when you modify numbers
or numbersrev
you're modifying the other at the same time. To make an actual copy of the object, you instead need to call numbersrev = numbers.copy()
. Also, @sahasrara62's comment is correct, you need to call for k in range(numLength)
instead of for k in range(8)
Upvotes: 0