简体   繁体   English

递归比较列表中的项目

[英]Recursively Comparing Items in a List

I've generated a unique list of strings.我生成了一个唯一的字符串列表。 Each string is 6 numbers separated by a colon.每个字符串是由冒号分隔的 6 个数字。 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.我正在尝试获取此列表并将一个项目与下一个保留案例进行比较,其中 6 个数字中的每一个都大于或等于下一个项目。 Initially, I wrote a function that did this, but it ended up returning all of the strings.最初,我编写了一个 function 来执行此操作,但它最终返回了所有字符串。 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.我怀疑我需要对 UniqueTierHash 进行某种递归评估,并在遍历列表时删除项目。 Any suggestions on how best to approach this would be appreciated.任何有关如何最好地解决此问题的建议将不胜感激。 Thanks.谢谢。

A good way to handle such data is to put it in a NumPy array.处理此类数据的一个好方法是将其放入 NumPy 数组中。 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]])

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;如果你想定期访问一个元素的索引,你应该使用类似for index, hash in enumerate(UniqueTierHash)的东西来迭代列表,但在这种情况下,我认为你并不需要索引; 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 #

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM