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


 

Python语法简洁,能够用一行代码实现很多有趣的功能,这次来整理30个常见的Python一行代码集合。

1、转置矩阵

  1. old_list = [[1, 2, 3], [3, 4, 6], [5, 6, 7]] 
  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 [1, 2, 3, 4] if 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. # {1: 1, 2: 4, 3: 9, 4: 16} 

20、if-else

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

even

21、无限循环

  1. while 1:0 

22、检查数据类型

  1. isinstance(2, int) 
  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 = [9, 4, 5, 4, 4, 5, 9, 5, 4] 
  2.  
  3. most_frequent_element = max(set(test_list), key=test_list.count) 
  4.  
  5. most_frequent_element 

最后,Python代码哲学崇尚简洁,伙伴们也可以尝试把代码简化,看能不能实现想要的功能。

评论关闭