Python Bubble排序递归。我究竟做错了什么?

时间:2018-07-13 19:32:42

标签: python dictionary recursion bubble-sort

我正在上课的递归作业。我知道如何使用该公式,但是我相信在正确打印字典的过程中,我在做错什么。有人可以帮我弄清楚如何获得正确的输出吗?

[('4213', 'STEM Center', 0), ('4201', 'Foundations Lab', 1), ('4204', 'CS Lab', 2), ('4218', 'Workshop Room', 3), ('4205', 'Tiled Room', 4), ('Out', 'Outside', 5)]
[('4201', 'Foundations Lab', 1), ('4204', 'CS Lab', 2), ('4205', 'Tiled Room', 4), ('4213', 'STEM Center', 0), ('4218', 'Workshop Room', 3), ('Out', 'Outside', 5)]
[('4204', 'CS Lab', 2), ('4201', 'Foundations Lab', 1), ('Out', 'Outside', 5), ('4213', 'STEM Center', 0), ('4205', 'Tiled Room', 4), ('4218', 'Workshop Room', 3)]
[('4204', 'CS Lab', 2), ('4201', 'Foundations Lab', 1), ('Out', 'Outside', 5), ('4213', 'STEM Center', 0), ('4205', 'Tiled Room', 4), ('4218', 'Workshop Room', 3)]

相反,我得到了:

[('4213', 'STEM Center', 0), ('4218', 'Workshop Room', 3), ('4201', 'Foundations Lab', 1), ('4205', 'Tiled Room', 4), ('4204', 'CS Lab', 2), ('out', 'Outside', 5)]

无 没有


我已经弄清楚了这一点。

def recursiveSort(sensor_list,n,t): #Recursive Sort
if n == 0:
    return sensor_list
    for i in range(n-1):
        if sensor_list[i][t] > sensor_list[i + 1][t]:
        temp = sensor_list[i]
        sensor_list[i] =sensor_list[i + 1]
        sensor_list[i + 1] = temp

return recursiveSort(sensor_list,n - 1,t)

Dict = {'4213' : ('STEM Center', 0),
'4201' : ('Foundations Lab', 1),
'4204' : ('CS Lab', 2),
'4218' : ('Workshop Room', 3),
'4205' : ('Tiled Room', 4),
'out' :  ('Outside', 5),
}

sensor_list=[]

[ sensor_list.append((key,Dict[key][0],Dict[key][1])) for key in Dict ] #Adding values to a dictionary into a tuple using list comprehension

print sensor_list


print recursiveSort(sensor_list,len(sensor_list),0)

print recursiveSort(sensor_list,len(sensor_list),1)

print sensor_list

2 个答案:

答案 0 :(得分:1)

我使用如下所示的适当缩进代码运行并获得正确的输出。

def recursiveSort(sensor_list,n,t): #Recursive Sort
    if n == 0:
        return sensor_list
    for i in range(n-1):
        if sensor_list[i][t] > sensor_list[i + 1][t]:
            temp = sensor_list[i]
            sensor_list[i] =sensor_list[i + 1]
            sensor_list[i + 1] = temp

    return recursiveSort(sensor_list,n - 1,t)

Dict = {'4213' : ('STEM Center', 0),
'4201' : ('Foundations Lab', 1),
'4204' : ('CS Lab', 2),
'4218' : ('Workshop Room', 3),
'4205' : ('Tiled Room', 4),
'out' :  ('Outside', 5),
}

sensor_list=[]

[sensor_list.append((key,Dict[key][0],Dict[key][1])) for key in Dict ] #Adding values to a dictionary into a tuple using list comprehension

print sensor_list


print recursiveSort(sensor_list,len(sensor_list),0)

print recursiveSort(sensor_list,len(sensor_list),1)

print sensor_list

根据您得到的输出,我认为错误是因为for循环的缩进不正确。很有可能是这样。

if n == 0:
    return sensor_list
    for i in range(n-1):
        if sensor_list[i][t] > sensor_list[i + 1][t]:
            temp = sensor_list[i]
            sensor_list[i] =sensor_list[i + 1]
            sensor_list[i + 1] = temp

for循环根本没有执行,因为

  • 当n == 0时,因为return语句在循环之前
  • 当n!= 0时,因为for循环部分位于if块内部。

您的return recursiveSort(sensor_list,n - 1,t)语句也可能缩进。

答案 1 :(得分:0)

^((([!#$%&'*+\-/=?^_`{|}~\w])|([!#$%&'*+\-/=?^_`{|}~\w][!#$%&'*+\-/=?^_`{|}~\.\w]{0,}[!#$%&'*+\-/=?^_`{|}~\w]))[@]\w+([-.]\w+)*\.\w+([-.]\w+)*)$