从lis的元组中选择

2024-09-28 16:57:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个名称对的元组列表,每对都有一个值,格式如下:

1.[('Carlo', 'Helen', 9), ('Carlo', 'Mary', 4), ('Jan', 'Rolly', 1), ('Renzi', 'Rolly', 3)] 
2.[('Sofy', 'Reem', 9), ('Sofy', 'David', 5),('Sofy', 'keem', 3) ('Roly', 'Tony', 4), ('Gianni', 'Tony', 2)]
3.[('Sofy', 'Reem', 9), ('Sofy', 'David', 5),('Sofy', 'keem', 3),('Roly', 'Reem', 5), ('Roly', 'David', 2),('Roly', 'keem', 3)]

我想要的是:对于列表中的每一行,如果两个或多个名称对共享一个名称(第一个或第二个名称),我必须选择具有高值的名称对。因此,对于上面的示例,输出将是

  1-('Carlo', 'Helen', 9) 
    ('Renzi', 'Rolly', 3)
  2-('Sofy', 'Reem', 9)
    ('Roly', 'Tony', 4)

有人能帮忙吗?你知道吗


Tags: 名称列表格式carlo元组davidhelenmary
3条回答

你需要两个功能。一种是迭代分组,另一种是求最大值:

def find_highest_pair(tuple):
    highest_value = 0
    result = []
    for pair in tuple:
        if pair[2] > highest_value:
            highest_value = pair[2]
            result = pair
    return result

def grouping(tuple,n):
    stack = []
    result = []
    backup = []
    if tuple:
        check_pair = tuple.pop(0)
        backup = tuple
        if tuple:
            if check_pair[n] == tuple[0][n]:
                stack.append(tuple[0])
                tuple.pop(0)
        if not stack and len(tuple) > 0:
            return grouping(backup,n)
    stack.append(check_pair)
    result = find_highest_pair(stack)
    return result


tuple1 = [('Carlo', 'Helen', 9), ('Carlo', 'Mary', 4), ('Jan', 'Rolly', 1), ('Renzi', 'Rolly', 3)]
tuple2 = [('Sofy', 'Reem', 9), ('Sofy', 'David', 5), ('Sofy', 'keem', 3), ('Roly', 'Tony', 4), ('Gianni', 'Tony', 2)]

print "1:"
print grouping(tuple1,0)
print grouping(tuple1,1)
print "2:"
print grouping(tuple2,0)
print grouping(tuple2,1)

我还没有找到改进的方法,但是你可以把你的清单翻两遍。在外循环中,每一项都被选为候选项。在内部循环中,每个在名字或姓氏上匹配的iteam,当且仅当其得分高于当前候选项时才替换候选项进行选择。在内部循环的末尾,如果候选对象尚未插入,则将其插入到选择列表中。你知道吗

def get_top_scorers(dataset):
    if dataset and all(isinstance(d, list) for d in dataset):
        return [get_top_scorers(d) for d in dataset]
    selected_list = []
    for candidate in dataset:
        chosen = candidate
        for next_candidate in dataset:
            if next_candidate == chosen:
                continue
            next_name, next_surname, next_score = next_candidate

            shares_name_with_selected = any(
                next_name == s[0] or next_surname == s[1]
                for s in selected_list
            )
            if shares_name_with_selected:
                continue

            name, surname, score = chosen
            if (name == next_name or surname == next_surname) and score < next_score:
                chosen = next_candidate

        shares_name_with_selected = any(
            chosen[0] == s[0] or chosen[1] == s[1]
            for s in selected_list
        )
        if chosen not in selected_list and not shares_name_with_selected:
            selected_list.append(chosen)
    return selected_list
 # this contains both lists
lt = [('Carlo', 'Helen', 9), ('Carlo', 'Mary', 4), ('Jan', 'Rolly', 1), ('Renzi', 'Rolly', 3),
                      ('Sofy', 'Reem', 9), ('Sofy', 'David', 5),('Sofy', 'keem', 3), ('Roly', 'Tony', 4), ('Gianni', 'Tony', 2)]

           def max_value_tuples(tuple_list):
            # find max value tuples in the list of tuple
            import itertools as it
            import operator as op
            groups = []

            # group by first element
            for k, g in it.groupby(tuple_list, key=op.itemgetter(0)):
                groups.append(list(g))
            # group by second element
            for k, g in it.groupby(tuple_list, key=op.itemgetter(1)):
                groups.append(list(g))

            final = []
            # leave out idividiual tuples
            for el in groups:
                if len(el) > 1:
                    final.append(el)

            result = []
            # sort and print with highest scrores
            for el in final:
                el = sorted(el, key=op.itemgetter(2))
                result.append(el[-1])

            return result

        print(max_value_tuples(lt))

        [('Carlo', 'Helen', 9), ('Sofy', 'Reem', 9), ('Renzi', 'Rolly', 3), ('Roly', 'Tony', 4)]

    with this and your astfile, you could do:

    all_lists = []
    for x in astfile:
        x_list = max_value_tuples(x)
        all_lists.append(x_list)

相关问题 更多 >