# python使用for循环对列表内元素进行排序方法

list = [13, 22, 6, 99, 11]
for m in range(len(list)-1):
for n in range(m+1, len(list)):
if list[m]> list[n]:
temp = list[n]
list[n] = list[m]
list[m] = temp
print list

[6, 11, 13, 22, 99]

list = [13, 22, 6, 99, 11]
for i in range(len(list)-1):
for j in range (i+1,len(list)):
# print str(i)
# print “init–” + str(j)
print ‘本次进行比较的list下标值’ + str(i),str(j)
if list[i] > list[j]:
# print i,j
# print ‘—–‘ + str(list[i]) + str(list[j]) + ‘——-‘
t = list[j]
list[j]= list[i]
list[i]=t
# print t
# print list[i],list[j]
print ‘********内部的一次循环***************’
print list
print list
print ‘++++++++本次内部循环结束+++++++\n\n’

–从打印信息可以看出循环过程–

[13, 22, 6, 99, 11]

********内部的一次循环***************
[6, 22, 13, 99, 11]

[6, 22, 13, 99, 11]

[6, 22, 13, 99, 11]
[6, 22, 13, 99, 11]
++++++++本次内部循环结束+++++++

********内部的一次循环***************
[6, 13, 22, 99, 11]

[6, 13, 22, 99, 11]

********内部的一次循环***************
[6, 11, 22, 99, 13]
[6, 11, 22, 99, 13]
++++++++本次内部循环结束+++++++

[6, 11, 22, 99, 13]

********内部的一次循环***************
[6, 11, 13, 99, 22]
[6, 11, 13, 99, 22]
++++++++本次内部循环结束+++++++

********内部的一次循环***************
[6, 11, 13, 22, 99]
[6, 11, 13, 22, 99]
++++++++本次内部循环结束+++++++

Posted in 未分类