Python一行代碼能做什么,這30個(gè)案例告訴你

Python語(yǔ)法簡(jiǎn)潔,能夠用一行代碼實(shí)現(xiàn)很多有趣的功能,這次來(lái)整理30個(gè)常見(jiàn)的Python一行代碼集合。

1、轉(zhuǎn)置矩陣

old_list = [[1, 2, 3], [3, 4, 6], [5, 6, 7]]
list(list(x) for x in zip(*old_list))
[[1, 3, 5], [2, 4, 6], [3, 6, 7]]

2、二進(jìn)制轉(zhuǎn)十進(jìn)制

decimal = int('1010', 2)
print(decimal) #10
10

3、字符串大寫(xiě)轉(zhuǎn)小寫(xiě)

# 方法一 lower()
"Hi my name is Allwin".lower()
# 'hi my name is allwin'
# 方法二 casefold()
"Hi my name is Allwin".casefold()
# 'hi my name is allwin'
'hi my name is allwin'

4、字符串小寫(xiě)轉(zhuǎn)大寫(xiě)

"hi my name is Allwin".upper()
# 'HI MY NAME IS ALLWIN'
'HI MY NAME IS ALLWIN'

5、將字符串轉(zhuǎn)換為字節(jié)

"convert string to bytes using encode method".encode()
# b'convert string to bytes using encode method'
b'convert string to bytes using encode method'

6、復(fù)制文件內(nèi)容

import shutil; shutil.copyfile('source.txt', 'dest.txt')
'dest.txt'

7、快速排序

qsort = lambda l : l if len(l)<=1 else qsort([x for x in l[1:] if x < l[0]]) + [l[0]] + qsort([x for x in l[1:] if x >= l[0]])
qsort([1,3,2])
[1, 2, 3]

8、n個(gè)連續(xù)數(shù)之和

n = 3
sum(range(0, n+1))
6

9、交換兩個(gè)變量

a=1
b=2
a,b = b,a

10、斐波那契數(shù)列

fib = lambda x: x if x<=1 else fib(x-1) + fib(x-2)
fib(10)
55

11、將嵌套列表合并為一個(gè)列表

main_list = [[1,2],[3,4],[5,6,7]]
[item for sublist in main_list for item in sublist]
[1, 2, 3, 4, 5, 6, 7]

12、運(yùn)行 HTTP 服務(wù)器

python3 -m http.server 8000

13、反轉(zhuǎn)列表

numbers = 'I Love China'
numbers[::-1]
'anihC evoL I'

14、返回階乘

import math; fact_5 = math.factorial(5)
fact_5
120

15、判斷列表推導(dǎo)式

even_list = [number for number in [1, 2, 3, 4] if number % 2 == 0]
even_list
[2, 4]

16、取最長(zhǎng)字符串

words = ['This', 'is', 'a', 'list', 'of', 'words']
max(words, key=len) 
'words'

17、列表推導(dǎo)式

li = [num for num in range(0,100)]
# this will create a list of numbers from 0 to 99

18、集合推導(dǎo)式

num_set = { num for num in range(0,100)}
# this will create a set of numbers from 0 to 99

19、字典推導(dǎo)式

dict_numbers = {x:x*x for x in range(1,5) }
# {1: 1, 2: 4, 3: 9, 4: 16}

20、if-else

print("even") if 4%2==0 else print("odd")
even

21、無(wú)限循環(huán)

while 1:0

22、檢查數(shù)據(jù)類(lèi)型

isinstance(2, int)
isinstance("allwin", str)
isinstance([3,4,1997], list)

23、while循環(huán)

a=5
while a > 0: a = a - 1; print(a)

24、使用print語(yǔ)句寫(xiě)入到文件里

print("Hello, World!", file=open('source.txt', 'w'))

25、統(tǒng)計(jì)字頻

print("umbrella".count('l'))
2

26、合并兩個(gè)列表

list1.extend(list2)
# contents of list 2 will be added to the list1

27、合并兩個(gè)字典

dict1.update(dict2)
# contents of dictionary 2 will be added to the dictionary 1

28、合并兩個(gè)集合

set1.update(set2)
# contents of set2 will be copied to the set1

29、時(shí)間戳

import time; print(time.time())
1632146103.8406303

30、統(tǒng)計(jì)最多的元素

test_list = [9, 4, 5, 4, 4, 5, 9, 5, 4]
most_frequent_element = max(set(test_list), key=test_list.count)
most_frequent_element
4

最后,Python代碼哲學(xué)崇尚簡(jiǎn)潔,伙伴們也可以嘗試把代碼簡(jiǎn)化,看能不能實(shí)現(xiàn)想要的功能。

本文參考medium文章:

https://allwin-raju-12.medium.com/50-python-one-liners-everyone-should-know-182ea7c8de9d

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容