Print lowest values in dict based on int

Question:

I have this dict with dummy values:

{'A': ['03 some', '03 what', '01 word', '03 oui', '01 ok', '03 name', '02 file', '01 yeah', '02 brain'], 'B': ['02 whatever', '01 ok', '02 zip', '01 stack', '02 great', '01 sure'], 'C': ['01 only_one', '01 it', '01 dummy']}

Q: How can I code python so that: per key, it prints the filename, of the highest value – 1, based on the has the lowest value the first 2 digits? If there is only 1 value for a key, skip the print.

Expected output:

# of key A
word
ok
file
yeah
brain

# of key B
ok
stack
sure

I started by using a sort, dict(sorted(x.items(), key=lambda item: item[1])), but this sorts the list in an undesired order.

Edit: I worded my question incorrectly. The example output is correct.

The idea is: "Print everything, other lower than the highest value"

Asked By: Kevin C

||

Answers:

I assumed there is a typo in the output you provided because the first 2 digits of file and brain are larger than the ones of the other values.

def lowest_values_each_key(dictionary):
    for letter, values in dictionary.items():
        words = dict()
        for value in values:
            number, word = value.split()
            number = int(number)
            if number not in words:
                words[number] = [word]
            else:
                words[number] += [word]
        words = dict(sorted(words.items()))
        del words[max(words)]
        for number, words in words.items():
            for word in words:
                print(word)

Output:

word
ok
yeah
file
brain
ok
stack
sure
Answered By: Jock
from collections import defaultdict

dictionary = {'A': ['03 some', 
                    '03 what', 
                    '01 word', 
                    '03 oui', 
                    '01 ok', 
                    '03 name', 
                    '02 file', 
                    '01 yeah', 
                    '02 brain', 
                    ], 
              'B': ['02 whatever', 
                    '01 ok', 
                    '02 zip', 
                    '01 stack', 
                    '02 great', 
                    '01 sure', 
                    ], 
              'C': ['01 only_one', 
                    '01 it', 
                    '01 dummy', 
                    ]
              }

for key, list_ in dictionary.items():
    # Find the highest key
    max_key = max(set(item[:2] for item in list_), key=int)
    for item in list_:
        filename_key, filename = item.split(maxsplit=1)
        # Exclude values if their key is the max key
        if filename_key != max_key:
            print(filename)
        
    print()

Output:

word
ok
file
yeah
brain

ok
stack
sure

Answered By: GordonAitchJay

A simple solution with Python 3.7+

dictionary = {
    'A': ['03 some', '03 what', '01 word', '03 oui', '01 ok', '03 name', '02 file', '01 yeah', '02 brain'],
    'B': ['02 whatever', '01 ok', '02 zip', '01 stack', '02 great', '01 sure'],
    'C': ['01 only_one', '01 it', '01 dummy']
}

for key, value in dictionary.items():

    words = {}
    for pair in value:
        number, word = pair.split()
        number = int(number)

        if number not in words:
            words[number] = []
        words[number].append(word)

    sorted_words = dict(sorted(words.items()))
    for k, v in sorted_words.items():
        for w in v:
            print(w)
    print()

Output

word
ok
yeah
file
brain
some
what
oui
name

ok
stack
sure
whatever
zip
great

only_one
it
dummy

Answered By: Ramriez
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.