如何从列表中删除从开始到结束的范围

def remove_section(alist, start, end):
    """
    Return a copy of alist removing the section from start to end inclusive

    >>> inlist = [8,7,6,5,4,3,2,1]
    >>> remove_section(inlist, 2, 5)
    [8, 7, 2, 1]
    >>> inlist == [8,7,6,5,4,3,2,1]
    True
    >>> inlist = ["bob","sue","jim","mary","tony"]
    >>> remove_section(inlist, 0,1)
    ['jim', 'mary', 'tony']
    >>> inlist == ["bob","sue","jim","mary","tony"]
    True
    """

我有点难过如何去做这个任何帮助将非常感激.

最佳答案 这应该做你想要的:

def remove_section(alist, start, end):
    return alist[:start] + alist[end+1:]
点赞