def insert_list(L,i,data):
length=len(L)
if i <1 or i>length:
return False
else:
for j in range(i-1,length)[::-1]:
L[j+1:j+2]=[L[j]]
L[i-1]=data
return L
if __name__ == '__main__':
L=[1,2,3,4,5]
insert_list(L,2,0)
print("插入元素:")
print(L)
def delete_list(L,i):
length=len(L)
if i<1 or i>length:
return False
else:
del L[i-1]
return L
if __name__ == '__main__':
L=[1,2,3,4,5]
delete_list(L,2)
print("删除元素:")
print(L)
def reverse_list(L):
i,j=0,len(L)-1
while i<j:
L[i],L[j]=L[j],L[i]
i,j=i+1,j-1
return L
if __name__ == '__main__':
L=[1,2,3,4,5]
reverse_list(L)
print("反转:")
print(L)
|