程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Tips accumulated by Python in question and answer channel (7)

編輯:Python

1. max(),min() Functional key Parameter application

Find the mode quickly :

>>> import random
>>> r = [rnd(1,9) for _ in range(20)]
>>> r
[3, 3, 5, 9, 1, 7, 4, 1, 1, 9, 6, 8, 3, 9, 8, 8, 6, 4, 6, 6]
>>> max(r, key=r.count)
6

Find the shortest password ( The first match ):

>>> from random import choices, randint as rnd
>>> s = ''.join((map(chr,[*range(ord('A'),ord('Z')+1),*range(ord('a'),ord('z')+1),*range(ord('0'),ord('9')+1),ord('-')])))
>>> p = [''.join(choices(s,k=rnd(3,10))) for _ in range(10)]
>>> p
['fiKYKBl', 'HZjwZUGg', 'pUaKkKtrE8', '2qet35e1Q', 'is34n11F', 'Jxed96', '9K8TOE', 'sqymdfx', 'SRjuq', 'J9TBu']
>>> min(p, key=lambda x:len(x))
'SRjuq'

Maximum number of digits 、 The least integer  :( The first matching number , And maximum 、 The minimum values are not necessarily equal )

>>> from random import choices, randint as rnd
>>> n = [int(''.join(map(str,choices(range(10),k=rnd(3,10))))) for _ in range(10)]
>>> n
[9345447, 1030757133, 8630, 293949, 497, 1206340275, 172, 950651, 983, 138]
>>> max(n, key=lambda x:len(str(x)))
1030757133
>>> min(n, key=lambda x:len(str(x)))
497
>>>
>>> max(n)
1206340275
>>> min(n)
138

 2. Lists groups of any specified length

def splitlist(lst,*group):
res,lst = [],lst[:]
for i in group:
t = []
for _ in range(i):
if lst: t.append(lst.pop(0))
else: break
if t: res.append(t)
if lst: res.append(lst)
return res
a = [1,2,3,4,5,6,7,10,11,12,20,21,23]
print(splitlist(a,8,4,2,1))
# [[1, 2, 3, 4, 5, 6, 7, 10], [11, 12, 20, 21], [23]]
print(splitlist(a,7,3,2,1))
# [[1, 2, 3, 4, 5, 6, 7], [10, 11, 12], [20, 21], [23]]
print(splitlist(a,7,3,2,2))
# [[1, 2, 3, 4, 5, 6, 7], [10, 11, 12], [20, 21], [23]]
print(splitlist(a,7,3,2))
# [[1, 2, 3, 4, 5, 6, 7], [10, 11, 12], [20, 21], [23]]
print(splitlist(a,7,3))
# [[1, 2, 3, 4, 5, 6, 7], [10, 11, 12], [20, 21, 23]]
print(splitlist(a,7,2,2,5))
# [[1, 2, 3, 4, 5, 6, 7], [10, 11], [12, 20], [21, 23]]

3. use numpy Solve multivariate first-order equations

import numpy as np
A = [[1,-2,1],[0,2,-8],[-4,5,9]] # coefficient matrix
B = [0,8,-9] # Constant matrix
x,y,z = np.linalg.inv(A).dot(B) # Find the solution of the system of equations
print(f'x={x}, y={y}, z={z}') # By default, it has only one set of solutions , No discussion 

4. The application dictionary returns the sum difference product quotient of two numbers at the same time

def exp(a,b):
choice = { 1: lambda x, y: x + y,
2: lambda x, y: x - y,
3: lambda x, y: x * y,
4: lambda x, y: x // y,
5: lambda x, y: x % y }
return tuple(choice[i+1](a,b) for i in range(5))
print(exp(8,3)) # (11, 5, 24, 2, 2)
print(exp(5,6)[2]) # 30

5. There are many ways to get elements that begin with a letter

>>> s = ['foot','head','Face','Hair','nose','Mouth','finger','ear']
>>> [w for w in s if w[0].lower()=='f']
['foot', 'Face', 'finger']
>>> [w for w in s if w[0] in ('f','F')]
['foot', 'Face', 'finger']
>>> [w for w in s if w.startswith(('F','f'))]
['foot', 'Face', 'finger']

6. Enter an integer , Wrong input will not cause abnormal exit

n = ''
while not n.isnumeric(): n = input(' Enter a non negative integer :')
n = int(n)
n = ''
while not (n.isnumeric() or len(n) and n[0]=='-' and n[1:].isnumeric()):
n = input(' Enter an integer :')
n = int(n)

 

【 Related reading 】

Python Tips accumulated in question and answer channel ( One )https://hannyang.blog.csdn.net/article/details/124935045

Python Tips accumulated in question and answer channel ( Two )https://hannyang.blog.csdn.net/article/details/125026881

Python Tips accumulated in question and answer channel ( 3、 ... and )
https://hannyang.blog.csdn.net/article/details/125058178

Python Tips accumulated in question and answer channel ( Four )
https://hannyang.blog.csdn.net/article/details/125211774

Python Tips accumulated in question and answer channel ( 5、 ... and )https://hannyang.blog.csdn.net/article/details/125270812

Python Tips accumulated in question and answer channel ( 6、 ... and )
https://hannyang.blog.csdn.net/article/details/125339202


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved