Contents
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 初始化数据库
def init(data):
data['first'] = {}
data['middle'] = {}
data['last'] = {}
# 查询数据库中标签和名(First,Middle, Last)
def lookup(data,label,name):
return data[label].get(name)
def store(data, *full_names):
for full_name in full_names:
names = full_name.split()
if len(names) == 2:
names.insert(1,'')
labels = 'first', 'middle', 'last'
# 联合标签和名字
for label,name in zip(labels,names):
# 调用lookup函数并将结果赋予people
people = lookup(data,label,name)
# 如果查询的结果非空,将对应的全名加入相应的label,name对应的全名集中
if people:
people.append(full_name)
# 若查询为空,将相应的labels和names以及对应的全名加入数据集中
else:
data[label][name] = [full_name]
Contents