Search code examples
pythonlistnested

Best way to edit nested list for specific problem?


I have a question and different ideas on how to approach it, but I'd like to know what more experienced programmers would do.

Background: I have a nested list, with sub lists, that always have two items, that looks like this: [[category, item];[category, item];...]. categories and items are strings. There are items that end with a number or float and there are others, that end with just characters or spaces. Some (but not all) items are duplicates.

Problem: Just focused on the items, categories do not need to be altered. Do not suggest to use dict or something else, it needs to be a nested list like above.

  1. I need to delete every duplicate, that does not end with a number/float - so just one of the 1-10 duplicates remains in the list.
  2. If there are duplicates (same items), that end with numbers, I need to sum up all numbers/floats and just leave one entry with the summed up number and delete the original ones.

Example:

Input:

[["fruits", "Apple 1"];["fruits", "Apple 2"];["fruits", "Apple 5"];["cooled", "iced tea 1,5"]; ["cooled", "iced tea 2"]; ["fruits"; "onions"]; ["fruits"; "onions"];["fruits"; "onions"];["frozen"; "Pizza"]

Output:

[["fruits", "Apple 8"];["cooled", "iced tea 3,5"];["fruits"; "onions"];["frozen"; "Pizza"]

Any ideas?


Solution

  • this worked for me

    from collections import Counter
    
    vals = [["fruits", "Apple 1"],["fruits", "Apple 2"],["fruits", "Apple 5"],["cooled", "iced tea 1,5"], ["cooled", "iced tea 2"], ["fruits", "onions"], ["fruits", "onions"],["fruits", "onions"],["frozen", "Pizza"]]
    
    counters = {}
    others = {}
    for sub_list in vals:
        items = sub_list[1].rsplit(' ', 1)
        try:
            num = float(items[-1].replace(',', '.'))
            if sub_list[0] not in counters.keys():
                counter = Counter({items[0]: num})
                counters[sub_list[0]] = counter
            else:
                counters[sub_list[0]].update({items[0]: num})
        except:
           
            others[sub_list[0]] = sub_list[1]
    
    my_result = []
    for key, counter in counters.items():
        items = [[key, f"{sub_key} {value}"] for sub_key, value in counter.items()]
        my_result = my_result + items
    my_result = my_result + [[key, value] for key, value in others.items()]
    print(my_result)        
    

    output

    [['fruits', 'Apple 8.0'], ['cooled', 'iced tea 3.5'], ['fruits', 'onions'], ['frozen', 'Pizza']]