编写一个函数,它以一个列表值作为参数,返回一个字符串,该字符串包含所有表项,表项之间用,隔开,并在最后一个表项之前插入and。如spam["apple",“bananas”],结果为“apple and bananas”
def func(temp_list):
string = ''
for index, temp in enumerate(temp_list):
if index == len(temp_list)-1:
string += ' and ' + temp
elif index == 0:
string += temp
else:
string += ',' + temp
return string
temp_list = ['apple', 'bananas', 'xxxx']
print(func(temp_list))
如果对你有帮助,请点击一下采纳谢谢
' and '.join(["apple","bananas"])