[英]How to return a list with the last item of the list repeated?
我很困惑我将如何完成这个问题的后半部分。
写一个包含整数的列表项,如果列表中的项数是偶数,则返回提供的列表,否则,它返回列表但重复最后一项(为了使列表中的项数甚至)。
这是我到目前为止。
def balance_list(items):
if len(items) % 2 == 0:
return items
else:
return items.append
这能解决你的问题吗?
def balance_list(items):
if len(items) % 2 == 1:
items.append(items[-1])
return items
print(balance_list([1, 2, 3, 4, 5]))
快乐编码!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.