List Slicing in Python Without Using Built

I'm very new to programming and am currently stuck on a practice problem involving list slicing without using the built-in function, so the idea is that you create your own slice function to be called upon by the user. Below is the question and following that is my attempt to solve it. Any help is appreciated. Thank you!

Python provides slicing functionality for lists, but for this question, you will implement your own function capable of producing list slices (note: you cannot use the slicing operator in your solution). The function should be called slice and take the following three inputs in this specific order:

  • A list, source, which the slice will be created from. This list cannot be modified by your function.
  • A positive integer, start, representing the starting index of the slice you will create. If this value is not in the range [0, len(list)-1], your function should return an empty list.
  • A positive integer, end, representing the ending index of the slice you will create. If this value is not in the range [start, len(list)-1], your function should return an empty list.
    If the parameter values are acceptable, your function will return a list that contains the items from source beginning at the index start and ending at the index end (inclusive). This is different from the Python slice operator, as the item at the index end is also included in the new list.
  • Examples:

    mylist = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]  
    slice(mylist, 0, 9) should be ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"] slice(mylist, 3, 4) should be ["D", "E"]  
    slice(mylist, 4, 3) should be [ ]  
    slice(mylist, 3, 8) should be ["D", "E", "F", "G", "H", "I"]  
    slice(mylist, 4, 4) should be ["E" ]
    

    My code:

    source=int(input("Enter a list: "))  
    start=int(input("Enter the starting digit: "))  
    end=int(input("Enter the ending digit: "))  
    
    def slice(source,start,end):  
            if start is not [0, len(source)-1]:
                print()  
            elif end is not [start, len(source)-1]:  
                print()  
        source.append(0,start)  
        source.append(len(source),end)  
    
    slice[]
    

    想象出它自己,谢谢你试图帮助,而不是居高临下@lmiguelvargasf我真的很感激。

    start = int(input(" Enter the starting index of the slice you will create: "))
    end = int(input("Enter the ending index of the slice you will create:  "))
    def slice(source, start, end):
        myList = []
        for i in range(start, end+1):
            if start in range(0, len(source)) and end in range(start,     len(source)):
            myList.append(source[i])
    return myList
    
    myList = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'] #This is the source
    print(slice(myList,start,end))
    
    链接地址: http://www.djcxy.com/p/26762.html

    上一篇: 具有负面和正面步骤的扩展切片的默认值是多少?

    下一篇: 在不使用内建的情况下在Python中列表切片