Home > Mobile >  Print lowest values in dict based on int
Print lowest values in dict based on int

Time:08-24

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"

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

I don't know if it actually helps,but you can achieve the same outcome by putting each value-list into a new list and then use the sorted function like so:

MyDict = {'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']}
ListA = MyDict['A']
print(sorted(ListA))

Output:

['01 ok', '01 word', '01 yeah', '02 brain', '02 file', '03 name', '03 oui', '03 some', '03 what']

(Example For Key 'A', of course if you don't want the 01,02,03 parts of the string you can remove them)

  • Related