Python Forum
Reading one value from an array in reverse
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reading one value from an array in reverse
#1
Take this array: [[100, 200, 0], [100, 300, 1], [100, 400, 2]]
It can be split into three things: [position1, position2, message]
I can reverse the main array using the [::-1], but when I read from it, and use that data, the result is the same.
Is there a way I can read just the last value from my array in reverse.
So, if I print the current array it is:
Output:
[100, 200, 0] [100, 300, 1] [100, 400, 2]
but when I reverse just the last value, it will be:
Output:
[100, 200, 3] [100, 300, 2] [100, 400, 1]
How can I achieve this?

Thanks,
Dream
Reply
#2
Not sure I follow here, what is the expected output?
Reply
#3
(May-03-2019, 06:12 PM)emkitsao Wrote: Not sure I follow here, what is the expected output?

Dont worry!

If you look at the first output, you can see the last numbers go in ascending order. Then when they are reversed, the output is the numbers going in descending order.

So I would like to reverse just the 2 index.

Maybe like this:
SUBROUTINE reverseNums(arr)
    FOR pos IN arr
        pos.REVERSE(pos[2])
    ENDFOR
ENDSUBROUTINE
meaning the last index of the array is reversed.

Hope it makes sense,
Dream

EDIT: I have just made this:
def reverseLastIndex(arr):
	temp = []
	for i in range(0,len(arr)):
		temp.append(arr[i][2])
	temp = temp[::-1]
	for j in range(0,len(arr)):
		arr[j][2] = temp[j]
	return temp
which works:
Output:
OLD: [[100, 200, 0], [100, 300, 1], [100, 400, 2]] NEW: [[100, 200, 2], [100, 300, 1], [100, 400, 0]]
because the last value of the array is reversed.
I just dont think this is that efficient, especially for a big array.

FYI I have made a mistake on the previous bit of code, it should return 'arr' not temp.
Reply
#4
mylist = [[100, 200, 0], [100, 300, 1], [100, 400, 2]]

def reverse_last(data):
    new_data = []
    for first, second in zip(data, reversed(data)):
        new_data.append(first[:-1] + second[-1:])
        
    return new_data

print(f'Old : {mylist}')
new_mylist = reverse_last(mylist)
print(f'New : {new_mylist}')
Output:
Old : [[100, 200, 0], [100, 300, 1], [100, 400, 2]] New : [[100, 200, 2], [100, 300, 1], [100, 400, 0]]
Reply
#5
(May-03-2019, 06:58 PM)Yoriz Wrote:
mylist = [[100, 200, 0], [100, 300, 1], [100, 400, 2]]

def reverse_last(data):
    new_data = []
    for first, second in zip(data, reversed(data)):
        new_data.append(first[:-1] + second[-1:])
        
    return new_data

print(f'Old : {mylist}')
new_mylist = reverse_last(mylist)
print(f'New : {new_mylist}')
Output:
Old : [[100, 200, 0], [100, 300, 1], [100, 400, 2]] New : [[100, 200, 2], [100, 300, 1], [100, 400, 0]]
Oh no! I have forgotten a major part!

Thanks for the code, but, I just remembered. I need to reverse the values for the first 4 items of the array. It's becuase I provided a small array as a test. Let's take this array:
Output:
[[0, 0, 1], [100, 0, 2], [200, 0, 3], [300, 0, 4], [0, 200, 9], [100, 200, 10], [200, 200, 11], [300, 200, 12], [0, 400, 17], [100, 400, 18], [200, 400, 19], [300, 400, 20]]
For every 4 item I need the values swapped. So the output would be:
Output:
[[0, 0, 4], [100, 0, 3], [200, 0, 2], [300, 0, 1], [0, 200, 12], [100, 200, 11], [200, 200, 10], [300, 200, 19], [0, 400, 20], [100, 400, 19], [200, 400, 18], [300, 400, 17]]
.

I am working on an example solution now.

Sorry about this!
Dream
Reply
#6
some star magic :-)
spam = [[100, 200, 0], [100, 300, 1], [100, 400, 2]]
*head, last = zip(*spam)
eggs = list(zip(*head, last[::-1]))
print(spam)
print(eggs)
# if you don't like list of tuples and want list of lists
eggs = list(map(list, eggs))
print(eggs)
Output:
[[100, 200, 0], [100, 300, 1], [100, 400, 2]] [(100, 200, 2), (100, 300, 1), (100, 400, 0)] [[100, 200, 2], [100, 300, 1], [100, 400, 0]]

oh, I didn't see the new requirement - it was posted while I was writing my post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
Thanks for the reply anyway!
I have gotten a little bit of an example here:
def reverseindex(arr, index_to_reverse, for_first_x_values):
	temp = []
	temp2 = []
	for i in range(0,for_first_x_values):
		temp.append(arr[i])
	for pos in temp:
		temp2.append(pos[index_to_reverse])
	temp2 = temp2[::-1]
	for j in range(0,for_first_x_values):
		temp[j][index_to_reverse] = temp2[j]
	return arr
