Notes learning python "python programming quick start - let automate the tedious work"

Problem one : *
write a function, a list of values it as an argument and returns a string. The string contains all entries between the entry and comma-delimited space, and insert and before the last entry. For example, the spam list to the front of the transfer function will return 'apples, bananas, tofu, and cats'. But you should be able to handle the function passed to it any list.
Ideas: first inserted and then turned into a list of strings

spam = ['apples', 'bananas', 'tofu', 'cats']
def sentence(lists):
	new_list=lists[:]#复制列表,避免更改原变量
	new_list[-1]='and '+new_list[-1]#插入‘and’
	new_str=','.join(new_list)#把join方法列表变为字符串
	print(new_str)
	return new_str
函数完善还需要检测输入参数是否为列表加上try
Published 23 original articles · won praise 5 · Views 393

Guess you like

Origin blog.csdn.net/weixin_43287121/article/details/104478469