导图社区 python进阶思维导图
这是一篇有关Python进阶的思维导图,从字符串、数据结构、深度学习、无论是在机器学习还是深度学习中等方面进行了概述。
编辑于2021-07-03 15:37:46python进阶
深度学习
离不开数学分析(高等数学)、线性代数、概率论等知识, 更离不开以编程为核心的动手实践。
无论是在机器学习还是深度学习中,
Python 已经成为主导性的编程语言。而且,现在许多主流的深度学习框架都提供Python接口,Python被用于数据预处理、定义网络模型、执行训练过程、数据可视化等
熟悉 Python 的基础语法,并掌握 NumPy,Pandas 及其他基础工具模块的使用对深度学习实践是非常重要的!
目录
Python数据结构
Python面向对象
Python JSON
Python 异常处理
常见Linux命令
Python数据结构
数字、字符串、列表、元祖、字典
数字
Python Number 数据类型用于存储数值。
Python Number 数据类型用于存储数值,包括整型、长整型、浮点型、复数。
(1)Python math 模块
:Python 中数学运算常用的函数基本都在 math 模块
import math print(math.ceil(4.1)) #返回数字的上入整数 print(math.floor(4.9)) #返回数字的下舍整数 print(math.fabs(-10)) #返回数字的绝对值 print(math.sqrt(9)) #返回数字的平方根 print(math.exp(1)) #返回e的x次幂 5 4 10.0 3.0 2.718281828459045
(2)Python随机数
首先import random,使用random()方法即可随机生成一个[0,1)范围内的实数
import random ran = random.random() print(ran) 0.9693881604049188
调用 random.random() 生成随机数时,每一次生成的数都是随机的。但是,当预先使用 random.seed(x) 设定好种子之后,其中的 x 可以是任意数字,此时使用 random() 生成的随机数将会是同一个。
print ("------- 设置种子 seed -------") random.seed(10) print ("Random number with seed 10 : ", random.random()) # 生成同一个随机数 random.seed(10) print ("Random number with seed 10 : ", random.random()) ------- 设置种子 seed ------- Random number with seed 10 : 0.5714025946899135 Random number with seed 10 : 0.5714025946899135
randint()生成一个随机整数
ran = random.randint(1,20) print(ran) 14
字符串
字符串连接:+
a = "Hello " b = "World " print(a + b) Hello World
重复输出字符串:*
print(a * 3) Hello Hello Hello
通过索引获取字符串中字符[]
print(a[0]) H
字符串截取[:] 牢记:左闭右开
print(a[1:4]) ell
判断字符串中是否包含给定的字符: in, not in
print('e' in a) print('e' not in a) True False
join():以字符作为分隔符,将字符串中所有的元素合并为一个新的字符串
new_str = '-'.join('Hello') print(new_str) H-e-l-l-o
字符串单引号、双引号、三引号
print('Hello World!') print("Hello World!") Hello World! Hello World!
转义字符 \
print("The \t is a tab") print('I\'m going to the movies') The is a tab I'm going to the movies
三引号让程序员从引号和特殊字符串的泥潭里面解脱出来,自始至终保持一小块字符串的格式是所谓的WYSIWYG(
所见即所得 )格式的。
print('''I'm going to the movies''') html = ''' <HTML><HEAD><TITLE> Friends CGI Demo</TITLE></HEAD> <BODY><H3>ERROR</H3> <B>%s</B><P> <FORM><INPUT TYPE=button VALUE=Back ONCLICK="window.history.back()"></FORM> </BODY></HTML> ''' print(html) I'm going to the movies <HTML><HEAD><TITLE> Friends CGI Demo</TITLE></HEAD> <BODY><H3>ERROR</H3> <B>%s</B><P> <FORM><INPUT TYPE=button VALUE=Back ONCLICK="window.history.back()"></FORM> </BODY></HTML>
列表
作用:类似其他语言中的数组
声明一个列表,并通过下标或索引获取元素
#声明一个列表 names = ['jack','tom','tonney','superman','jay'] #通过下标或索引获取元素 print(names[0]) print(names[1]) jack tom #获取最后一个元素 print(names[-1]) print(names[len(names)-1]) jay jay #获取第一个元素 print(names[-5]) jack #遍历列表,获取元素 for name in names: print(name) jack tom tonney superman jay #查询names里面有没有superman for name in names: if name == 'superman': print('有超人') break else: print('有超人') 有超人 #更简单的方法,来查询names里有没有superman if 'superman' in names: print('有超人') else: print('有超人')
列表元素添加
#声明一个空列表 girls = [] #append(),末尾追加 girls.append('杨超越') print(girls) ['杨超越'] #extend(),一次添加多个。把一个列表添加到另一个列表 ,列表合并。 models = ['刘雯','奚梦瑶'] girls.extend(models) #girls = girls + models print(girls) ['杨超越', '刘雯', '奚梦瑶'] #insert():指定位置添加 girls.insert(1,'虞书欣') print(girls) ['杨超越', '虞书欣', '刘雯', '奚梦瑶']
列表元素修改,通过下标找到元素,然后用=赋值
fruits = ['apple','pear','香蕉','pineapple','草莓'] print(fruits) fruits[-1] = 'strawberry' print(fruits) ['apple', 'pear', '香蕉', 'pineapple', '草莓'] ['apple', 'pear', '香蕉', 'pineapple', 'strawberry'] ''' 将fruits列表中的‘香蕉’替换为‘banana’ ''' for fruit in fruits: if '香蕉' in fruit: fruit = 'banana' print(fruits) for i in range(len(fruits)): if '香蕉' in fruits[i]: fruits[i] = 'banana' break print(fruits) ['apple', 'pear', '香蕉', 'pineapple', 'strawberry'] ['apple', 'pear', 'banana', 'pineapple', 'strawberry']
列表元素删除
words = ['cat','hello','pen','pencil','ruler'] del words[1] print(words) ['cat', 'pen', 'pencil', 'ruler'] words = ['cat','hello','pen','pencil','ruler'] words.remove('cat') print(words) ['hello', 'pen', 'pencil', 'ruler'] words = ['cat','hello','pen','pencil','ruler'] words.pop(1) print(words)
列表切片
在Python中处理列表的部分元素,称之为切片。
创建切片,可指定要使用的第一个元素和最后一个元素的索引。注意:左闭右开
将截取的结果再次存放在一个列表中,所以还是返回列表
animals = ['cat','dog','tiger','snake','mouse','bird'] print(animals[2:5]) print(animals[-1:]) print(animals[-3:-1]) print(animals[-5:-1:2]) print(animals[::2]) ['tiger', 'snake', 'mouse'] ['bird'] ['snake', 'mouse'] ['dog', 'snake'] ['cat', 'tiger', 'mouse']
列表排序
随机生成10个不同的整数,并进行排序
''' 生成10个不同的随机整数,并存至列表中 ''' import random random_list = [] for i in range(10): ran = random.randint(1,20) if ran not in random_list: random_list.append(ran) print(random_list) [16, 19, 1, 7, 15, 9, 6, 2, 17]
上述代码存在什么问题吗?
import random random_list = [] i = 0 while i < 10: ran = random.randint(1,20) if ran not in random_list: random_list.append(ran) i+=1 print(random_list) [16, 11, 3, 8, 12, 2, 14, 5, 20, 13] #默认升序 new_list = sorted(random_list) print(new_list) #降序 new_list = sorted(random_list,reverse =True) print(new_list) [2, 3, 5, 8, 11, 12, 13, 14, 16, 20] [20, 16, 14, 13, 12, 11, 8, 5, 3, 2]
元组
与列表类似,元祖中的内容不可修改