繁体   English   中英

如何返回列表中最后一项重复的列表?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM