我对编程非常陌生,而且我目前仍然坚持使用列表切片的练习题而不使用内置函数,所以我们的想法是创建自己的切片函数以供用户调用。以下是问题,以下是我试图解决它。任何帮助表示赞赏。谢谢!
Python为列表提供切片功能,但是对于这个问题,您将实现自己的能够生成列表切片的函数(注意:您不能在解决方案中使用切片运算符)。该函数应称为slice,并按以下特定顺序获取以下三个输入:
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" ]
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[]
答案 0 :(得分:-1)
自己想出来,感谢你试图帮助而不是屈尊俯就@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))