This, when given:
Output:
[[0, 0, 1], [100, 0, 2], [200, 0, 3], [300, 0, 4], [0, 200, 9], [100, 200, 10], [200, 200, 11], [300, 200, 12], [0, 400, 17], [100, 400, 18], [200, 400, 19], [300, 400, 20]]
will output:
Output:
[[0, 0, 4], [100, 0, 3], [200, 0, 2], [300, 0, 1]]
This means my logic is working. I just now need to repeat it for the whole array, not just 4 parts.

Dream
Reply
#8
Bug 
I guess I'm kinda addicted to List comprehensions, and could not resist to give them a try at this problem.

Ergo:
ml1 = [[0, 0, 1], [100, 0, 2], [200, 0, 3], [300, 0, 4], [0, 200, 9], [100, 200, 10], [200, 200, 11], [300, 200, 12], [0, 400, 17], [100, 400, 18], [200, 400, 19], [300, 400, 20]]
ss = 4 ## SectioonSize
print;print 1.0, ml1

if len(ml1)%ss:
  print;print("NOTE: List length not a multiple of", ss)
  ## continueing anyway

ml2 = [ (ml1[i*ss:(i+1)*ss]) for i in range(int(len(ml1)/ss)) ] ## Split source list into sections of 4 entries.

ml3 = [ sl[i][0:ss-2] + [sl[len(sl)-i-1][2]] for sl in ml2 for i in range(len(sl)) ] ## process the sections.

print;print 2.0, ml3
Output:
1.0 [[0, 0, 1], [100, 0, 2], [200, 0, 3], [300, 0, 4], [0, 200, 9], [100, 200, 10], [200, 200, 11], [300, 200, 12], [0, 400, 17], [100, 400, 18], [200, 400, 19], [300, 400, 20]] 2.0 [[0, 0, 4], [100, 0, 3], [200, 0, 2], [300, 0, 1], [0, 200, 12], [100, 200, 11], [200, 200, 10], [300, 200, 9], [0, 400, 20], [100, 400, 19], [200, 400, 18], [300, 400, 17]]
(General code readability was not a high priority in this case)
(code edit: renamed s1 var to ss)

---

General working:
(presuming one knowns general basics of list-comprehensions.)
As the required reversal is limited to sections of 4 main-list entries we first split the source data into sections of 4. So those sections can be processed independent of the other data.
This is done by dividing the total length of the list by the intended section length, and than storing those sections as separate sub-lists.
ml2 = [        i                   for i in range(int(len(ml1)/ss)) ] ## => [1,2,3] :: walk trough the number of sets of 4.
ml2 = [      ( i*ss , (i+1)*ss )   for i in range(int(len(ml1)/ss)) ] ## => [(0, 4), (4, 8), (8, 12)] :: list capture parts per section.
ml2 = [ ( ml1[ i*ss : (i+1)*ss ] ) for i in range(int(len(ml1)/ss)) ] ## => [ ml1[0:4], ml1[4:8], ...] :: final section split.
Now the sections can be processed separately.
First we split/target the main list section parts with the for sl in ml2 part. (sl=SubList).
Than we set up the sl part targeting with for i in range(len(sl)
Now we can swap the last element in the section parts (done per main-list section). sl[i][0:s1-2] takes care of capturing the leading elements, to which we add the reversed last element [sl[len(sl)-i-1][2]]
(note: this last part is not making use of the ss var, although it probably could/should, without using ss using list[:-1] an list[-1] calls would make more sense here (probably/untested).)
ml3 = [         i    for sl in ml2   for i in range(len(sl)) ] ## => [0, 1, 2, 3,  0, 1, 2, 3,  ...]
ml3 = [ len(sl)-i-1  for sl in ml2   for i in range(len(sl)) ] ## => [3, 2, 1, 0,  3, 2, 1, 0,  ...]
Reply
#9
Thanks for the reply!

I can’t test it out right now, but by the looks of it, it works.

Would you mind explaining how it works in more detail?

Thanks again,
Dream
Reply
#10
(May-03-2019, 09:08 PM)DreamingInsanity Wrote: Would you mind explaining how it works in more detail?
No I don't mind.
Added a general processing part to the post.

PS: The more specific one is in what is unclear, the more specific/targeted the help/explanation can be.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python Jeremy7 8 7,138 Mar-02-2021, 01:54 AM
Last Post: Jeremy7
  while loop reading list in reverse Jerry51 1 1,579 Apr-24-2020, 12:44 PM
Last Post: deanhystad
  Reading data from serial port as byte array vlad93 1 12,086 May-18-2019, 05:26 AM
Last Post: heiner55
  Need help with reading input from stdin into array list Annie123 2 5,072 Mar-24-2019, 01:19 PM
Last Post: Annie123

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020