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
#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


Messages In This Thread
RE: Reading one value from an array in reverse - by MvGulik - May-03-2019, 08:29 PM

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,411 Mar-02-2021, 01:54 AM
Last Post: Jeremy7
  while loop reading list in reverse Jerry51 1 1,614 Apr-24-2020, 12:44 PM
Last Post: deanhystad
  Reading data from serial port as byte array vlad93 1 12,210 May-18-2019, 05:26 AM
Last Post: heiner55
  Need help with reading input from stdin into array list Annie123 2 5,146 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