Home > Net >  How can i look if a list of tuples has a duplicate string, and then return the tuple with the higher
How can i look if a list of tuples has a duplicate string, and then return the tuple with the higher

Time:09-23

This is the code i tried:

def generate_combined_list(inputs: list):

    data_types = {'set': set(), 'list': [], 'dict': {}, 'int': 1, 'float': 1.5, 'string': 'poodle'}
    new_list = []

    for i in inputs:
        var = i
        if var[1] in data_types:
            for i in range(var[0]):
                new_list.append(data_types[tup[1]])
    return new_list

expected output:

print(generate_combined_list([(3, 'int'), (5, 'int')]))  
# expected output [1, 1, 1, 1, 1]

print(generate_combined_list([(3, 'int'), (5, 'list'), (4, 'int')]))  
# expected output [[], [], [], [], [], 1, 1, 1, 1]

CodePudding user response:

Use a dictionary to hold all the strings that were found, replacing the value with the current value if it's larger.

from collections import defaultdict
from copy import copy

def generate_combined_list(inputs: list):

    data_types = {'set': set(), 'list': [], 'dict': {}, 'int': 1, 'float': 1.5, 'string': 'poodle'}
    new_list = []
    counts = defaultdict(lambda: 0)

    for dt, count in inputs:
        if dt in data_types:
            counts[dt] = max(counts[dt], count)

    for dt, count in counts:
        new_list.extend([copy(data_types[dt]) for _ in count]

    return new_list

I used copy(data_types[dt]) so that the result won't contain multiple references to the same list, set, dict, etc.

CodePudding user response:

Similar idea to Barmars solution.

Sort the inputs by type,amount into a dictionary - as types are duplicates only highest will remain. Use the values of that dict to filter the incoming list and you are done:

def generate_combined_list(inputs: list):

    data_types = {'set': set(), 'list': [], 'dict': {}, 'int': 1, 
                  'float': 1.5, 'string': 'poodle'}
    new_list = []
    
    # sort by (type,value) ascending and create dict - only largest of each type survives
    keep = {v:(k,v) for (k,v) in sorted(inputs, key = lambda x : (x[1], x[0])) }
    # just remember the values
    # print(keep)                   # remove comment for some insight
    keep = keep.values()

    # iterate the kept values
    for tup in keep:
        if tup[1] in data_types:
            for i in range(tup[0]):
                new_list.append(data_types[tup[1]])

    return new_list


print(generate_combined_list([(3, 'int'), (5, 'list'), (4, 'int')]))  

Output:

[[], [], [], [], [], 1, 1, 1, 1]

This will respect the order in which the things get taken from the original list.

  • Related