列表实例

网友投稿 1034 2022-09-08

列表实例

列表实例

1、去除重复数

def eliminateDuplicates(lst): result=[] for i in lst: if i not in result: result.append(i) return resultdef main(): lst = [1, 2, 3, 2, 1, 6, 3, 4, 5, 2] list = eliminateDuplicates(lst) print(list)main()

2、冒泡排序

# 冒泡排序import randomdef BubbleSort(lst): GoOnFlag=True m=len(lst)-1 while m>0 and GoOnFlag: GoOnFlag=False for i in range(m): if lst[i]>lst[i+1]: lst[i],lst[i+1]=lst[i+1],lst[i] GoOnFlag=True m-=1def main(): lst=[x for x in range(12)] random.shuffle(lst) print(lst) BubbleSort(lst) print(lst)main()

3、双向冒泡排序

# 双向冒泡排序:从小到大import randomdef BubbleSort(lst): left = 0 right = len(lst) - 1 while left < len(lst)-1: for i in range(left + 1, len(lst)): if lst[i] < lst[left]: lst[i], lst[left] = lst[left], lst[i] left += 1 for i in range(right-1 , left-1,-1): if lst[i] > lst[right]: lst[i], lst[right] = lst[right], lst[i] right-=1 return lstdef main(): lst = [x for x in range(12)] while True: random.shuffle(lst) print(lst) BubbleSort(lst) print(lst) main()

4、冒泡排序动态

from tkinter import *import timeimport randomlistLength = 50height = 600width = 800class BubbleSort(): def __init__(self, mywin): self.win = mywin self.list = [x for x in range(1, listLength+1)] self.barwidth = (width-20)//listLength self.height = (height-30)//listLength self.left= (width-listLength*self.barwidth)//2 self.reset() def reset(self): random.shuffle(self.list) self.drawRect(-1,50) def drawRect(self,move,flag): self.win.canvas.delete("line") for i in range(len(self.list)): color = "white" if i == move: color = "RED" if i > flag: color = "BLUE" self.win.canvas.create_rectangle(i * self.barwidth+self.left, height-10, (i+1) * self.barwidth+self.left, height-10-self.height * self.list[i], fill=color, tag="line") self.win.canvas.create_text(i * self.barwidth+self.left+self.barwidth/2, height-18-self.height * self.list[i], text=str(self.list[i]), tag="line") self.win.canvas.after(100) self.win.canvas.update() def sort(self): for i in range(0,len(self.list)-1): for j in range(0,len(self.list)-i-1): if self.list[j] > self.list[j+1]: temp = self.list[j] self.list[j] = self.list[j+1] self.list[j+1] = temp self.drawRect(j+1,len(self.list)-i-1)class SortWin(): def __init__(self): self.win = Tk() self.win.title("Bubble Sort") # Set a title self.canvas = Canvas(self.win, bg="white", width=width, height=height) self.canvas.pack() self.frame = Frame(self.win) self.frame.pack() self.label = Label(self.frame, text="冒泡排序法") self.label.pack(side=LEFT) self.btStep1 = Button(self.frame, text="Begin", command=self.begin) self.btStep1.pack(side=LEFT) self.btStep2 = Button(self.frame, text="Reset", command=self.reset) self.btStep2.pack(side=LEFT) def begin(self): self.bubbleSort.sort() def reset(self): self.bubbleSort.reset() def show(self): self.bubbleSort = BubbleSort(self) self.win.mainloop()main = SortWin()main.show()

5、选择排序

from tkinter import *import randomheight = 600width = 800class InsertSort(): def __init__(self, mywin, list): self.isStop=False self.window = mywin self.list = list self.listLength = len(self.list) self.barwidth = (width - 20) // self.listLength # 单位宽度 self.height = (height - 30) // self.listLength # 单位高度 self.left = (width - self.listLength * self.barwidth) // 2 self.reset() def reset(self): self.drawRect(-1, 0) def drawRect(self, current, finishFlag): if not self.isStop : self.window.canvas.delete("line") for i in range(len(self.list)): color = "white" if i == current: color = "RED" if 0<= i < finishFlag: color = "BLUE" self.window.canvas.create_rectangle(i * self.barwidth + self.left, height - 10, (i + 1) * self.barwidth + self.left, height - 10 - self.height * self.list[i], fill=color, tag="line") self.window.canvas.create_text(i * self.barwidth + self.left + self.barwidth / 2, height - 18 - self.height * self.list[i], text=str(self.list[i]), tag="line") self.window.canvas.after(100) self.window.canvas.update() def sort(self): for i in range(len(self.list)): currentMin = self.list[i] currentMinIndex = i self.drawRect(i,i-1) for j in range(i + 1, len(self.list)): if currentMin > self.list[j]: currentMin = self.list[j] currentMinIndex = j self.drawRect(j,i) if currentMinIndex != i: self.list[currentMinIndex] = self.list[i] self.list[i] = currentMin self.drawRect(i,i) self.drawRect(i,len(self.list))class SortWin(): def __init__(self): self.window = Tk() self.window.title("Insert Sort") self.canvas = Canvas(self.window, bg="white", width=width, height=height) self.canvas.pack() self.frame = Frame(self.window) self.frame.pack() self.label = Label(self.frame, text="插入排序法") self.label.pack(side=LEFT) self.btStep1 = Button(self.frame, text="Begin", command=self.begin) self.btStep1.pack(side=LEFT) self.btStep2 = Button(self.frame, text="Reset", command=self.reset) self.btStep2.pack(side=LEFT) self.btExit= Button(self.frame, text="Exit", command=self.exit).pack(side=LEFT) self.btStop= Button(self.frame, text="Stop", command=self.stop).pack(side=LEFT) self.list = [x for x in range(1, 21)] random.shuffle(self.list) def begin(self): self.insertSort.isStop=False self.insertSort.sort() def reset(self): self.insertSort.isStop=False random.shuffle(self.list) self.insertSort = InsertSort(self, self.list) self.insertSort.sort() def show(self): self.insertSort = InsertSort(self, self.list) self.window.mainloop() def exit(self): self.insertSort.isStop=True self.window.quit() def stop(self): self.insertSort.isStop=True return self.listmain = SortWin()main.show()

转自​​​ https://baidu.com/link?url=XRzhHeuUn9eaPX22bWem5OWneJlSy71ytWhtXh_HQg4fLkfwxqVZCSDQSIz4SKtAE4YOv728AzDOVL35YzpAFywGIi5IGXi7r08DvVMscaS&wd=&eqid=ae4f2f4b00066b4d000000065e5dcf9f​​

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:windows下计划任务schtasks使用案例
下一篇:❤️七夕佳节,用Python制作表白神器,程序员也应该拥有爱情!【附源码,建议
相关文章

 发表评论

暂时没有评论,来抢沙发吧~