简体   繁体   English

如何根据元组中的变量对元组列表进行排序

[英]How to sort list of tuples based on variable in tuple

Given a list of tuples in this format (item,[dependency, priority]) where the first element indicates an item, and the first element of the second indicates a dependency on a previously listed item(that means, the first item must appear in the list before the second item, on which it is dependent, appears), and finally a third element indicates the priority of rank for items of similar dependency.给定一个以这种格式 (item,[dependency, priority]) 的元组列表,其中第一个元素表示一个项目,第二个元素的第一个元素表示对先前列出的项目的依赖(这意味着,第一个元素必须出现在它所依赖的第二个项目之前的列表出现),最后第三个元素指示相似依赖项的排名优先级。 An example list looks like this:示例列表如下所示:

[(1, [0, 4]),  
(2, [0, 3]),  
(3, [1, 4]),  
(4, [1, 1]),  
(5, [2, 4])]

I need a way to sort the list such that items for which the dependency has been satisfied can be ranked higher than items with a lower priority but with fewer or no dependencies.我需要一种对列表进行排序的方法,以便满足依赖性的项目可以比具有较低优先级但具有较少或没有依赖性的项目排名更高。

For example, in the above list, the third item (3, [1, 4]) has a higher priority than the second item (2, [0, 3]), and so should be in the second position.例如,在上面的列表中,第三项 (3, [1, 4]) 的优先级高于第二项 (2, [0, 3]),因此应该在第二个 position 中。 The desired output of the example list should be this:示例列表中所需的 output 应该是这样的:

[(1, [0, 4]),  
(3, [1, 4]),  
(2, [0, 3]), 
(5, [2, 4]),
(4, [1, 1])]

I cannot simply sort by priority as that would elevate the fifth item 5, [2, 4]) to a spot above its dependency, which is 2.我不能简单地按优先级排序,因为这会将第五项 5, [2, 4]) 提升到高于其依赖项 2 的位置。

So my idea was to iterate through the list, grouping all items for which a dependency is satisfied.所以我的想法是遍历列表,将满足依赖关系的所有项目分组。 Then sort that list by priority.然后按优先级对该列表进行排序。 Then recombine all resulting lists back into a single list.然后将所有结果列表重新组合成一个列表。

I cannot figure out how to do this.我无法弄清楚如何做到这一点。 My best attempt was something like this below.我最好的尝试是下面这样。 with dependency as n.依赖为 n。 but it only really works for the first iteration.但它只适用于第一次迭代。 When the dependency is higher than one, it returns all items.当依赖项大于 1 时,它返回所有项。 I suspect this is not the best strategy to accomplish the desired result.我怀疑这不是实现预期结果的最佳策略。

Any ideas or suggestions would be appreciated!任何想法或建议将不胜感激!


    def rank_poss(array,n):
      final = []
      slot_list = []
      for i in array:
          if i[1][0] <= n and i[0] != 1:
              slot_list.append(i)  
      temp = sorted(slot_list, reverse=True, key = lambda x: x[1][2])
      final.append(temp)
      return final 

I don't know if I correctly understand your rules so I don't know if I get correct result.我不知道我是否正确理解了你的规则,所以我不知道我是否得到正确的结果。

In older Python you could use sorted (and few other functions) with argument cmp= to assign function which gets two items from list, compare them and return 0 when they are the same, -1 when first should be before second, 1 when second should be before first.在较旧的 Python 中,您可以使用带参数cmp=sorted (以及少数其他函数)来分配 function ,它从列表中获取两个项目,比较它们并在它们相同时返回0 ,当第一个应该在第二个之前时返回1 ,当第二个时返回-1应该在前。

New Pythons don't have this argument but the is function functools.cmp_to_key() which should help to work like with old cmp= and then you may try to create more complex method to sort items.新的 Python 没有这个参数,但是 function functools.cmp_to_key()应该有助于像旧的cmp=一样工作,然后您可以尝试创建更复杂的方法来对项目进行排序。

data = [
    (1, [0, 4]),  
    (2, [0, 3]),  
    (3, [1, 4]),  
    (4, [1, 1]),  
    (5, [2, 4])
]

import functools
    
def compare(a, b):
    if a[1][0] >= b[0]:
        return 1
    if a[1][1] > b[1][1]:
        return -1
    return 1

result = sorted(data, key=functools.cmp_to_key(compare))
     
for item in result:
    print(item)

Result:结果:

(1, [0, 4])
(3, [1, 4])
(2, [0, 3])
(5, [2, 4])
(4, [1, 1])

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

相关问题 如何根据元组列表中的值对元组进行排序 - How to sort a tuple based on a value within the list of tuples 如何按元组的长度对元组列表进行排序 - How to sort a list of tuples by the length of the tuple 如何按第二个元组元素对元组列表进行排序? - How to sort a list of tuples, by the second tuple element? Python lamda 函数根据与前一个元组的关系对元组列表进行排序 - Python lamda function to sort list of tuples based on relationship to previous tuple 如何根据另一个列表对元组列表进行排序 - how to sort a list of tuples based on another list 如何根据列表中的键对元组列表进行排序? - How to sort a list of tuples based on the keys in a list? 如何根据另一个列表中元组元素的顺序对元组列表进行排序? - How to sort a list of tuples according to the order of a tuple element in another list? 如何按给定索引处的元素对列表/元组的列表/元组进行排序? - How to sort a list/tuple of lists/tuples by the element at a given index? 如何根据元组中另一个值(第三个值)的降序对元组的排序列表(基于第二个值)中的相似值进行排序 - How to sort similar values in a sorted list (based on second value) of tuples based on another value(third value) in the tuple in descending order 如何使用元组的两个元素对元组列表进行排序? - How to sort list of tuples using both elements of tuple?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM