【问题标题】:Python 3 Infinite Loop With Bubble Sort带冒泡排序的 Python 3 无限循环
【发布时间】:2017-06-29 12:57:37
【问题描述】:

我有一个问题,我必须对数字列表进行排序,我做得很好。但是接下来我要将列表中最高的 5 个数字放入一个列表中,并且我必须注意有重复的数字。但是,当我尝试这样做时,没有任何打印。如果我尝试打印“计数”,则会打印出无限数量的递增数字。我究竟做错了什么?

list = [8,3,7,4,2,1,6,5,10,9,3,9,6,7,5]

def sortList(list):
    switch = 1
    temp = int(0)

    while (switch != 0):
        switch = 0
        for i in range(len(list)-1):
            if list[i] > list[i+1]:
                temp = list[i]
                list[i] = list[i+1]
                list[i+1] = temp
                switch = 1

#这是我的问题所在

    count = int(0) 
    expensive5 = []

    while count != 5:

        for i in range(len(list)-1, 0, -1):
            if float(list[i]) > float(list[i-1]):
                expensive5.append(list[i])
                count += 1
                #if i print count here, I get an infinite number of increasing numbers





     print(expensive5)


sortList(list)

【问题讨论】:

    标签: python python-3.x sorting infinite-loop bubble-sort


    【解决方案1】:

    您可以使用集合仅获取列表中的唯一元素,因为列表已排序,您可以获得最高的 5 个数字,如下所示:

    l = [8,3,7,4,2,1,6,5,10,9,3,9,6,7,5]
    
    def bubble_sort(l):
      changed = True
      while changed:
        changed = False
        for i in range(len(l) - 1):
          if l[i] > l[i+1]:
            l[i], l[i+1] = l[i+1], l[i]
            changed = True
      return l
    
    print(list(set(bubble_sort(l)))[-5:]) #[6, 7, 8, 9, 10]
    

    试试here!

    其他一般提示:

    【讨论】:

    • 我不推荐这个。集合不保证其内容的任何排序,因此虽然 CPython 当前可能会保留排序顺序,但您不应该依赖这种行为。因此,要使其正常工作,您需要在创建集合后再次排序。
    • OP 标记为 python 3.x docs.python.org/3/whatsnew/…
    • 我明白了,我没有意识到这一点。我仍然坚持我的说法(或者至少建议在答案中澄清这一点),因为 1)不能保证它可以在 python 3.6 之前的版本上工作,而且很多使用 python 3.x 的人都没有'不一定在 python 3.6 上,并且 2)转换为一个集合对于 OP 试图做的事情来说是不必要的昂贵
    【解决方案2】:

    我建议不要计数,而是简单地使用集合,然后使用切片来获取列表中所需的数字。

    expensive5 = bubble_sort(list(set(l)))[:-6:-1]
    print(expensive5)
    

    另外,我建议不要使用list 作为变量名,因为它是转换为list 类型的默认函数。这里我改用变量l

    l = [8,3,7,4,2,1,6,5,10,9,3,9,6,7,5]
    
    def sortList(l):
        switch = 1
        temp = int(0)
    
        while (switch != 0):
            switch = 0
            for i in range(len(l)-1):
                if l[i] > l[i+1]:
                    temp = l[i]
                    l[i] = l[i+1]
                    l[i+1] = temp
                    switch = 1
        return l
    

    【讨论】:

    • 我不推荐这个。集合不保证其内容的任何排序,因此虽然 CPython 当前可能保留排序顺序,但您不应该依赖这种行为。因此,要使其正常工作,您需要在创建集合后再次排序。
    • 从 python 3.6 开始,设置保留顺序。
    • 我明白了,我没有意识到这一点。我仍然坚持我的说法(或者至少建议在答案中澄清这一点),因为 1)不能保证它可以在 python 3.6 之前的版本上工作,而且很多使用 python 3.x 的人都没有'不一定在 python 3.6 上,并且 2)转换为一个集合对于 OP 试图做的事情来说是不必要的昂贵
    【解决方案3】:

    问题是您的 count 变量在第一次通过 for 循环时传递了 5,因此当 while 循环检查其条件时,count 永远不会等于 5。要解决此问题,您需要将检查移动到内部循环中,如下所示:

    count = int(0) 
    expensive5 = []
    
    for i in range(len(list)-1, 0, -1):
        if float(list[i]) > float(list[i-1]):
            expensive5.append(list[i])
            count += 1
            #if i print count here, I get an infinite number of increasing numbers
            if count == 5: break
    
    print(expensive5)
    

    【讨论】:

      猜你喜欢
      • 2012-09-17
      • 1970-01-01
      • 1970-01-01
      • 2016-08-31
      • 2018-05-05
      • 1970-01-01
      • 2021-01-29
      • 2019-06-20
      • 1970-01-01
      相关资源
      最近更新 更多