将数据添加到最后:
aList=[1,3,5,7,9,2,4,-1,-1,-1]#10个数 x=int(input('请输入一个整数:')) i=len(aList)-1#i=9 while i>=0: if aList[i]!=-1: break i=i-1 aList[i+1]=x for x in aList: if x!=-1: print(x)
将数据依顺序排序后,添加到顺序位置,使顺序规律不变:
aLi=[1,3,5,7,9,2,4,10000,10000,10000]#10个数,10000代表空数据 aList=sorted(aLi) print('从小到大排序:',aList) # aList=[1,3,5,7,9,12,14,-1,-1,-1] x=int(input('请输入一个整数:')) i=len(aList)-1#i=9 while i>=0: if aList[i]==10000: pass elif aList[i]>x: aList[i+1]=aList[i] else:break i-=1 aList[i+1]=x for x in aList: if x!=-1: print(x)