零星知识Python
1 s=input().split()
2 a,b,c=int(s[0]),int(s[1]),eval(s[2])
3print(a+c+b)
4
5for i in range(26):
6print(chr(ord('a')+i),end='')
7
8 n=int(input())
9for i in range(1,n+1):
10if n%i:
11print(i, end='')
12
13for i in range(5,0,-1):
14print(i)
15
16 count=0
17while count<9:
18print(count,'⼩于9')
19 count+=1
20else:
21print(count,'⼤于等于9')
22
23 num=input()
24 lst=num.split()
25 k=int(lst[0])
26
27try:
28while True:
29for i in lst:
30 k=max(k,int(i))
31print(k)
32 lst=input().split()
33except:
34print('error')
35print(k)
36
37 n=int(input())
38 k=1
39 s=0
40for i in range(1,1+n):
41 k*=ipython正则表达式判断
42 s+=k
43print(s)
44
45# ⽬标:算出⼀组数的最⼤跨度,
46# ⽅法:通过列表函数操作将列表的数据格式转化,进⾏跨度运算
47 lsi=input().split()
48 lst=[]
49for i in range(len(lsi)):
50 lst.append(int(lsi[i]))
51 kuadu=max(lst)-min(lst)
52print(kuadu)
53
54# ⾓⾕猜想
55 n=int(input())
56while n!=1:
57if n%2==0:
58print('{0:.0f}/2={1:.0f}'.format(n,n/2))
59 n=n/2
60elif n%2!=0:
61print('{0:.0f}*3+1={1:.0f}'.format(n,3*n+1))
62 n=3*n+1
63print('End')
64
65# 数2
66 k=input().split()
67 l,r=int(k[0]),int(k[1])
68 count2=0
69for i in range(l,r+1):
70 count2+=str(i).count('2')
71print(count2)
72
73# 递归奇异三⾓形
74import turtle
75
76
77def draw(points):
78# 根据三个坐标,画⼀个三⾓形
79 t.penup()
80 t.goto(points['left'])
81 t.pendown()
82 t.goto(points['top'])
83 t.goto(points['right'])
84 t.goto(points['left'])
85
86
87def getMid(p1, p2):
88# 获取两个坐标的中点的坐标
89return (p1[0] + p2[0]) / 2, (p1[1] + p2[1]) / 2
90
91
92def sierpinski(degree, points):
93 draw(points)
94if degree > 0:
95# 以原三⾓形左边底⾓坐标为起点,画⼀个边长为原来三⾓形边长⼀半的等边三⾓形
96 sierpinski(degree - 1, {'left': points['left'], 'top': getMid(points['left'], points['top']),
97'right': getMid(points['left'], points['right'])})
98
99# 以原三⾓形左边中点坐标为起点,画⼀个边长为原来三⾓形边长⼀半的等边三⾓形
100 sierpinski(degree - 1, {'left': getMid(points['left'], points['top']), 'top': points['top'],
101'right': getMid(points['top'], points['right'])})
102
103# 以原三⾓形底边中点坐标为起点,画⼀个边长为原来三⾓形边长⼀半的等边三⾓形
104 sierpinski(degree - 1,
105 {'left': getMid(points['left'], points['right']), 'top': getMid(points['top'], points['right']), 106'right': points['right']})
107
108
109 t = turtle.Turtle()
110 t.speed(3) # 画笔速度
111# 初始的三⾓形的三个顶点
112 points = {'left': (-200, -100), 'top': (0, 200), 'right': (200, -100)}
113 sierpinski(1, points)
114
115 turtle.done()
116#---------------------------------------------------------------------------------------------------------------------------------------117# isinstance判断数据类型
118 a=[1,2,3,5,8,9]
119print(isinstance(a,list))
120#---------------------------------------------------------------------------------------------------------------------------------------121# 指针概念
122 a=[1,2,3]
123 b=[1,2,3]
124 a=b
125 b[0]=5
126
127print(a)
128#---------------------------------------------------------------------------------------------------------------------------------------129# 正则表达式,多重分隔符
130 a='Beautiful, is; better*than\nugly'
131print(re.split(';| |,|\*|\n| ',a))
132#---------------------------------------------------------------------------------------------------------------------------------------133# #异常处理,及捕获
134 s='123asde7988794ffd'
135try:
136 s.index('abf')
137except Exception as e:
138print(e)
139#---------------------------------------------------------------------------------------------------------------------------------------140
141
142#列表排序
143#1.定义函数制定功能进⾏排序
144def selectionsort(n):
145for i in range(len(n)-1):
146for j in range(i,len(n)):
147if n[i]>n[j]:
148 n[i],n[j]=n[j],n[i]
149print(n)
150 selectionsort([5,68,79,49,42,855,5,5,48,99,40,5,6])
151 a=[15,8,5,9,8,7,46,5]
152 a.sort()
153print(a)
154
155#2.列表函数进⾏排序
156 a.sort(reverse=True)#reverse进⾏排序反转
157print(a)
158#---------------------------------------------------------------------------------------------------------------------------------------159#3.在 sort 中使⽤ key 进⾏逐次调⽤⾃定义函数排序
160def myKey(x):#此法也可⽤于多级排序
161return x%10
162 a=[32,5,8,9,7,4,1]
163 a.sort(key=myKey)
164print(a)
165#---------------------------------------------------------------------------------------------------------------------------------------
166
167print(sorted('This is my lover'.split(),key=str.lower))#语法:sorted(iterable(可迭代对象), key=None, reverse=False)
168# #sort 与 sorted 区别:
169# sort 是应⽤在 list 上的⽅法,sorted 可以对所有可迭代的对象进⾏排序操作。
170# list 的 sort ⽅法返回的是对已经存在的列表进⾏操作,⽽内建函数 sorted ⽅法返回的是⼀个新的 list,⽽不是在原来的基础上进⾏的操作。171#时间复杂度均为O(nlog(n))
172#---------------------------------------------------------------------------------------------------------------------------------------
173#使⽤lambda进⾏定义排序
174 k=[(1,2,3),(1,1,1),(3,0,2),(3,2,1)]
175 k.sort(key=lambda x:x[1])
176print(k)
177#---------------------------------------------------------------------------------------------------------------------------------------
178#列表元组⾼级⽤法
179#列表:append,extend,insert,remove,reverse,index del map(dunction,sequence)
180#map(dunction,sequence) 将⼀个序列通过function映射到另⼀个序列(字元列..) ,返回延时求值对象即只能调⽤⼀次,
181def f(x):
182print(x,end='')
183return x*x
184 a=map(f,[1,2,3])
185print(list(a))
186print(tuple(a))
187#---------------------------------------------------------------------------------------------------------------------------------------
188#此法可以⽤于简化格式化输⼊
189
190 x,y,z=map(int,input().split())
191print(x,y,z)
192#---------------------------------------------------------------------------------------------------------------------------------------
193#filter 列表过滤语法:filter(function,sequence)返回⼀个function为true的序列(字元列..)
194
195def f1(x):
196return x%2==0
197 lst=list(filter(f1,[1,2,3,4,5,6,7,8]))
198print(lst)
199#---------------------------------------------------------------------------------------------------------------------------------------
200#列表⽣成式语法:[f(x) for x in range(m,n)]
201print([x*x for x in range(5,12)])
202print([m+n for m in'ABC'for n in'XYZ' ])
203print([[m+n for m in'ABC'] for n in'XYZ' ])
204
205 l=['hello','JAck','tom',12,8,0,89]
206print([i.lower() for i in l if isinstance(i,str)])
207print(sorted([i for i in l if isinstance(i,int)]))
208# #当然元组也可以有这样的式⼦
209print(tuple(x*x for x in range(3,9)))
210
211#---------------------------------------------------------------------------------------------------------------------------------------
212#⼆维列表
213#列表拷贝
214#1.浅拷贝(换壳不换内)
215 a=[1,2,3,4,5,6,7,[0]]
216 b=a[:]#切⽚操作⽣成⼀个新的列表,浅拷贝(仅仅拷贝指针不包含其内容的ID)
217print(id(a))
218print(id(b))
219 a[-1].append(5)
220print(a)
221print(b)
222print('其原因就是元素的指针并没有改变,故为浅拷贝,即⽣成新的外壳,不改变内在')
223print(id(a[-1]))
224print(id(b[-1]))
225# #2.深度拷贝 copy库
226import copy
227 a=[1,[2,3]]
228 b=copy.deepcopy(a)
229 b[1].append(5)
230print(a)
231print(b)
232# #---------------------------------------------------------------------------------------------------------------------------------------
233#⼆维列表处理例⼦:
234#像素灰度处理,给定⼀个n⾏m列的⼆维列表,表⽰图像包含像素点的⾏数和列数
235#接下来n⾏,每⾏m个整数,表⽰图像的每个像素点的灰度,相邻两个整数之间⽤单个空格隔开,每个元素均在0~255
236#输出新灰度值为四周像素点灰度值不变,中间新灰度值为其及其上下左右五点的平均值,若为⼩数则四舍五⼊
237import copy
238 n,m=map(int,input().split())
239 a=[]
240for i in range(n):
241 a.append(list(map(int,input().split())))
242 b=copy.deepcopy(a)
243for i in range(1,n-1):
244for j in range(1,m-1):
245 b[i][j]=round((b[i][j]+b[i][j-1]+b[i][j+1]+b[i-1][j]+b[i+1][j])/5)
246print(b)
247for i in range(0,n):
248for j in range(0,m):
249print(b[i][j],end='')
250print()
251# #--------------------------------------------------------------------------------------------------------------------------------------- 1 with open(r'C:\Users\DELL\','w',encoding='utf-8') as f:
2for i in range(1,10):
3for j in range(1,i+1):
4if i==3 and j==2 or i==4 and j==2:
5 f.write(f'{j}*{i}={i*j}'),f.write('')
6print(f'{j}*{i}={i*j}', end='')
7else:
8 f.write(f'{j}*{i}={i*j}'),f.write('')
9print(f'{j}*{i}={i*j}',end='')
10 f.write('\n')
11print()
#向⽂本⽂件中写⼊9*9乘法表
1print(list(enumerate(['jack','thor','tom','jsck','think'])))
2 [(0, 'jack'), (1, 'thor'), (2, 'tom'), (3, 'jsck'), (4, 'think')]
3print(list(enumerate('123456789')))
4 [(0, '1'), (1, '2'), (2, '3'), (3, '4'), (4, '5'), (5, '6'), (6, '7'), (7, '8'), (8, '9')]
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论