一个小的教程程序有段代码不明白什么意思
这是一个简单的建立名字索引的程序,我想红色字体的代码有什么用呢,我感觉加不加它们结果都是一样的
data={}data['first']={'duncan': ['duncan Lie deng']}data['middle']={'Lie': ['duncan Lie deng']}data['last']={'deng': ['duncan Lie deng']}people={}full_name='Simth Will'names = full_name.split()if len(names)==2: names.insert(1,'')labels=('first','middle','last')for label,name in zip(labels,names):[color=#FF0000] people=data[label].get(name) if people: people.append(full_name) else:[/color] data[label][name]=[full_name]print data
data={ 'first': {'duncan': ['duncan Lie deng']}, 'middle': {'Lie': ['duncan Lie deng']}, 'last': {'deng': ['duncan Lie deng']}, }labels = ('first','middle','last')people, full_name = {}, 'Simth Will'names = full_name.split()if len(names)==2: names.insert(1,'')for label,name in zip(labels,names): people=data[label].get(name) # 此时的变量`people`不再是初始化的{}, 而是data[label]中取到的列表。 # 建议取消前面的people初始化 # 并将此句替换为: people = data[label].get(name, []) # 以取消歧义 if people: people.append(full_name) else: data[label][name]=[full_name] print data
[解决办法]
就现有数据而言“这个people得到的值不是None吗?貌似在这个程序中永远不会运行”,但当遇到适当的值时,people将可能按提供的full_name取到适合的列表
[解决办法]
不是有地方用到了嘛......