使用循环和数组排序最高到最低

时间:2015-09-22 05:56:07

标签: python arrays sorting

while Checked == False:
for Counter in range(1, NoOfRecords + 1):
    if Test[Counter] < Test[Counter + 1]:
        Temp = Test[Counter]
        Test[Counter] = Test[Counter + 1]
        Test[Counter + 1] = Temp
    if Test[1] > Test[2] > Test[3] > Test[3] > Test[4] > Test[5]:
        Checked = True
print(Test[1], Test[2], Test[3], Test[4], Test[5])

我的目标是,基本上将输入从最低到最高排序。在此之前,要求用户输入0到5之间的5个数字。

然而,在输入5个输入后,没有其他任何内容出现,没有打印。

你能提出一个改进吗?或者用于分类过程的不同类型的循环?感谢

编辑完整代码

import array
Test = array.array ("f", range(11))
Counter = int(1)
NoOfRecords = int(5)
Checked = False
Temp = int(0)
def Tempurature (Day,Lowest,Highest):
    ok = False
    while ok == False:
        try:
            Input = float(input(Day + " Tempurature:"))
            if Input >= Lowest and Input <= Highest:
                ok = True
                return Input
            else:
                print ("\nPlease enter a number between " + str(Lowest) + " and " + str(Highest))
        except:
            print ("\nPlease enter a valid number")

for Counter in range(1, NoOfRecords + 1):
    Test[Counter] = Tempurature ("Mid-Day", 0, 5)

while Checked == False:
    for Counter in range(1, NoOfRecords + 1):
        if Test[Counter] < Test[Counter + 1]:
            Temp = Test[Counter]
            Test[Counter] = Test[Counter + 1]
            Test[Counter + 1] = Temp
    if Test[1] > Test[2] > Test[3] > Test[3] > Test[4] > Test[5]:
        Checked = True

print(Test[1], Test[2], Test[3], Test[4], Test[5])

3 个答案:

答案 0 :(得分:1)

编辑:我的回答是错的,订单检查不是倒退,所以我改写了。

这次,请看:

if Test[1] > Test[2] > Test[3] > Test[3] > Test[4] > Test[5]:

哇,哇:

                       Test[3] > Test[3]

不可能发生。

让我们print(Test)进行一次运行:

Mid-Day Tempurature: 1
Mid-Day Tempurature: 2
Mid-Day Tempurature: 3
Mid-Day Tempurature: 4
Mid-Day Tempurature: 5
array('f', [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0])
array('f', [0.0, 2.0, 3.0, 4.0, 5.0, 6.0, 1.0, 7.0, 8.0, 9.0, 10.0])
array('f', [0.0, 2.0, 3.0, 4.0, 5.0, 6.0, 1.0, 7.0, 8.0, 9.0, 10.0])
array('f', [0.0, 3.0, 4.0, 5.0, 6.0, 2.0, 1.0, 7.0, 8.0, 9.0, 10.0])
array('f', [0.0, 3.0, 4.0, 5.0, 6.0, 2.0, 1.0, 7.0, 8.0, 9.0, 10.0])
array('f', [0.0, 4.0, 5.0, 6.0, 3.0, 2.0, 1.0, 7.0, 8.0, 9.0, 10.0])
array('f', [0.0, 4.0, 5.0, 6.0, 3.0, 2.0, 1.0, 7.0, 8.0, 9.0, 10.0])
array('f', [0.0, 5.0, 6.0, 4.0, 3.0, 2.0, 1.0, 7.0, 8.0, 9.0, 10.0])
array('f', [0.0, 5.0, 6.0, 4.0, 3.0, 2.0, 1.0, 7.0, 8.0, 9.0, 10.0])
array('f', [0.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0, 7.0, 8.0, 9.0, 10.0])
6.0 5.0 4.0 3.0 2.0

查看Test [5 + 1]的排序循环到达它从未输入的元素并将6.0拖到列表的前面。

