成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Python一行代碼能做什么,30個實用案例代碼詳解

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

 

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

1、轉置矩陣

  1. old_list = [[123], [346], [567]] 
  2. list(list(x) for x in zip(*old_list)) 

[[1, 3, 5], [2, 4, 6], [3, 6, 7]]

2、二進制轉十進制

  1. decimal = int('1010'2
  2.  
  3. print(decimal) #10 

10

3、字符串大寫轉小寫

  1. # 方法一 lower() 
  2.  
  3. "Hi my name is Allwin".lower() 
  4.  
  5. 'hi my name is allwin' 
  6.  
  7. # 方法二 casefold() 
  8.  
  9. "Hi my name is Allwin".casefold() 
  10.  
  11. 'hi my name is allwin' 

'hi my name is allwin'

4、字符串小寫轉大寫

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

'HI MY NAME IS ALLWIN'

5、將字符串轉換為字節

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

b'convert string to bytes using encode method'

6、復制文件內容

  1. import shutil; shutil.copyfile('source.txt''dest.txt'

'dest.txt'

7、快速排序

  1. 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]]) 
  2.  
  3. qsort([1,3,2]) 

[1, 2, 3]

8、n個連續數之和

  1. n = 3 
  2.  
  3. sum(range(0, n+1)) 

6

9、交換兩個變量

  1. a=1 
  2.  
  3. b=2 

a,b = b,a

10、斐波那契數列

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

55

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

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

[1, 2, 3, 4, 5, 6, 7]

12、運行 HTTP 服務器

  1. python3 -m http.server 8000 

13、反轉列表

  1. numbers = 'I Love China' 
  2.  
  3. numbers[::-1

'anihC evoL I'

14、返回階乘

  1. import math; fact_5 = math.factorial(5
  2.  
  3. fact_5 

120

15、判斷列表推導式

  1. even_list = [number for number in [1234if number % 2 == 0
  2.  
  3. even_list 

[2, 4]

16、取最長字符串

  1. words = ['This''is''a''list''of''words'
  2.  
  3. max(words, key=len)  

'words'

17、列表推導式

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

18、集合推導式

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

19、字典推導式

  1. dict_numbers = {x:x*x for x in range(1,5) } 
  2.  
  3. # {112439416

20、if-else

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

even

21、無限循環

  1. while 1:0 

22、檢查數據類型

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

23、while循環

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

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

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

25、統計字頻

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

2

26、合并兩個列表

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

27、合并兩個字典

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

28、合并兩個集合

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

29、時間戳

  1. import time; print(time.time()) 

1632146103.8406303

30、統計最多的元素

  1. test_list = [945445954
  2.  
  3. most_frequent_element = max(set(test_list), key=test_list.count) 
  4.  
  5. most_frequent_element 

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

 

責任編輯:張燕妮 來源: Python大數據分析
相關推薦

2021-05-28 07:39:17

SQL代碼操作

2024-08-08 09:15:08

SQL代碼復制表

2016-12-02 08:53:18

Python一行代碼

2021-11-05 06:57:50

架構工具代碼

2024-12-30 09:03:09

2022-04-09 09:11:33

Python

2024-05-31 13:14:05

2020-08-19 10:30:25

代碼Python多線程

2021-11-02 16:25:41

Python代碼技巧

2024-11-08 17:22:22

2017-04-13 19:20:18

Python代碼并行任務

2022-02-18 11:51:36

Python代碼編程語言

2020-09-28 12:34:38

Python代碼開發

2023-12-25 15:28:57

Python工具pywebio

2019-10-10 16:49:18

Python鏡音雙子腳本語言

2024-09-26 15:46:54

Python編程

2020-08-12 14:54:00

Python代碼開發

2014-02-12 13:43:50

代碼并行任務

2017-04-05 11:10:23

Javascript代碼前端

2023-09-12 10:10:57

開發者工具開源
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 久热精品在线 | 国产精品自拍视频网站 | 亚洲成人精品一区二区 | 亚洲欧美在线观看视频 | 毛片一区二区三区 | 国产精品久久久久久久久图文区 | www.色.com| 亚洲精品乱码8久久久久久日本 | 九九色综合 | 毛片1| 久久久久国产一区二区三区 | 午夜视频一区二区 | 91电影| 欧美成人在线网站 | 日韩精品一区二区三区高清免费 | 一区二区三区国产精品 | 男女免费视频网站 | 在线观看中文字幕 | 欧美国产日本一区 | 国产亚洲一区二区在线观看 | 一级看片免费视频囗交动图 | 午夜男人视频 | 午夜精品三区 | 成人做爰999| 国产精品成人一区二区三区 | 亚洲国产免费 | www天天操| 国内精品久久久久久久影视简单 | 国产精品亚洲精品久久 | 人碰人操 | 亚洲国产精品久久久 | 久久精品国产一区二区三区 | 精精精精xxxx免费视频 | 亚洲精品免费观看 | 亚洲精品久久国产高清情趣图文 | 亚洲国产aⅴ成人精品无吗 国产精品永久在线观看 | 成人精品国产一区二区4080 | 欧产日产国产精品国产 | 一区二区国产精品 | 久久久一二三 | 伊人二区 |