简体   繁体   English

打印字典变量名称列表

[英]Print the list of dictionary variable names

My assignment is: 我的任务是:

Make several dictionaries, where the name of each dictionary is the name of a pet. 制作几个词典,其中每个词典的名称是宠物的名称。 In each dictionary, include the kind of animal and the owner's name. 在每个字典中,包括动物的种类和所有者的名字。 Store these dictionaries in a list called pets. 将这些词典存储在名为pets的列表中。 Next, loop through your list and as you do print everything you know about each pet. 接下来,循环浏览您的列表,并打印您知道的每个宠物的所有内容。

What I have so far: 到目前为止我所拥有的:

rover = {'type': 'dog', 'owner': 'joe'}
blackie = {'type': 'cat', 'owner': 'gail'}
polly = {'type': 'bird', 'owner': 'paul'}
seth = {'type': 'snake', 'owner': 'stan'}

pets = [rover, blackie, polly, seth]

for pet in pets:
    print("\nPet Name:", "\nType:", pet['type'].title(), "\nPet Owner:", pet['owner'].title())

Output so far: 目前为止的输出:

Pet Name: Type: Dog Pet Owner: Joe 宠物名称:类型:狗宠物主人:乔

Pet Name: Type: Cat Pet Owner: Gail 宠物名称:类型:猫宠物主人:盖尔

Pet Name: Type: Bird Pet Owner: Paul 宠物名称:类型:鸟宠物主人:保罗

Pet Name: Type: Snake Pet Owner: Stan 宠物名称:类型:蛇宠物主人:斯坦

My Question: 我的问题:

What do I need to add to my code to have the output include the Pet Name? 我需要添加到我的代码中以使输出包含宠物名称?

Desired Output: 期望的输出:

Pet Name: Rover Type: Dog Pet Owner: Joe 宠物名称:流浪者类型:狗宠物主人:乔

Pet Name: Blackie Type: Cat Pet Owner: Gail 宠物名称:Blackie类型:猫宠物主人:盖尔

Pet Name: Polly Type: Bird Pet Owner: Paul 宠物名称:波莉类型:鸟宠物主人:保罗

Pet Name: Seth Type: Snake Pet Owner: Stan 宠物名称:赛斯类型:蛇宠物主人:斯坦

我会将名字存储在字典中。

rover = {'name' : 'rover', 'type': 'dog', 'owner': 'joe'}

You can use an if statement: 您可以使用if语句:

for pet in pets:
    for v in pet.values():
        if v == 'joe':
            print("\nPet Name: Rover", "\nType:", pet['type'].title(), "\nPet Owner:", pet['owner'].title())
        if v == 'gail':
            print("\nPet Name: Blackie", "\nType:", pet['type'].title(), "\nPet Owner:", pet['owner'].title())
        if v == 'paul':
            print("\nPet Name: Polly", "\nType:", pet['type'].title(), "\nPet Owner:", pet['owner'].title())
        if v == 'stan':
            print("\nPet Name: Seth", "\nType:", pet['type'].title(), "\nPet Owner:", pet['owner'].title())

Output: 输出:

'Pet Name: Rover '
'Type: Dog'
'Pet Owner: Joe'

'Pet Name: Blackie'
'Type: Cat'
'Pet Owner: Gail'

'Pet Name: Polly'
'Type: Bird'
'Pet Owner: Paul'

'Pet Name: Seth'
'Type: Snake'
'Pet Owner: Stan'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用名称列表创建字典 - Creating a dictionary with a list of names 给定Python中的变量名称列表,如何创建一个变量名称为键的字典(对于变量的值)? - Given a list of variable names in Python, how do I a create a dictionary with the variable names as keys (to the variables' values)? 如何创建和打印一个字典,其中键作为列表中的名称,其值作为名称出现在列表中的次数 - How to create and print a Dictionary that has keys as the names in list and their values as number of times the name appears on the list 打印列表值的字典 - Print dictionary of list values 将字典输出打印到列表中 - print dictionary output into list 使用带有 eval() 的变量名字典 - Using a dictionary of variable names with eval() 变量名和值字符串到字典 - Variable names and values string to dictionary 如何从字典中为每个进程打印进程名称列表及其值? - How can I print a list of process names and their values from a dictionary for each process? 遍历文件名列表以查看文件名是否在字典值中(值是列表),然后打印键和文件名 - Iterate over list of file names to see if file name is in dictionary Values (Valuse are lists), then print Key and file name 将包含重复变量名称和变量值的类字典对象列表转换为 Pandas dataframe - Converting a list of dictionary-like objects containing repeated variable names and variable values to a Pandas dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM