列表

详情


NP69. 姓名与学号

描述

创建一个依次包含键-值对{'name': 'Niuniu'和'Student ID': 1}的字典my_dict_1,
创建一个依次包含键-值对{'name': 'Niumei'和'Student ID': 2}的字典my_dict_2,
创建一个依次包含键-值对{'name': 'Niu Ke Le'和'Student ID': 3}的字典my_dict_3,
创建一个空列表dict_list,使用append()方法依次将字典my_dict_1、my_dict_2和my_dict_3添加到dict_list里,
使用for循环遍历dict_list,对于遍历到的字典,使用print()语句一行输出类似字符串"Niuniu's student id is 1."的语句以打印对应字典中的内容。

输入描述

输出描述

按题目描述进行输出即可。
Niuniu's student id is 1.
Niumei's student id is 2.
Niu Ke Le's student id is 3.

原站题解

Python 解法, 执行用时: 10ms, 内存消耗: 2836KB, 提交时间: 2022-06-21

my_dict_1={'name': 'Niuniu','Student ID': 1}
my_dict_2={'name': 'Niumei','Student ID': 2}
my_dict_3={'name': 'Niu Ke Le','Student ID': 3}
dict_list=[]
dict_list.append(my_dict_1)
dict_list.append(my_dict_2)
dict_list.append(my_dict_3)
for i in dict_list:
    #print i 
    print "%s's student id is %d."%(i['name'],i['Student ID'])

Python 解法, 执行用时: 10ms, 内存消耗: 2848KB, 提交时间: 2022-07-22

my_dict_1 = {'name':'Niuniu','Student ID':1}
my_dict_2 = {'name':'Niumei','Student ID':2}
my_dict_3 = {'name':'Niu Ke Le','Student ID':3}
my_dict = []
my_dict.append(my_dict_1)
my_dict.append(my_dict_2)
my_dict.append(my_dict_3)
for i in my_dict:
    print("{}'s student id is {}.".format(i['name'],i['Student ID']))

Python 解法, 执行用时: 10ms, 内存消耗: 2848KB, 提交时间: 2022-07-17

my_dict_1 = {'name': 'Niuniu','Student ID': 1}
my_dict_2 = {'name': 'Niumei','Student ID': 2}
my_dict_3 = {'name': 'Niu Ke Le','Student ID': 3}
dict_list = []
dict_list.append(my_dict_1)
dict_list.append(my_dict_2)
dict_list.append(my_dict_3)
for i in dict_list:
    print("%s's student id is %d."%(i['name'],i['Student ID']))

Python 解法, 执行用时: 10ms, 内存消耗: 2848KB, 提交时间: 2022-06-10

a = {"name": "Niuniu", "Student ID": 1}
b = {"name": "Niumei", "Student ID": 2}
c = {"name": "Niu Ke Le", "Student ID": 3}
d = [a, b, c];

for a in d:
    print("{}'s student id is {}.".format(a["name"], a["Student ID"]));

Python 解法, 执行用时: 10ms, 内存消耗: 2852KB, 提交时间: 2022-07-13

my_dict_1={'name':'Niuniu','Student ID':1}
my_dict_2={'name':'Niumei','Student ID':2}
my_dict_3={'name':'Niu Ke Le','Student ID':3}
dict_list=[]
dict_list.append(my_dict_1)
dict_list.append(my_dict_2)
dict_list.append(my_dict_3)
for i in dict_list:
    print("{0}'s student id is {1}.".format(i["name"],i["Student ID"]))

上一题