您现在的位置是:亿华云 > 应用开发
Python一行代码能做什么,30个实用案例代码详解
亿华云2025-10-09 03:25:02【应用开发】3人已围观
简介Python语法简洁,能够用一行代码实现很多有趣的功能,这次来整理30个常见的Python一行代码集合。1、转置矩阵old_list=[[1,2,3],[3,4,6],[5,6,7]]list(lis
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 allwinhi my name is 码能码详allwin
4、字符串小写转大写
"hi my name is Allwin".upper() # HI MY NAME IS ALLWINHI MY NAME IS ALLWIN
5、个实将字符串转换为字节
"convert string to bytes using encode method".encode() # bconvert string to bytes using encode methodbconvert 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=2a,个实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 800013、反转列表
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 9918、集合推导式
num_set = { num for num in range(0,100)} # this will create a set of numbers from 0 to 9919、字典推导式
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:022、检查数据类型
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 list127、合并两个字典
dict1.update(dict2) # contents of dictionary 2 will be added to the dictionary 128、合并两个集合
set1.update(set2) # contents of set2 will be copied to the set129、时间戳
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_element最后,Python代码哲学崇尚简洁,伙伴们也可以尝试把代码简化,看能不能实现想要的功能。
很赞哦!(31236)
站长推荐
.com域名是国际最广泛流行的通用域名,目前全球注册量第一的域名,公司企业注册域名的首选。国际化公司通常会注册该类域名。
详解服务器端和移动端性能测试指标
AMD EPYC服务器芯片将涨价10%~30%
戴尔科技VxRail助力企业的优势从数据中心扩展到整个业务
因为域名解析需要同步到DNS根服务器,而DNS根服务器会不定时刷,只有DNS根服务器刷新后域名才能正常访问,新增解析一般会在10分钟左右生效,最长不会超过24小时,修改解析时间会稍微延长。
如何监控SQL Server服务器CPU的各项指标
IPF2022浪潮信息生态伙伴大会召开 智算创见加速数实相融
NEC成功开发全球首个可扩展到全连接量子退火架构的基本单元