0
点赞
收藏
分享

微信扫一扫

列表实例


1、去除重复数

def eliminateDuplicates(lst):
result=[]
for i in lst:
if i not in result:
result.append(i)
return result

def main():
lst = [1, 2, 3, 2, 1, 6, 3, 4, 5, 2]
list = eliminateDuplicates(lst)
print(list)

main()

列表实例_ide

2、冒泡排序

# 冒泡排序
import random
def 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-=1
def main():
lst=[x for x in range(12)]
random.shuffle(lst)
print(lst)
BubbleSort(lst)
print(lst)
main()

列表实例_冒泡排序_02

3、双向冒泡排序

# 双向冒泡排序:从小到大
import random

def 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 lst

def main():
lst = [x for x in range(12)]
while True:
random.shuffle(lst)
print(lst)
BubbleSort(lst)
print(lst)

main()

列表实例_ide_03

4、冒泡排序动态

from tkinter import *
import time
import random
listLength = 50
height = 600
width = 800

class 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()

列表实例_屏保_04

5、选择排序

from tkinter import *
import random

height = 600
width = 800


class 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.list


main = SortWin()
main.show()

列表实例_屏保_05

6.弹珠屏保(转载),比普通动画多了一个功能就是全屏

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

import random
import tkinter
import tkinter.messagebox


class RandomBall():
'''
单个球定义、运动的类
'''
def __init__(self, root_canvas, width, height):
'''
参数说明:
canvas:从ScreenSaver类中传入的画布
width,height:从SS类中传入的宽高,即屏幕宽高
'''

# 将传入变量赋为RB类的属性
self.canvas = root_canvas
self.width = width
self.height = height

# 随机生成球的中心坐标
self.xcenter = random.randint(100, width-100)
self.ycenter = random.randint(100, height-100)
# 随机生成球的运动速度
self.xvelocity = random.randint(8,16)
self.yvelocity = random.randint(8,16)
# 计算球的半径
self.radius = random.randint(60, 100)
# 利用十六进制随机数与lambda表达式生成球的颜色
# RGB表示法:三个数字,每个数字的值是0-255之间,表示红绿蓝三个颜色的大小
# 在某些系统中,直接用英文单词表示也可以,比如red,green
color = lambda : random.randint(0,255)
self.color = '#%02x%02x%02x' % (color(),color(),color())

# 创建球的函数
def create_ball(self):
'''
用构造函数定义的变量值,在canvas上画一个球
'''
# tkinter没有画圆形函数
# 只有一个画椭圆函数,画椭圆需要定义两个坐标,
# 在一个长方形内画椭圆,我们只需要定义长方形左上角和右下角就好
# 求两个坐标的方法是,已知圆心的坐标,则圆心坐标减去半径能求出
# 左上角坐标,加上半径能求出右下角坐标(向右x为正,向下y为正)
xleftup = self.xcenter - self.radius
yleftup = self.ycenter - self.radius
xrightdown = self.xcenter + self.radius
yrightdown = self.ycenter + self.radius
# 创建球
self.item = self.canvas.create_oval(xleftup,yleftup,
xrightdown,yrightdown,
fill=self.color,
outline=self.color)

# 球运动的函数
def move_ball(self):
# 计算球移动后的中心点坐标
self.xcenter += self.xvelocity
self.ycenter += self.yvelocity
# 当球与边框发生碰撞时,需要进行回弹操作,即对应方向的速度取负
if self.xcenter + self.radius >= self.width:
self.xvelocity = - self.xvelocity
if self.xcenter - self.radius <= 0:
self.xvelocity = - self.xvelocity
if self.ycenter + self.radius >= self.height:
self.yvelocity = - self.yvelocity
if self.ycenter - self.radius <= 0:
self.yvelocity = - self.yvelocity
# 在canvas上移动球,前提是create_ball已经调用
self.canvas.move(self.item, self.xvelocity, self.yvelocity)



class ScreenSaver():
'''
屏保定义类
程序启动
'''
def __init__(self):
# 创建球存储列表
self.balls = []
# 随机生成球的数量
self.num = random.randint(10,20)

# 利用tkinter生成root窗口
self.root = tkinter.Tk()
# 获取屏幕宽、高尺寸
root_w, root_h = self.root.winfo_screenwidth(), self.root.winfo_screenheight()
# 取消边框
self.root.overrideredirect(1)
# 绑定退出函数与相应动作
self.root.bind('<Motion>', self.myquit)
self.root.bind('<Key>', self.myquit)
self.root.bind('<Any-Button>', self.myquit)

# 创建画布,配置尺寸与颜色属性
self.canvas = tkinter.Canvas(self.root, width=root_w, height=root_h)
self.canvas.pack()

# 利用循环与RandomBall类在画布上画球,并append到列表中
for i in range(self.num):
ball = RandomBall(self.canvas, width=root_w, height=root_h)
ball.create_ball()
self.balls.append(ball)

# 调用球运动函数
self.run_screen_saver()
# 启用tkinter时间消息循环mainloop
self.root.mainloop()

# 球运动函数
def run_screen_saver(self):
# 循环实例化的ball调用move_ball函数
for ball in self.balls:
ball.move_ball()
# 使用after实现递归,通过不断调用各球的move_ball函数,实现位置刷新
self.root.after(50, self.run_screen_saver)

# 停止运行
# 此处e只是利用了事件处理机制,际上并不关心事件的类型
def myquit(self, e):
# 扩展:
# 此屏保程序扩展成,一旦捕获事件,则判断屏保不退出
# 显示一个Button,Button上显示事件类型,点击Button后屏保才退出
if tkinter.messagebox.askokcancel("彩球碰撞", '确定退出?'):
self.root.destroy()
else:
pass


if __name__ == '__main__':
# 启动屏保
ScreenSaver()

列表实例_屏保_06


举报

相关推荐

0 条评论