Python实用案例代码详解

简介: 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) #10
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'

'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')
//代码效果参考:http://www.ezhiqi.com/bx/art_6837.html
'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
//代码效果参考:http://www.ezhiqi.com/zx/art_1898.html

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_5
120
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)]
//代码效果参考:http://www.ezhiqi.com/zx/art_1588.html

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_element

相关文章
|
22天前
|
存储 算法 调度
【复现】【遗传算法】考虑储能和可再生能源消纳责任制的售电公司购售电策略(Python代码实现)
【复现】【遗传算法】考虑储能和可再生能源消纳责任制的售电公司购售电策略(Python代码实现)
128 26
|
11天前
|
测试技术 Python
Python装饰器:为你的代码施展“魔法”
Python装饰器:为你的代码施展“魔法”
196 100
|
11天前
|
开发者 Python
Python列表推导式:一行代码的艺术与力量
Python列表推导式:一行代码的艺术与力量
202 95
|
19天前
|
Python
Python的简洁之道:5个让代码更优雅的技巧
Python的简洁之道:5个让代码更优雅的技巧
174 104
|
19天前
|
开发者 Python
Python神技:用列表推导式让你的代码更优雅
Python神技:用列表推导式让你的代码更优雅
304 99
|
11天前
|
缓存 Python
Python装饰器:为你的代码施展“魔法
Python装饰器:为你的代码施展“魔法
129 88
|
16天前
|
数据采集 监控 数据库
Python异步编程实战:爬虫案例
🌟 蒋星熠Jaxonic,代码为舟的星际旅人。从回调地狱到async/await协程天堂,亲历Python异步编程演进。分享高性能爬虫、数据库异步操作、限流监控等实战经验,助你驾驭并发,在二进制星河中谱写极客诗篇。
Python异步编程实战:爬虫案例
|
16天前
|
监控 机器人 编译器
如何将python代码打包成exe文件---PyInstaller打包之神
PyInstaller可将Python程序打包为独立可执行文件,无需用户安装Python环境。它自动分析代码依赖,整合解释器、库及资源,支持一键生成exe,方便分发。使用pip安装后,通过简单命令即可完成打包,适合各类项目部署。
|
19天前
|
设计模式 人工智能 API
AI智能体开发实战:17种核心架构模式详解与Python代码实现
本文系统解析17种智能体架构设计模式,涵盖多智能体协作、思维树、反思优化与工具调用等核心范式,结合LangChain与LangGraph实现代码工作流,并通过真实案例验证效果,助力构建高效AI系统。
239 7
|
24天前
|
存储 大数据 Unix
Python生成器 vs 迭代器:从内存到代码的深度解析
在Python中,处理大数据或无限序列时,迭代器与生成器可避免内存溢出。迭代器通过`__iter__`和`__next__`手动实现,控制灵活;生成器用`yield`自动实现,代码简洁、内存高效。生成器适合大文件读取、惰性计算等场景,是性能优化的关键工具。
177 2

推荐镜像

更多