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

相关文章
|
1天前
|
JavaScript 前端开发 Python
python执行js代码
本文档详细介绍如何安装Node.js环境及PyExecJS库,并提供示例代码展示其功能。首先,通过指定链接安装Node.js,安装完毕后可在命令行中输入`node --version`来验证安装是否成功。接着,使用`pip install PyExecJS`安装PyExecJS库,该库允许Python程序执行JavaScript代码。文档还提供了多个示例代码,展示了如何在Python环境中执行和编译JavaScript代码,并可以选择特定的JavaScript运行时环境,如Node.js或JScript。最后,通过具体案例展示了PyExecJS的功能与使用方法。
10 3
|
1天前
|
Java Linux Python
Linux环境下 代码java调用python出错
Linux环境下 代码java调用python出错
12 3
|
1天前
|
Python
5-19|记录Python调用salt代码
5-19|记录Python调用salt代码
|
2天前
|
存储 Python
深度剖析:Python里字典树Trie的构建与查询,让你的代码更优雅!
在编程的世界里,数据结构的选择往往直接决定了程序的效率和可读性。今天,我们将深入探索一种高效处理字符串搜索与匹配的数据结构——字典树(Trie),也称作前缀树或单词查找树。通过Python实现Trie树,我们将看到它如何优雅地解决一系列字符串相关的问题,并提升代码的整体质量。
9 2
|
3天前
|
Unix Linux 网络安全
python中连接linux好用的模块paramiko(附带案例)
该文章详细介绍了如何使用Python的Paramiko模块来连接Linux服务器,包括安装配置及通过密码或密钥进行身份验证的示例。
9 1
|
3天前
|
Shell Linux Python
python执行linux系统命令的几种方法(python3经典编程案例)
文章介绍了多种使用Python执行Linux系统命令的方法,包括使用os模块的不同函数以及subprocess模块来调用shell命令并处理其输出。
10 0
|
3天前
|
调度 数据库 Python
python中APScheduler的使用详解(python3经典编程案例)
文章详细讲解了在Python中使用APScheduler来安排和执行定时任务的方法,包括不同调度器的配置与使用场景。
14 0
|
3天前
|
数据可视化 搜索推荐 JavaScript
pyecharts模块的几个经典案例(python经典编程案例)
文章提供了多个使用pyecharts模块创建数据可视化的Python编程案例,展示如何生成各种类型的图表并进行定制化设置。
9 0
|
3天前
|
机器学习/深度学习 开发工具 git
matplotlib各种案例总结(python经典编程案例)
该文章汇总了使用matplotlib绘制不同类型图表的方法和案例,包括条形图、折线图等,并展示了如何调整颜色和线条样式等属性。
11 0
|
3天前
|
数据挖掘 Python
用python的tushare模块分析股票案例(python3经典编程案例)
该文章提供了使用Python的tushare模块分析股票数据的案例,展示了如何获取股票数据以及进行基本的数据分析。
11 0