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

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

 

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

1、转置矩阵

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

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

2、二进制转十进制

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

10

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' 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

'hi my name is allwin'

4、字符串小写转大写

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

'HI MY NAME IS ALLWIN'

5、将字符串转换为字节

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

b'convert string to bytes using encode method'

6、复制文件内容

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

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

[1, 2, 3]

8、n个连续数之和

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

6

9、交换两个变量

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

a,b = b,a

10、斐波那契数列

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

55

11、将嵌套列表合并为一个列表

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

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

12、运行 HTTP 服务器

python3 -m http.server 8000 
  • 1.

13、反转列表

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

'anihC evoL I'

14、返回阶乘

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

120

15、判断列表推导式

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

[2, 4]

16、取最长字符串

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

'words'

17、列表推导式

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

18、集合推导式

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

19、字典推导式

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

20、if-else

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

even

21、无限循环

while 1:0 
  • 1.

22、检查数据类型

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

23、while循环

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

24、使用print语句写入到文件里

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

25、统计字频

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

2

26、合并两个列表

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

27、合并两个字典

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

28、合并两个集合

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

29、时间戳

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

1632146103.8406303

30、统计最多的元素

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

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

 

责任编辑:张燕妮 来源: Python大数据分析
相关推荐

2021-05-28 07:39:17

SQL代码操作

2024-08-08 09:15:08

SQL代码复制表

2016-12-02 08:53:18

Python一行代码

2024-12-30 09:03:09

2021-11-05 06:57:50

架构工具代码

2022-04-09 09:11:33

Python

2024-05-31 13:14:05

2021-11-02 16:25:41

Python代码技巧

2020-08-19 10:30:25

代码Python多线程

2017-04-13 19:20:18

Python代码并行任务

2024-11-08 17:22:22

2022-02-18 11:51:36

Python代码编程语言

2020-09-28 12:34:38

Python代码开发

2024-09-26 15:46:54

Python编程

2020-08-12 14:54:00

Python代码开发

2023-12-25 15:28:57

Python工具pywebio

2014-02-12 13:43:50

代码并行任务

2017-04-05 11:10:23

Javascript代码前端

2019-10-10 16:49:18

Python镜音双子脚本语言

2023-09-12 10:10:57

开发者工具开源
点赞
收藏

51CTO技术栈公众号