wein3967
wein3967

Reputation: 45

Recursively Comparing Items in a List

I've generated a unique list of strings. Each string is 6 numbers separated by a colon. The list of strings has been sorted from largest to smallest by the first number then progressively by 2nd, 3rd, and so on. Example snippet below:

UniqueTierHash = [ '6:3:5:6.5:5:2.5',
                   '6:3:5:5.5:5:3.5',
                   '6:2.5:5:5:4:3',
                   '6:2.5:5.5:5.5:4.5:3.5',
                   '5.5:4.5:4.5:4.5:5.5:4.5' ]

I'm trying to take this list and compare one item to the next keeping cases where each of the 6 numbers is larger or equal to the next item. Initially, I wrote a function that did this, but it ended up returning all of the strings. This is because a lesser string would be subsequently compared to the next string, and being different, both would be retained.

TierHashKeep = []

for i in UniqueTierHash:

    if UniqueTierHash.index(i) == len(UniqueTierHash) - 1: break

    test = function.CompareTierHash(i,UniqueTierHash[UniqueTierHash.index(i) + 1])
    print(i + ' \n' + UniqueTierHash[UniqueTierHash.index(i) + 1])
    print(test)

    if test == False:
        TierHashKeep.append(i)
        TierHashKeep.append(UniqueTierHash[UniqueTierHash.index(i) + 1])
    elif test == True:
        TierHashKeep.append(i)
    else:
        print('Error in TierCompare')

I suspect that I need to employ some kind of recursive evaluation of the UniqueTierHash and remove items as I iterate through the list. Any suggestions on how best to approach this would be appreciated. Thanks.

Upvotes: 0

Views: 545

Answers (2)

Joffan
Joffan

Reputation: 1480

You're making life hard for yourself here; if you want regular access to the index of an element, you should iterate the list with something like for index, hash in enumerate(UniqueTierHash), but in this case I don't think you really need the index; you just want access to two adjacent elements, so you can grab the first element and then iterate over the remaining list, retaining the "previous" value each time. An alternative method would be to iterate just over indices and grab the elements directly each time through the loop.

I have only retained the actual values that pass your test here; you seemed to be keeping all values and some twice.

UniqueTierHash = [ '6:3:5:6.5:5:2.5',
                   '6:3:5:5.5:5:3.5',
                   '6:2.5:5:5:4:3',
                   '6:2.5:5.5:5.5:4.5:3.5',
                   '5.5:4.5:4.5:4.5:5.5:4.5' ]

TierHashKeep = []

this_hash = UniqueTierHash[0] # first element to start the pairing

for next_hash in UniqueTierHash[1:]: # iterate through rest of list

    test = function.CompareTierHash(this_hash, next_hash)

    print(this_hash + ' \n' + next_hash) ####### check #
    print(test) ####### check #

    if test == True:
        TierHashKeep.append(this_hash)
    elif test != False:
        print('Error in TierCompare')

    this_hash = next_hash # roll forward to the next pair

print(TierHashKeep) ####### check #

Upvotes: 0

Arne
Arne

Reputation: 10545

A good way to handle such data is to put it in a NumPy array. One way to do the filtering is to initialize a list with just the first row of the full array, and then iterate over the other rows of the full array, comparing each one to the last element of the new list, and adding it to the new list if all its elements are smaller:

import numpy as np

list_of_strings = ['6:3:5:6.5:5:2.5', # data slightly changed for testing
                   '6:3:5:5.5:5:3.5',
                   '6:2.5:5:5:4:2',
                   '6:2.5:5.5:5.5:4.5:3.5',
                   '5.5:1:1:1:1:0.5']

numbers = np.array([s.split(':') for s in list_of_strings], 
                   dtype=float)

numbers_descending = [numbers[0]]

for row in numbers[1:]:
    if all(row <= numbers_descending[-1]):
        numbers_descending.append(row)

numbers_descending = np.array(numbers_descending)

numbers_descending
array([[6. , 3. , 5. , 6.5, 5. , 2.5],
       [6. , 2.5, 5. , 5. , 4. , 2. ],
       [5.5, 1. , 1. , 1. , 1. , 0.5]])

Upvotes: 3

Related Questions