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

Python 在問答頻道中刷題積累到的小技巧(七)

編輯:Python

1. max(),min()函數的key參數應用

快速找出眾數:

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

找最短密碼(首個匹配到的):

>>> 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'

位數最多、最少的整數 :(首個匹配到的數,與最大值、最小值未必相等)

>>> 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. 列表任意指定長度的分組

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. 用numpy解多元一次方程組

import numpy as np
A = [[1,-2,1],[0,2,-8],[-4,5,9]] # 系數矩陣
B = [0,8,-9] # 常數矩陣
x,y,z = np.linalg.inv(A).dot(B) # 求方程組的解
print(f'x={x}, y={y}, z={z}') # 默認它有且只有一組解,不展開討論

4. 應用字典同時返回兩數的和差積商余

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. 多種方法取出某字母開頭的元素

>>> 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. 輸入一個整數,輸錯不會異常退出

n = ''
while not n.isnumeric(): n = input('輸入一個非負整數:')
n = int(n)
n = ''
while not (n.isnumeric() or len(n) and n[0]=='-' and n[1:].isnumeric()):
n = input('輸入一個整數:')
n = int(n)

 

【相關閱讀】

Python 在問答頻道中刷題積累到的小技巧(一)https://hannyang.blog.csdn.net/article/details/124935045

Python 在問答頻道中刷題積累到的小技巧(二)https://hannyang.blog.csdn.net/article/details/125026881

Python 在問答頻道中刷題積累到的小技巧(三)
https://hannyang.blog.csdn.net/article/details/125058178

Python 在問答頻道中刷題積累到的小技巧(四)
https://hannyang.blog.csdn.net/article/details/125211774

Python 在問答頻道中刷題積累到的小技巧(五)https://hannyang.blog.csdn.net/article/details/125270812

Python 在問答頻道中刷題積累到的小技巧(六)
https://hannyang.blog.csdn.net/article/details/125339202


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