2021年3月9日星期二

How do I iterate a nested dictionary with string formatting?

I checked a few other posts and either they didn't contain the information I need or I didn't understand them. I want to make this program print the sentence for every entry in the nested dictionary, and maybe also make a function to do this as well (not familiar with these yet).

I know it will use a for loop but what I can't figure out is how to configure the keys(?).

people = {      1: {          'name': 'David Wallace',          'age': 50,          'occupation': 'CFO',          'ethnicity': 'American',          'location': 'New York'      },      2: {          'name': 'Michael',          'age': 42,          'occupation': 'Regional Manager',          'ethnicity': 'American',          'location': 'Scranton, Pennsylvania'      },      3: {          'name': 'Jim',          'age': 27,          'occupation': 'Sales Rep',          'ethnicity': 'American',          'location': 'Scranton, Pennsylvania'      }  }  print('{name} is a {age} year-old {ethnicity} {occupation} from {location}.'.format(**people))  
https://stackoverflow.com/questions/66556953/how-do-i-iterate-a-nested-dictionary-with-string-formatting March 10, 2021 at 09:05AM

没有评论:

发表评论