日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

【Python】Python一行代码能做什么,30个实用案例代码详解

發布時間:2025/3/12 python 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Python】Python一行代码能做什么,30个实用案例代码详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Python語法簡潔,能夠用一行代碼實現很多有趣的功能,這次來整理30個常見的Python一行代碼集合。

1、轉置矩陣

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、二進制轉十進制

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

3、字符串大寫轉小寫

#?方法一?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、字符串小寫轉大寫

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

5、將字符串轉換為字節

"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、復制文件內容

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個連續數之和

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

9、交換兩個變量

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

10、斐波那契數列

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

11、將嵌套列表合并為一個列表

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、運行 HTTP 服務器

python3?-m?http.server?8000

13、反轉列表

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

14、返回階乘

import?math;?fact_5?=?math.factorial(5) fact_5120

15、判斷列表推導式

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

16、取最長字符串

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

17、列表推導式

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

18、集合推導式

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

19、字典推導式

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、無限循環

while?1:0

22、檢查數據類型

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

23、while循環

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

24、使用print語句寫入到文件里

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

25、統計字頻

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

26、合并兩個列表

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

27、合并兩個字典

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

28、合并兩個集合

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

29、時間戳

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

30、統計最多的元素

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

最后,Python代碼哲學崇尚簡潔,伙伴們也可以嘗試把代碼簡化,看能不能實現想要的功能。

本文參考medium文章:

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

往期精彩回顧適合初學者入門人工智能的路線及資料下載機器學習及深度學習筆記等資料打印機器學習在線手冊深度學習筆記專輯《統計學習方法》的代碼復現專輯 AI基礎下載機器學習的數學基礎專輯黃海廣老師《機器學習課程》視頻課

本站qq群851320808,加入微信群請掃碼:

總結

以上是生活随笔為你收集整理的【Python】Python一行代码能做什么,30个实用案例代码详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。