list = [4,2,6,9,8,7,1,5,3]
print(list[0:])
print(list[:-1])
print(list[5:])
print(list[:-3])
print(list[0:1])
print(list[1:-7])
print(list[-8:2])
print(list[::])
print(list[::1])
print(list[::2])
print(list[::3])
print(list[::-1])#if step<0 fetch the number from the right to the left, means 'reverse' here
print(list[::-2])
print(list[-2::-2])The answer
[4, 2, 6, 9, 8, 7, 1, 5, 3]
[4, 2, 6, 9, 8, 7, 1, 5]
[7, 1, 5, 3]
[4, 2, 6, 9, 8, 7]
[4]
[2]
[2]
[4, 2, 6, 9, 8, 7, 1, 5, 3]
[4, 2, 6, 9, 8, 7, 1, 5, 3]
[4, 6, 8, 1, 3]
[4, 9, 1]
[3, 5, 1, 7, 8, 9, 6, 2, 4]
[3, 1, 8, 6, 4]
[5, 7, 9, 2]简单的冒泡排序法练练手
# Bubble_sort
# 2018.5.24 in NEFU
n=10 # the number of the element of the list is n
list=list(range(n,0,-1))# the two 'list' is different !
for i in range(0,n-1):
for j in range(0,n-i-1):
if list[j] > list[j+1]:
list[j] , list[j+1] = list[j+1] , list[j]
print(list)

Python列表切片与冒泡排序示例
本文通过一系列Python代码示例介绍了列表切片的使用方法,并演示了如何利用冒泡排序算法对列表进行排序。从正向切片到逆向切片,再到指定步长的切片应用,文章全面覆盖了Python中列表切片的各种应用场景。
456

被折叠的 条评论
为什么被折叠?



