返回列表 发帖

[问题求助] 【已解决】python3 小数无法使用冒泡排序法排序

本帖最后由 bingxing8000 于 2018-4-28 16:59 编辑
lst = [3.5,1.5,0,6.8,7.8,1.5]
for i in range(len(lst)):
    for j in range(i):
        if lst[j] > lst[j + 1]:
             lst[j],lst[j + 1]= lst[j + 1],lst[j]
print (lst)COPY
结果
[0, 1.5, 3.5, 6.8, 1.5, 7.8]COPY
请问,如何正确的排序
赞成“拿来主义”,但是鄙视“伸手党”

使用sorted排序
lst = [3.5,1.5,0,6.8,7.8,1.5]
lst1=sorted(lst)
print(lst1)COPY
结果
[0, 1.5, 1.5, 3.5, 6.8, 7.8]COPY
赞成“拿来主义”,但是鄙视“伸手党”

TOP

明明是代码写错了。
去学去写去用才有进步。安装python3代码存为xx.py 双击运行或右键用IDLE打开按F5运行

TOP

原来代码是抄的啊。抄了一个假的算法。
去学去写去用才有进步。安装python3代码存为xx.py 双击运行或右键用IDLE打开按F5运行

TOP

回复 4# codegay

确实是借鉴别的代码。
赞成“拿来主义”,但是鄙视“伸手党”

TOP

去学去写去用才有进步。安装python3代码存为xx.py 双击运行或右键用IDLE打开按F5运行

TOP

回复 6# codegay

哈哈,感觉到了浓浓的鄙视。
赞成“拿来主义”,但是鄙视“伸手党”

TOP

返回列表