Posts: 161
Threads: 36
Joined: Jun 2018
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
Posts: 1
Threads: 0
Joined: May 2019
Not sure I follow here, what is the expected output?
Posts: 161
Threads: 36
Joined: Jun 2018
May-03-2019, 06:43 PM
(This post was last modified: May-03-2019, 06:51 PM by DreamingInsanity.)
(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.
Posts: 2,168
Threads: 35
Joined: Sep 2016
May-03-2019, 06:58 PM
(This post was last modified: May-03-2019, 06:58 PM by Yoriz.)
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]]
Posts: 161
Threads: 36
Joined: Jun 2018
(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
Posts: 8,155
Threads: 160
Joined: Sep 2016
May-03-2019, 07:20 PM
(This post was last modified: May-03-2019, 07:20 PM by buran.)
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
Posts: 161
Threads: 36
Joined: Jun 2018
May-03-2019, 07:31 PM
(This post was last modified: May-03-2019, 07:32 PM by DreamingInsanity.)
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
Posts: 50
Threads: 10
Joined: Dec 2017
May-03-2019, 08:29 PM
(This post was last modified: May-04-2019, 08:44 PM by MvGulik.)
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, ...]
Posts: 161
Threads: 36
Joined: Jun 2018
May-03-2019, 09:08 PM
(This post was last modified: May-03-2019, 09:08 PM by DreamingInsanity.)
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
Posts: 50
Threads: 10
Joined: Dec 2017
May-04-2019, 06:15 AM
(This post was last modified: May-04-2019, 06:16 AM by MvGulik.)
(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.
|