但你说“我的目标是,基本上,将输入从最低到最高排序。”这表明你将它们从最高到最低排序。

  1. 为什么使用11元素数组作为5元素列表?
  2. 不需要int(0)int(5),0和5已经是整数。而且您不需要在顶部预先写入每个变量,只需要预先编写一些变量。
  3. 获取数字时while ok == false检查没有做任何有用的事情,因为你从while循环的中间返回函数。
  4. 交换比较,使它们从最低到最高排序。
  5. 将&lt; =添加到测试中,以便它也适用于相同的数字
  6. While Checked == False:更像是While not Checked:
  7. 所以:

    import array
    Test = array.array ("f", range(11))
    NoOfRecords = 5
    Checked = False
    
    def Temperature (Day,Lowest,Highest):
        while True:
            try:
                Input = float(input(Day + " Temperature:"))
                if Input >= Lowest and Input <= Highest:
                    return Input
                else:
                    print ("\nPlease enter a number between " + str(Lowest) + " and " + str(Highest))
            except ValueError:
                print ("\nPlease enter a valid number")
    
    for Counter in range(1, NoOfRecords + 1):
        Test[Counter] = Temperature ("Mid-Day", 0, 5)
    
    while not Checked:
        for Counter in range(1, NoOfRecords):
            if Test[Counter] > Test[Counter + 1]:
                Test[Counter]
                Test[Counter] = Test[Counter + 1]
                Test[Counter + 1] = Temp
    
        if Test[1] <= Test[2] <= Test[3] <= Test[4] <= Test[5]:
            Checked = True
    
    print(Test[1], Test[2], Test[3], Test[4], Test[5])
    

    然后,使用列表......

    Test = []
    NoOfRecords = 5
    Checked = False
    
    def Temperature(Day, Lowest, Highest):
        while True:
            try:
                Input = float(input(Day + " Temperature:"))
                if Input >= Lowest and Input <= Highest:
                    return Input
                else:
                    print("\nPlease enter a number between {0} and {1}".format(Lowest, Highest))
            except ValueError:
                print ("\nPlease enter a valid number")
    
    
    for i  in range(NoOfRecords):
        Test.append(Temperature ("Mid-Day", 0, 5))
    
    
    while not Checked:
        for i  in range(NoOfRecords-1):
            if Test[i] > Test[i + 1]:
    
                Test[i], Test[i+1] = Test[i+1], Test[i]
    
        if Test[0] <= Test[1] <= Test[2] <= Test[3] <= Test[4]:
            Checked = True
    
    print(Test)
    

答案 1 :(得分:1)

这似乎更简单,更pythonic。但如果你的目标是学习一些关于排序的东西,这将毫无用处......

Test = []
NoOfRecords = 5


def Tempurature(Day, Lowest, Highest):
    while True:
        try:
            Input = float(input(Day + " Tempurature:"))
            if Highest >= Input >= Lowest:
                return Input
            else:
                print("Please enter a number between {} and {}".format(
                      Lowest, Highest))
        except ValueError as exc:
            print("Please enter a valid number")
            # print(exc)

for Counter in range(NoOfRecords):
    Test.append(float(Tempurature("Mid-Day", 0, 5)))

print(Test)
print(sorted(Test, reverse=True))

答案 2 :(得分:0)

如果你坚持使用分类程序,我建议修改如下:

while Checked == False:
    for Counter in range(1, NoOfRecords):
        if Test[Counter] < Test[Counter + 1]:
            Temp = Test[Counter]
            Test[Counter] = Test[Counter + 1]
            Test[Counter + 1] = Temp
    if Test[1] >= Test[2] and Test[2] >= Test[3] and Test[2] >= Test[3] and Test[3] >= Test[4] and Test[4] >= Test[5]:
        Checked = True

这是因为您只想关注要排序的数组部分,因此for Counter in range(1, NoOfRecords)(因为您在Test[Counter + 1]循环中引用for,所以会考虑最后一个元素)。

另外,应该测试if条件中可能的相等性,以避免程序卡在while循环中的情况