格式化要求:
namelist([ {'name': 'Bart'}, {'name': 'Lisa'}, {'name': 'Maggie'} ])
# returns 'Bart, Lisa & Maggie'
namelist([ {'name': 'Bart'}, {'name': 'Lisa'} ])
# returns 'Bart & Lisa'
namelist([ {'name': 'Bart'} ])
# returns 'Bart'
namelist([])
# returns ''
def namelist(names):
#your code here
n = len(names)
name_list = [e['name'] for e in names]
if n < 2:
return {
0: '',
1: '{}'
}[n].format(*name_list)
else:
return ', '.join(name_list[:-1]) + ' & ' + name_list[-1]
网友评论