[英]Following a python learning tutorial but my code isn't showing the correct output and I can't figure out why
[英]I write myself a little bubble sorting code while self-learning python, but couldnt figure out why it doesnt work
我被教导要按照以下方式改进代码,但是bubble_sort_1函数仍未返回排序列表,我看不到我错误分配了变量的代码的哪一部分,因为现在它只是返回相同的原始列表
当我将其复制时,压痕似乎被弄乱了,希望您能理解我在这里写的内容
学生= [
('tiffany','A',15),
('jane','B',10),
('ben','C',8),
('simon','A',15),
('john','A',21),
('jimmy','F',1),
('charles','C',9),
('freddy','D',4),
('dave','B',12)]
def bubble_sort_1(列表):
def swap(item1,item2):
temp=item1
list[list.index(item1)]=item2
list[list.index(item2)]=temp
for i in range(len(list)-1):
if list[i][2]>list[i+1][2]:
swap(list[i],list[i+1])
def bubble_sort_2(列表):
def swap(index1,index2):
temp=list[index1]
list[index1]=list[index2]
list[index2]=temp
for i in range(len(list)-1):
if list[i][2]>list[i+1][2]:
swap(i,(i+1))
def bubble_sort_2_improved(列表):
def swap(index1,index2):
temp=list[index1]
list[index1]=list[index2]
list[index2]=temp
still_need_sorting=True
while still_need_sorting==True:
still_need_sorting=False
for i in range(len(list)-1):
if list[i][2]>list[i+1][2]:
swap(i,(i+1))
still_need_sorting=True
bubble_sort_2_improved(学生)
为学生中的i:打印(i)
尝试这个
def bubble_sort(L):
"""(list)->list
sort the items of list and return list
>>> LL=[7,3,5,2]
>>> bubble_sort(LL)
[2, 3, 5, 7]
"""
#index of last element of the list
end=len(L)-1
while end!=0:
# buuble sort passes to the list and move one large item at end of list
for i in range(end):
if L[i]>L[i+1]:
L[i],L[i+1]=L[i+1],L[i]
end=end-1
return L
>>> students= [
('tiffany','A',15),
('jane','B',10),
('ben','C',8),
('simon','A',15),
('john','A',21),
('jimmy','F',1),
('charles','C',9),
('freddy','D',4),
('dave','B',12)]
>>> bubble_sort(students)
它给出输出
[('ben','C',8),('charles','C',9),('dave','B',12),('freddy','D',4),( 'jane','B',10),('jimmy','F',1),('john','A',21),('simon','A',15),('tiffany ','A',15)]
数据结构是元组列表
您的swap()
函数无法正常运行。 之后:
list[list.index(item1)]=item2
然后,当下一行使用list.index(item2)
它可能会找到您刚刚将item2
放入其中的索引,而不是其原始索引。 在重新分配它们之前,应该都获得两个索引。
def swap(item1, item2):
index1 = list.index(item1)
index2 = list.index(item2)
list[index1] = item2
list[index2] = item1
但是,您对list.index()
所有使用都是非常糟糕的方法,因为它必须反复搜索列表。 这也意味着列表不能有重复项,因为index()
只会找到第一个匹配项。 相反,您应该遍历索引并将它们传递给swap()
函数。
for i in range(len(list)-1):
student = list[i]
next = list[i+1]
if student[2] > next[2]:
swap(i, i+1)
我试图在其他方法可以导致更好的结果的地方重写您的代码,同时保持您的总体想法。 如果有什么事情无法识别,请随时提出。
要点如下:
if..else
代替try..except
另外,请尽量不要重复使用现有名称(如list
和sorted
,这会导致非常奇怪且难以调试的错误。
students = [ ('tiffany', 'A', 15), ('jane', 'B', 10), ('ben', 'C', 8), ('simon', 'A', 15), ('john', 'A', 21), ('jimmy', 'F', 1), ('charles', 'C', 9), ('freddy', 'D', 4), ('dave', 'B', 12)] def bubble_sort(to_sort): def swap(index_1, index_2): # just use indices directly temp = to_sort[index_1] # this step was missing to_sort[index_1] = to_sort[index_2] to_sort[index_2] = temp we_are_done = False # slightly more obvious than keeping a count while not we_are_done: we_are_done = True # this looks weird, but sadly we don't have do..while in python for index in range(len(to_sort) - 1): # this also avoids the checking-if-next-exists if to_sort[index][2] > to_sort[index+1][2]: we_are_done = False swap(index, index+1) bubble_sort(students) # this function sorts in-place, nothing to return print (students)
打印:
[('jimmy', 'F', 1), ('freddy', 'D', 4), ('ben', 'C', 8), ('charles', 'C', 9), ('jane', 'B', 10), ('dave', 'B', 12), ('tiffany', 'A', 15), ('simon', 'A', 15), ('john', 'A', 21)]
您的代码有2个问题
除此之外,由于列表是可变的,因此您无需将最终结果分配给“已排序”,因为原始的“学生”已经拥有正确的输出
此代码有效:
students= [
('tiffany','A',15),
('jane','B',10),
('ben','C',8),
('simon','A',15),
('john','A',21),
('jimmy','F',1),
('charles','C',9),
('freddy','D',4),
('dave','B',12)]
def bubble_sort(list):
def swap(item1,item2):
pos1 = list.index(item1)
pos2 = list.index(item2)
list[pos1] = item2
list[pos2]=item1
count=0
for student in list:
try:
next=list[(list.index(student))+1]
if student[2]>next[2]:
swap(student,next)
count+=1
except IndexError:
break
if count==0 :
return list
else:
return bubble_sort(list)
sorted=bubble_sort(students)
print (sorted)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.