python - Formatting Dictionary -


so working dictionary nice information, , wan print out words in nicely formatted way means of function.

i have this:

    norwdict = {         'et hus': {             'pron': 'hus',             'def': 'house',             'pos': 'noun',             'gen': 'neuter', }         'en blomst' :{             'pron':'blomst',             'def':'flower',             'pos': 'noun',             'gen':'masc', }  

i want print looks like:

printword(norwdict, 'blomst')  en blomst (blomst), noun     flower.   

what things do in order format in function def printword()?

i'd use str.format. see: https://docs.python.org/2/library/string.html#formatstrings

as is, work this:

def print_word(your_dict, your_word):     # need sort of logic go 'blomst' 'en blomst'     key, = {k k in your_dict.keys() if your_word in k}      # {'pron': 'blomst', ... }     values = your_dict[key]      print """{key} ({pron}), {pos}     {def}""".format(key=key, **values) 

str.format lets pass in named arguments, can here doing ** unpacking on inner dict.


Popular posts from this blog