Light Mode
Dark Mode
What are criteria for sorting elements in a dict?

I half-solved the task, but got stuck at this initial case:

yaml("name: Bob Dylan\nburn: 24 May 1941\nresident: Malibu, California, U.S\n\nchildren: 6")

which should be transformed in the dictionary:

Right result:{"resident":"Malibu, California, U.S","burn":"24 May 1941","name":"Bob Dylan","children":6}

I was under impression that the keys of the dictionary should be sorted alphabetically. But obviously not. SO what is criteria for sorting out dict keys? My result is:

Your result:{"burn":"24 May 1941","children":"6","name":"Bob Dylan","resident":"Malibu, California, U.S"}

(BTW, there is a typo. Bob was bOrn not bUrn on 24 May 1941, I guess.)

And this is my code:

def yaml(a):
    recnik = {}
    b = []
    a = a.split('\n')
    a = [i for i in a if len(i) != 0]
    a = sorted(a)
    for i in a:
        b = i.split(':')
        recnik[b[0]] = b[1].strip()
    for k in recnik:
        if k == 'age' or k == '12':
            recnik[k] = int(recnik[k])
    return recnik
Created: May 25, 2020, 5:15 p.m.
Updated: May 25, 2020, 7:10 p.m.
0
10
User avatar
Bundy