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

Hey mates! I've been pounding my head against this problem for the last 2 weeks so I think it's time to get some help! Wall
I have a nested list like
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]]
(The real-life list is several hundred sublists long). :)

Here's what I have to do...
1. Only compare nested lists that begin with the same first number (this first number defines the radiation treatment field that the sublists are within). For instance, the first four all begin with 0, so I will be comparing only over those four, then comparing only the two that begin with 1, then only the two that begin with 2. It's safe to assume that this is always increasing numerically.
2. Okay, now for each sublist that meets the criteria above:
   (a). Compare each element. Is oldljp[0][1] == oldljp[1][1] == oldljp[2][1] and so on.
    (b). If so, I want to string the element.
After this runs, I would hopefully have something like
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]]
So far I've tried a combination of for loops 4 levels deep, zipping, setting up a Pandas dataframe, and using Numpy. I've never gotten any of these to work. My most recent attempt is below:
def randMlcVmat(self, oldljp):
    '''
    Add random motion to the provided sequence of Leaf Jaw Positions and
    return the new values for later use.
    '''


    # Get a list of beam sequences. Note that this depends on the oldljp
    # list passed to the function - since the conebeam sequence is always
    # removed and other sequences may also be removed, the number of beam
    # sequences found here may not be the same as in the original RP*.dcm.
    beamSeq =
    if self.ui.checkBox_staticLeaves.isChecked():
        for index, seq in enumerate(oldljp):
            if seq[0][0].split(':')[0] not in beamSeq:
                beamSeq.append(seq[0][0].split(':')[0])

    beamRange =
    for beam in beamSeq:
        beamHold =
        for cpIndex, cp in enumerate(oldljp):
            # print('cp: %r beam: %r' % (cp[0][0].split(':')[0], beam))
            # print(cp[0][0].split(':')[0] == beam)
            if cp[0][0].split(':')[0] == beam:
                beamHold.append(cpIndex)
        beamRange.append(beamHold)

    # print(beamRange)

    for bRange in beamRange:
        flip90 = list(zip(*oldljp[bRange[0]:bRange[-1] + 1]))
        # print(flip90)
        for lIndex, leafSeq in enumerate(flip90):
            if leafSeq.count(leafSeq[0]) == len(leafSeq):
                print(flip90[lIndex])
               
# From here I always lose my way.
...and then other unrelated code
I'd be grateful for any help!
Thanks!
Fred
OS: Arch
Editor: Atom with Material Syntax UI and the Termination terminal plugin

Micah 6:8
Reply


Messages In This Thread
Compare each n-th element of a nested list - by Fred Barclay - Oct-31-2017, 01:26 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert element of list to integer(Two dimentional array) zorro_phu 3 4,829 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