Python Forum
Compare each n-th element of a nested list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Compare each n-th element of a nested list
#2
Maybe this helps (C-like, not very pythonish):

#!/usr/bin/python3
OLDLJP = [[['0:0:0'], 1, 2, 3, 4],
          [['0:0:1'], 1, 2, 5, 4],
          [['0:0:3'], 1, 2, 9, 3],
          [['0:0:4'], 1, 2, 8, 1],
          [['1:0:0'], 1, 4, 3, 6],
          [['1:0:1'], 1, 5, 7, 8],
          [['2:1:1'], 1, 4, 3, 4],
          [['2:3:8'], 1, 1, 1, 1]]

NEWLJP = [[['0:0:0'], '1', '2', 3, 4],
          [['0:0:1'], '1', '2', 5, 4],
          [['0:0:3'], '1', '2', 9, 3],
          [['0:0:4'], '1', '2', 8, 1],
          [['1:0:0'], '1',  4,  3, 6],
          [['1:0:1'], '1',  5,  7, 8],
          [['2:1:1'], '1',  4,  3, 4],
          [['2:3:8'], '1',  1,  1, 1]]

def calculategroup(list, start, end):
   #print(start, end)
    for x in range(1, 5):
        equal = True
        for y in range(start+1, end):
            if list[y-1][x] != list[y][x]:
                equal = False
                break
        if equal:
            for y in range(start, end):
               list[y][x] = str(list[y][x])

def calculate(list):
    start = 0
    for i in range(1, len(list)):
        if list[i-1][0][0][0] != list[i][0][0][0]:
            calculategroup(list, start, i)
            start = i
    calculategroup(list, start, len(list))


calculate(OLDLJP)
print(OLDLJP)
if OLDLJP == NEWLJP:
   print("ok")
else:
   print("wrong")
#done
Reply


Messages In This Thread
RE: Compare each n-th element of a nested list - by heiner55 - Oct-31-2017, 05:20 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert element of list to integer(Two dimentional array) zorro_phu 3 4,873 Jun-12-2018, 04:49 AM
Last Post: zorro_phu

Forum Jump:

User Panel Messages

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