- 论坛徽章:
- 0
|
- def init(data):
- data['first']={}
- data['middle']={}
- data["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):
- people=lookup(data,label,name)
- if people:people.append(full_name)
- else:data[label][name]=[full_name]
- d={}
- init(d)
复制代码 然后书里面是这么调用store的~- store(d,'luke Skywalker','Anakin Skywalker')
复制代码 于是,我想试试看能否从用户input输入。。。
问题1:但是。。input得到的是一个元组。。怎么才能转换为store需要的格式呢?
另外一个方法就是不用关键字参数,用列表代替。后半部修改为:- def store(data,full_names):
- print(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):
- people=lookup(data,label,name)
- if people:people.append(full_name)
- else:data[label][name]=[full_name]
- d={}
- init(d)
- a=input('names:').split(',')
- store(d,a)
- 有没更好的意见或者对问题1的解答呢?~?~?~?感谢~
- print(d)
复制代码 |
|