'''
3. 编写一个叫做db_headings的函数,要求能够找出内层字典中所用到的全部键,并以集合的形式返回。
以给出的字典为例,该函数应该返回集合{'author', 'forename', 'born', 'surname', 'notes', 'died'}。
{
'jgoodall':{'surname':'goodall',
'forename':'Jane',
'born':1934,
'died':None,
'notes':'primate researcher',
'author':['In the Shadow of Man','The Chimpanzees of Gombe']},
'rfranklin':{'surname':'Franklin',
'forename':'Rosalind',
'born':1920,
'died':1957,
'notes':'contributed to discovery of DNA'},
'rcarson':{'surname':'Carson',
'forename':'rachel',
'born':1907,
'died':1964,
'notes':'raised awareness of effects of DDT',
'author':['Silent Sporing']}
}
'''
def db_headings():
people = {
'jgoodall':{'surname':'goodall',
'forename':'Jane',
'born':1934,
'died':None,
'notes':'primate researcher',
'author':['In the Shadow of Man','The Chimpanzees of Gombe']},
'rfranklin':{'surname':'Franklin',
'forename':'Rosalind',
'born':1920,
'died':1957,
'notes':'contributed to discovery of DNA'},
'rcarson':{'surname':'Carson',
'forename':'rachel',
'born':1907,
'died':1964,
'notes':'raised awareness of effects of DDT',
'author':['Silent Sporing']}
}
l=[]
for name in people.values():
a = name
for name in a.keys():
l.append(name)
n=[]
for i in l:
if i not in n:
n.append(i)
print(n)
db_headings()
'''
#创建一个存储一个学生的信息,通过遍历可以取出所有信息
student={'name':'xiaoming','age':11,'school':'tsinghua'}
for key,value in student.items():
print(key+':'+str(value))
输出:
age:11
name:xiaoming
school:tsinghua
注意:
遍历出的返回值输出和存储的顺序不一样,输出顺序每次都会变化
在for循环中key和value两个变量需要使用逗号‘,’隔开
#取键
student = {'name': 'xiaoming', 'age': 11, 'school': 'tsinghua'}
for name in student.keys():
print(name)
输出
name
age
school
#keys()返回的值顺序是不确定的,如果想按序排列,可以使用sorted()进行排序
for name in sorted(people.keys()):
'''