python基础全部知识点整理,超级全(20万字+)

2023-05-31,,

目录

        Python编程语言简介

https://www.cnblogs.com/hany-postq473111315/p/12256134.html

        Python环境搭建及中文编码

https://www.cnblogs.com/hany-postq473111315/p/12256337.html

        Python 基础语法

https://www.cnblogs.com/hany-postq473111315/p/12257287.html

        Python 变量类型及变量赋值

https://www.cnblogs.com/hany-postq473111315/p/12258952.html

        Python 标准数据类型

https://www.cnblogs.com/hany-postq473111315/p/12259374.html

        Python 数字数据类型

https://www.cnblogs.com/hany-postq473111315/p/12259684.html

        Python字符串

https://www.cnblogs.com/hany-postq473111315/p/12259859.html

        Python列表

https://www.cnblogs.com/hany-postq473111315/p/12259925.html

        Python元组

https://www.cnblogs.com/hany-postq473111315/p/12260559.html

        Python字典

https://www.cnblogs.com/hany-postq473111315/p/12260671.html

        Python数据类型转换

https://www.cnblogs.com/hany-postq473111315/p/12260913.html

        Python算术运算符

https://www.cnblogs.com/hany-postq473111315/p/12261418.html

        Python比较运算符

https://www.cnblogs.com/hany-postq473111315/p/12262652.html

        Python赋值运算符

https://www.cnblogs.com/hany-postq473111315/p/12262791.html

        Python位运算符

https://www.cnblogs.com/hany-postq473111315/p/12262990.html

        Python逻辑运算符

https://www.cnblogs.com/hany-postq473111315/p/12263166.html

        Python成员运算符

https://www.cnblogs.com/hany-postq473111315/p/12263573.html

        Python身份运算符

https://www.cnblogs.com/hany-postq473111315/p/12263746.html

        Python运算符优先级

https://www.cnblogs.com/hany-postq473111315/p/12263888.html

        Python条件语句

https://www.cnblogs.com/hany-postq473111315/p/12264149.html

        Python简单的语句组

https://www.cnblogs.com/hany-postq473111315/p/12264240.html

        Python循环语句

https://www.cnblogs.com/hany-postq473111315/p/12264329.html

        Python循环控制语句

https://www.cnblogs.com/hany-postq473111315/p/12264653.html

        Python while循环语句

https://www.cnblogs.com/hany-postq473111315/p/12264768.html

        Python无限循环

https://www.cnblogs.com/hany-postq473111315/p/12267695.html

        Python while 循环中使用 else 语句

https://www.cnblogs.com/hany-postq473111315/p/12267719.html

        Python while 中简单的语句组

https://www.cnblogs.com/hany-postq473111315/p/12267753.html

        Python for循环语句

https://www.cnblogs.com/hany-postq473111315/p/12268067.html

        Python for循环使用 else 语句

https://www.cnblogs.com/hany-postq473111315/p/12268076.html

        Python for循环通过序列索引迭代

https://www.cnblogs.com/hany-postq473111315/p/12268174.html

        Python 循环嵌套

https://www.cnblogs.com/hany-postq473111315/p/12268209.html

        Python break语句

https://www.cnblogs.com/hany-postq473111315/p/12268271.html

        Python continue语句

https://www.cnblogs.com/hany-postq473111315/p/12268306.html

        Python pass语句

https://www.cnblogs.com/hany-postq473111315/p/12268565.html

        Python 数字类型转换

https://www.cnblogs.com/hany-postq473111315/p/12268709.html

        Python数学函数

https://www.cnblogs.com/hany-postq473111315/p/12268737.html

        Python随机数函数

https://www.cnblogs.com/hany-postq473111315/p/12269533.html

        Python三角函数

https://www.cnblogs.com/hany-postq473111315/p/12269772.html

        Python数学常量

https://www.cnblogs.com/hany-postq473111315/p/12269905.html

        Python创建字符串

https://www.cnblogs.com/hany-postq473111315/p/12275504.html

        Python访问字符串中的值

https://www.cnblogs.com/hany-postq473111315/p/12275513.html

        Python字符串更新

https://www.cnblogs.com/hany-postq473111315/p/12275591.html

        Python转义字符

https://www.cnblogs.com/hany-postq473111315/p/12275690.html

        Python字符串运算符

https://www.cnblogs.com/hany-postq473111315/p/12275891.html

        Python字符串格式化

https://www.cnblogs.com/hany-postq473111315/p/12276031.html

        Python三引号

https://www.cnblogs.com/hany-postq473111315/p/12283515.html

        Python Unicode字符串

https://www.cnblogs.com/hany-postq473111315/p/12283589.html

        Python字符串内建函数_上

https://www.cnblogs.com/hany-postq473111315/p/12284044.html

        Python字符串内建函数_下

https://www.cnblogs.com/hany-postq473111315/p/12284555.html

        Python列表

https://www.cnblogs.com/hany-postq473111315/p/12286383.html

        Python访问列表中的值

https://www.cnblogs.com/hany-postq473111315/p/12286568.html

        Python更新列表

https://www.cnblogs.com/hany-postq473111315/p/12286660.html

        Python删除列表元素

https://www.cnblogs.com/hany-postq473111315/p/12286710.html

        Python列表脚本操作符

https://www.cnblogs.com/hany-postq473111315/p/12286755.html

        Python列表截取

https://www.cnblogs.com/hany-postq473111315/p/12286809.html

        Python列表函数和方法

https://www.cnblogs.com/hany-postq473111315/p/12286964.html

        Python元组

https://www.cnblogs.com/hany-postq473111315/p/12287445.html

        Python访问元组

https://www.cnblogs.com/hany-postq473111315/p/12287609.html

        Python修改元组

https://www.cnblogs.com/hany-postq473111315/p/12287673.html

        Python删除元组

https://www.cnblogs.com/hany-postq473111315/p/12287811.html

        Python元组运算符

https://www.cnblogs.com/hany-postq473111315/p/12287938.html

        Python元组索引、截取

https://www.cnblogs.com/hany-postq473111315/p/12287975.html

        Python元组内置函数

https://www.cnblogs.com/hany-postq473111315/p/12288016.html

        Python字典

https://www.cnblogs.com/hany-postq473111315/p/12288198.html

        Python访问、修改、删除字典中的值

https://www.cnblogs.com/hany-postq473111315/p/12288231.html

        Python字典内置函数和方法

https://www.cnblogs.com/hany-postq473111315/p/12288354.html

        Python日期和时间_什么是Tick_什么是时间元组_获取当前时间

https://www.cnblogs.com/hany-postq473111315/p/12289960.html

        Python获取当前时间_获取格式化时间_格式化日期

https://www.cnblogs.com/hany-postq473111315/p/12290540.html

        Python Time模块

https://www.cnblogs.com/hany-postq473111315/p/12290622.html

        Python日历模块

https://www.cnblogs.com/hany-postq473111315/p/12290929.html

        Python定义一个函数

https://www.cnblogs.com/hany-postq473111315/p/12293310.html

        Python函数调用

https://www.cnblogs.com/hany-postq473111315/p/12294753.html

        Python按值传递参数和按引用传递参数

https://www.cnblogs.com/hany-postq473111315/p/12294915.html

        Python函数参数

https://www.cnblogs.com/hany-postq473111315/p/12295062.html

        Python匿名函数_return语句

https://www.cnblogs.com/hany-postq473111315/p/12295925.html

        Python变量作用域

https://www.cnblogs.com/hany-postq473111315/p/12296130.html

        Python全局变量和局部变量

https://www.cnblogs.com/hany-postq473111315/p/12298147.html

        Python模块_import语句_from...import 函数名_from ... import *

https://www.cnblogs.com/hany-postq473111315/p/12298371.html

        Python定位模块_PYTHONPATH变量

https://www.cnblogs.com/hany-postq473111315/p/12299402.html

        Python命名空间和作用域

https://www.cnblogs.com/hany-postq473111315/p/12299536.html

        Python dir( )函数

https://www.cnblogs.com/hany-postq473111315/p/12299640.html

        Python globals和locals函数_reload函数

https://www.cnblogs.com/hany-postq473111315/p/12299945.html

        Python包

https://www.cnblogs.com/hany-postq473111315/p/12302885.html

        Python打印到屏幕_读取键盘输入

https://www.cnblogs.com/hany-postq473111315/p/12303156.html

        Python打开和关闭文件

https://www.cnblogs.com/hany-postq473111315/p/12303245.html

        Python read和write方法

https://www.cnblogs.com/hany-postq473111315/p/12303304.html

        PythonFile对象的属性

https://www.cnblogs.com/hany-postq473111315/p/12303309.html

        Python里的目录方法

https://www.cnblogs.com/hany-postq473111315/p/12303318.html

        Python重命名和删除文件

https://www.cnblogs.com/hany-postq473111315/p/12303314.html

        猜数字游戏

https://www.cnblogs.com/hany-postq473111315/p/12651677.html

        实现功能菜单栏

https://www.cnblogs.com/hany-postq473111315/p/12652887.html

        pass 出错问题

https://www.cnblogs.com/hany-postq473111315/p/12652896.html

        Python 实现分层聚类算法

https://www.cnblogs.com/hany-postq473111315/p/12671890.html

        KNN算法基本原理与sklearn实现

https://www.cnblogs.com/hany-postq473111315/p/12672247.html

        矩阵常用操作

https://www.cnblogs.com/hany-postq473111315/p/12672549.html

        不同复制操作对比(三种)

https://www.cnblogs.com/hany-postq473111315/p/12672623.html

        排序和索引的问题

https://www.cnblogs.com/hany-postq473111315/p/12672695.html

        正则表达式补充

https://www.cnblogs.com/hany-postq473111315/p/12675139.html

        Pandas 复习

https://www.cnblogs.com/hany-postq473111315/p/12675657.html

        Pandas 复习2

https://www.cnblogs.com/hany-postq473111315/p/12677896.html

        Series结构(常用)

https://www.cnblogs.com/hany-postq473111315/p/12678080.html

        部分画图

https://www.cnblogs.com/hany-postq473111315/p/12679161.html

        提取txt文本有效内容

https://www.cnblogs.com/hany-postq473111315/p/12680021.html

        获取全部 txt 文本中出现次数最多的前N个词汇

https://www.cnblogs.com/hany-postq473111315/p/12680560.html

        使用朴素贝叶斯模型对邮件进行分类

https://www.cnblogs.com/hany-postq473111315/p/12680882.html

        函数进阶1

https://www.cnblogs.com/hany-postq473111315/p/12681153.html

        函数进阶2

https://www.cnblogs.com/hany-postq473111315/p/12681487.html

        Python异常及异常处理

https://www.cnblogs.com/hany-postq473111315/p/12303328.html

        Python 中 False 和 True 关键字

https://www.cnblogs.com/hany-postq473111315/p/12257604.html

        正则表达式补充2

https://www.cnblogs.com/hany-postq473111315/p/12681687.html

        正则表达式基础1

https://www.cnblogs.com/hany-postq473111315/p/12682011.html

        两数相加(B站看视频总结)

https://www.cnblogs.com/hany-postq473111315/p/12682862.html

        函数进阶3

https://www.cnblogs.com/hany-postq473111315/p/12683682.html

        函数进阶4 

https://www.cnblogs.com/hany-postq473111315/p/12683729.html

        列表推导式

https://www.cnblogs.com/hany-postq473111315/p/12683951.html

        关于类和异常的笔记

https://www.cnblogs.com/hany-postq473111315/p/12683969.html

        类 巩固小结

https://www.cnblogs.com/hany-postq473111315/p/12684207.html

        模块小结

https://www.cnblogs.com/hany-postq473111315/p/12684404.html

        异常 巩固1

https://www.cnblogs.com/hany-postq473111315/p/12684508.html

        异常 巩固2

https://www.cnblogs.com/hany-postq473111315/p/12684638.html

        logging日志基础示例

https://www.cnblogs.com/hany-postq473111315/p/12684676.html

        异常 巩固3

https://www.cnblogs.com/hany-postq473111315/p/12684747.html

        多线程复习1

https://www.cnblogs.com/hany-postq473111315/p/12685980.html

        多线程复习2

https://www.cnblogs.com/hany-postq473111315/p/12686171.html

        yield 复习

https://www.cnblogs.com/hany-postq473111315/p/12686330.html

        协程解决素数

https://www.cnblogs.com/hany-postq473111315/p/12686467.html

        爬虫流程复习

https://www.cnblogs.com/hany-postq473111315/p/12686857.html

        列表常用方法复习

https://www.cnblogs.com/hany-postq473111315/p/12691570.html

        字典常用操作复习

https://www.cnblogs.com/hany-postq473111315/p/12691575.html

        单链表复习

https://www.cnblogs.com/hany-postq473111315/p/12695182.html

        双链表复习

https://www.cnblogs.com/hany-postq473111315/p/12697287.html

        单向循环链表

https://www.cnblogs.com/hany-postq473111315/p/12700855.html

        将"089,0760,009"变为 89,760,9

https://www.cnblogs.com/hany-postq473111315/p/12701254.html

        Linux最常用的基本操作复习

https://www.cnblogs.com/hany-postq473111315/p/12702144.html

        python连接数据库

https://www.cnblogs.com/hany-postq473111315/p/12704539.html

        复习实现栈的基本操作

https://www.cnblogs.com/hany-postq473111315/p/12705117.html

        显示列表重复值

https://www.cnblogs.com/hany-postq473111315/p/12705591.html

        复习 队列的实现

https://www.cnblogs.com/hany-postq473111315/p/12706952.html

        扩展 双端队列

https://www.cnblogs.com/hany-postq473111315/p/12707455.html

        复习 二叉树的创建

https://www.cnblogs.com/hany-postq473111315/p/12723432.html

        复习 广度遍历

https://www.cnblogs.com/hany-postq473111315/p/12725087.html

        复习 深度遍历(先序中序后序)

https://www.cnblogs.com/hany-postq473111315/p/12725386.html

        应用场景

https://www.cnblogs.com/hany-postq473111315/p/12725748.html

        类 扩展

https://www.cnblogs.com/hany-postq473111315/p/12727814.html

        复习 装饰器

https://www.cnblogs.com/hany-postq473111315/p/12731157.html

        扩展 求奇数和

https://www.cnblogs.com/hany-postq473111315/p/12732794.html

        字符串重复出现 

https://www.cnblogs.com/hany-postq473111315/p/12732912.html

        最基本的Tkinter界面操作

https://www.cnblogs.com/hany-postq473111315/p/12736499.html

        Tkinter常用简单操作

https://www.cnblogs.com/hany-postq473111315/p/12736695.html

        Tkinter经典写法

https://www.cnblogs.com/hany-postq473111315/p/12736898.html

        Label 组件基本写法

https://www.cnblogs.com/hany-postq473111315/p/12739187.html

        类实例化的对象调用的方法或属性来自于类的哪个方法中

https://www.cnblogs.com/hany-postq473111315/p/12739493.html

        Button基本用语

https://www.cnblogs.com/hany-postq473111315/p/12739711.html

        Entry基本用法

https://www.cnblogs.com/hany-postq473111315/p/12739996.html

        Text多行文本框基本用法

https://www.cnblogs.com/hany-postq473111315/p/12741482.html

        Radiobutton基础语法

https://www.cnblogs.com/hany-postq473111315/p/12742418.html

        Checkbutton基本写法

https://www.cnblogs.com/hany-postq473111315/p/12742886.html

        扩展写法

https://www.cnblogs.com/hany-postq473111315/p/12748369.html

        整理上课内容

https://www.cnblogs.com/hany-postq473111315/p/12758012.html

        Seaborn基础1

https://www.cnblogs.com/hany-postq473111315/p/12765835.html

        Seaborn基础2

https://www.cnblogs.com/hany-postq473111315/p/12765847.html

        Seaborn基础3

https://www.cnblogs.com/hany-postq473111315/p/12765857.html

        Seaborn实现单变量分析

https://www.cnblogs.com/hany-postq473111315/p/12766032.html

        Seaborn实现回归分析

https://www.cnblogs.com/hany-postq473111315/p/12766396.html

        Seaborn实现多变量分析

https://www.cnblogs.com/hany-postq473111315/p/12766576.html

        将形如 5D, 30s 的字符串转为秒

https://www.cnblogs.com/hany-postq473111315/p/12770867.html

        获得昨天和明天的日期

https://www.cnblogs.com/hany-postq473111315/p/12770871.html

        计算两个日期相隔的秒数

https://www.cnblogs.com/hany-postq473111315/p/12770888.html

        遍历多个 txt 文件进行获取值

https://www.cnblogs.com/hany-postq473111315/p/12771248.html

        Django学习路

https://www.cnblogs.com/hany-postq473111315/p/12774607.html

        Django学习路2

https://www.cnblogs.com/hany-postq473111315/p/12775279.html

        Django学习路3

https://www.cnblogs.com/hany-postq473111315/p/12778795.html

        安装第三方库

https://www.cnblogs.com/hany-postq473111315/p/12785024.html

        安装第三方库进阶

https://www.cnblogs.com/hany-postq473111315/p/12785212.html

        Python第一次实验

https://www.cnblogs.com/hany-postq473111315/p/12787851.html

        打包程序问题

https://www.cnblogs.com/hany-postq473111315/p/12788073.html

        pip 国内源

https://www.cnblogs.com/hany-postq473111315/p/12802294.html

        format 进阶

https://www.cnblogs.com/hany-postq473111315/p/12806654.html

        进阶删除重复元素

https://www.cnblogs.com/hany-postq473111315/p/12819466.html

        实现优先级队列

https://www.cnblogs.com/hany-postq473111315/p/12819475.html

        爬虫流程复习2

https://www.cnblogs.com/hany-postq473111315/p/12819589.html

        numpy第三方库

https://www.cnblogs.com/hany-postq473111315/p/12821506.html

        pandas第三方库

https://www.cnblogs.com/hany-postq473111315/p/12821512.html

        函数式回文串

https://www.cnblogs.com/hany-postq473111315/p/12821617.html

        判断是否包含重复值

https://www.cnblogs.com/hany-postq473111315/p/12821629.html

        函数实现 多个数据求平均值

https://www.cnblogs.com/hany-postq473111315/p/12821637.html

        对传入的数据进行分类

https://www.cnblogs.com/hany-postq473111315/p/12821665.html

        进阶 对传入的数据进行分类

https://www.cnblogs.com/hany-postq473111315/p/12821676.html

        二进制字符长度

https://www.cnblogs.com/hany-postq473111315/p/12821692.html

        将包含_或-的字符串最开始的字母小写,其余的第一个字母大写

https://www.cnblogs.com/hany-postq473111315/p/12821750.html

        将字符串的首字母大写其余字符根据需要,判断是否大写

https://www.cnblogs.com/hany-postq473111315/p/12821780.html

        将每一个分隔开的字符的首字母大写

https://www.cnblogs.com/hany-postq473111315/p/12821790.html

        无论传入什么数据都转换为列表

https://www.cnblogs.com/hany-postq473111315/p/12821818.html

        斐波那契数列进一步讨论性能

https://www.cnblogs.com/hany-postq473111315/p/12827717.html

        迭代器和可迭代对象区别

https://www.cnblogs.com/hany-postq473111315/p/12827742.html

        map 函数基本写法

https://www.cnblogs.com/hany-postq473111315/p/12827787.html

        filter 函数基本写法

https://www.cnblogs.com/hany-postq473111315/p/12827853.html

        functools 中的 reduce 函数基本写法

https://www.cnblogs.com/hany-postq473111315/p/12827943.html

        三元运算符

https://www.cnblogs.com/hany-postq473111315/p/12827981.html

        学装饰器之前必须要了解的四点

https://www.cnblogs.com/hany-postq473111315/p/12828151.html

        列表推导式,最基本写法

https://www.cnblogs.com/hany-postq473111315/p/12828176.html

        字典推导式,最基本写法

https://www.cnblogs.com/hany-postq473111315/p/12828267.html

        集合推导式,最基本写法

https://www.cnblogs.com/hany-postq473111315/p/12828283.html

        状态码

https://www.cnblogs.com/hany-postq473111315/p/12835104.html

        爬虫流程复习3

https://www.cnblogs.com/hany-postq473111315/p/12836761.html

        Django坑_01

https://www.cnblogs.com/hany-postq473111315/p/12837316.html

        list 和 [ ] 的功能不相同

https://www.cnblogs.com/hany-postq473111315/p/12838134.html

        Django创建简单数据库

https://www.cnblogs.com/hany-postq473111315/p/12840986.html

        数据库设计基础知识

https://www.cnblogs.com/hany-postq473111315/p/12841422.html

        Django学习路4_数据库添加元素,读取及显示到网页上

https://www.cnblogs.com/hany-postq473111315/p/12841899.html

        []  和 () 的区别

https://www.cnblogs.com/hany-postq473111315/p/12842366.html

        使用 you-get 下载免费电影或电视剧

https://www.cnblogs.com/hany-postq473111315/p/12843854.html

        Django学习路5_更新和删除数据库表中元素

https://www.cnblogs.com/hany-postq473111315/p/12844479.html

        Django学习路6_修改数据库为 mysql ,创建mysql及进行迁徙

https://www.cnblogs.com/hany-postq473111315/p/12844651.html

        python 连接 mysql 的三种驱动

https://www.cnblogs.com/hany-postq473111315/p/12844667.html

        pandas_DateFrame的创建

https://www.cnblogs.com/hany-postq473111315/p/12844777.html

        pandas_一维数组与常用操作

https://www.cnblogs.com/hany-postq473111315/p/12844790.html

        pandas_使用属性接口实现高级功能

https://www.cnblogs.com/hany-postq473111315/p/12844800.html

        pandas_使用透视表与交叉表查看业绩汇总数据

https://www.cnblogs.com/hany-postq473111315/p/12844818.html

        pandas_分类与聚合

https://www.cnblogs.com/hany-postq473111315/p/12844831.html

        pandas_处理异常值缺失值重复值数据差分

https://www.cnblogs.com/hany-postq473111315/p/12844851.html

        pandas_数据拆分与合并

https://www.cnblogs.com/hany-postq473111315/p/12844857.html

        pandas_数据排序

https://www.cnblogs.com/hany-postq473111315/p/12844866.html

        pandas_时间序列和常用操作

https://www.cnblogs.com/hany-postq473111315/p/12844876.html

        pandas_学习的时候总会忘了的知识点

https://www.cnblogs.com/hany-postq473111315/p/12844887.html

        pandas_查看数据特征和统计信息

https://www.cnblogs.com/hany-postq473111315/p/12844895.html

        pandas_读取Excel并筛选特定数据

https://www.cnblogs.com/hany-postq473111315/p/12844906.html

        pandas_重采样多索引标准差协方差

https://www.cnblogs.com/hany-postq473111315/p/12844921.html

        Numpy random函数

https://www.cnblogs.com/hany-postq473111315/p/12844924.html

        Numpy修改数组中的元素值

https://www.cnblogs.com/hany-postq473111315/p/12844928.html

        Numpy创建数组

https://www.cnblogs.com/hany-postq473111315/p/12844937.html

        Numpy改变数组的形状

https://www.cnblogs.com/hany-postq473111315/p/12844944.html

        Numpy数组排序

https://www.cnblogs.com/hany-postq473111315/p/12844954.html

        Numpy数组的函数

https://www.cnblogs.com/hany-postq473111315/p/12844962.html

        Numpy数组的运算

https://www.cnblogs.com/hany-postq473111315/p/12844978.html

        Numpy访问数组元素

https://www.cnblogs.com/hany-postq473111315/p/12845013.html

        关于这几天发布的文章

https://www.cnblogs.com/hany-postq473111315/p/12845031.html

        TCP 客户端

https://www.cnblogs.com/hany-postq473111315/p/12845038.html

        TCP 服务器端

https://www.cnblogs.com/hany-postq473111315/p/12845044.html

        UDP 绑定信息

https://www.cnblogs.com/hany-postq473111315/p/12845055.html

        UDP 网络程序-发送_接收数据

https://www.cnblogs.com/hany-postq473111315/p/12845060.html

        WSGI应用程序示例

https://www.cnblogs.com/hany-postq473111315/p/12845068.html

        定义 WSGI 接口

https://www.cnblogs.com/hany-postq473111315/p/12845080.html

        encode 和 decode 的使用

https://www.cnblogs.com/hany-postq473111315/p/12845094.html

        abs,all,any函数的使用

https://www.cnblogs.com/hany-postq473111315/p/12845118.html

        一行代码合并两个字典

https://www.cnblogs.com/hany-postq473111315/p/12845131.html

        一行代码求多个列表中的最大值

https://www.cnblogs.com/hany-postq473111315/p/12845153.html

        文件的某些操作(以前发过类似的)

https://www.cnblogs.com/hany-postq473111315/p/12845193.html

        冒泡排序

https://www.cnblogs.com/hany-postq473111315/p/12845219.html

        希尔排序

https://www.cnblogs.com/hany-postq473111315/p/12845247.html

        归并排序

https://www.cnblogs.com/hany-postq473111315/p/12845260.html

        快速排序

https://www.cnblogs.com/hany-postq473111315/p/12845271.html

        插入排序

https://www.cnblogs.com/hany-postq473111315/p/12845283.html

        选择排序

https://www.cnblogs.com/hany-postq473111315/p/12845306.html

        数据库基础应用

https://www.cnblogs.com/hany-postq473111315/p/12845310.html

        数据库进行参数化,查询一行或多行语句

https://www.cnblogs.com/hany-postq473111315/p/12845325.html

        数据结构_链表(单链表,单向循环链表,双链表)

https://www.cnblogs.com/hany-postq473111315/p/12845339.html

        数据结构_队列(普通队列和双端队列)

https://www.cnblogs.com/hany-postq473111315/p/12845346.html

        数据结构_栈

https://www.cnblogs.com/hany-postq473111315/p/12845356.html

        数据结构_二叉树

https://www.cnblogs.com/hany-postq473111315/p/12845365.html

        二分法查找

https://www.cnblogs.com/hany-postq473111315/p/12845370.html

        正则表达式_合集上

https://www.cnblogs.com/hany-postq473111315/p/12845456.html

        正则表达式_合集下(后续还会有补充)

https://www.cnblogs.com/hany-postq473111315/p/12845485.html

        线程_apply堵塞式

https://www.cnblogs.com/hany-postq473111315/p/12845503.html

        线程_FIFO队列实现生产者消费者

https://www.cnblogs.com/hany-postq473111315/p/12845511.html

        线程_GIL最简单的例子

https://www.cnblogs.com/hany-postq473111315/p/12845522.html

        线程_multiprocessing实现文件夹copy器

https://www.cnblogs.com/hany-postq473111315/p/12845531.html

        线程_multiprocessing异步

https://www.cnblogs.com/hany-postq473111315/p/12845536.html

        线程_Process实例

https://www.cnblogs.com/hany-postq473111315/p/12845545.html

        线程_Process基础语法

https://www.cnblogs.com/hany-postq473111315/p/12845549.html

        线程_ThreadLocal

https://www.cnblogs.com/hany-postq473111315/p/12845560.html

        线程_互斥锁_Lock及fork创建子进程

https://www.cnblogs.com/hany-postq473111315/p/12845573.html

        线程_gevent实现多个视频下载及并发下载

https://www.cnblogs.com/hany-postq473111315/p/12845587.html

        线程_gevent自动切换CPU协程

https://www.cnblogs.com/hany-postq473111315/p/12845603.html

        线程_使用multiprocessing启动一个子进程及创建Process 的子类

https://www.cnblogs.com/hany-postq473111315/p/12845615.html

        线程_共享全局变量(全局变量在主线程和子线程中不同)

https://www.cnblogs.com/hany-postq473111315/p/12845624.html

        线程_多线程_列表当做实参传递到线程中

https://www.cnblogs.com/hany-postq473111315/p/12845630.html

        线程_threading合集

https://www.cnblogs.com/hany-postq473111315/p/12845649.html

        线程_进程间通信Queue合集

https://www.cnblogs.com/hany-postq473111315/p/12845662.html

        线程_进程池

https://www.cnblogs.com/hany-postq473111315/p/12845673.html

        线程_可能发生的问题

https://www.cnblogs.com/hany-postq473111315/p/12845683.html

        == 和 is 的区别

https://www.cnblogs.com/hany-postq473111315/p/12845720.html

        __getattribute__小例子

https://www.cnblogs.com/hany-postq473111315/p/12846865.html

        __new__方法理解

https://www.cnblogs.com/hany-postq473111315/p/12846871.html

        ctime使用及datetime简单使用

https://www.cnblogs.com/hany-postq473111315/p/12846876.html

        functools函数中的partial函数及wraps函数

https://www.cnblogs.com/hany-postq473111315/p/12846888.html

        gc 模块常用函数

https://www.cnblogs.com/hany-postq473111315/p/12846892.html

        hashlib加密算法

https://www.cnblogs.com/hany-postq473111315/p/12846896.html

        __slots__属性

https://www.cnblogs.com/hany-postq473111315/p/12846904.html

        isinstance方法判断可迭代和迭代器

https://www.cnblogs.com/hany-postq473111315/p/12846908.html

        metaclass 拦截类的创建,并返回

https://www.cnblogs.com/hany-postq473111315/p/12846911.html

        timeit_list操作测试

https://www.cnblogs.com/hany-postq473111315/p/12846913.html

        nonlocal 访问变量

https://www.cnblogs.com/hany-postq473111315/p/12846915.html

        pdb 进行调试

https://www.cnblogs.com/hany-postq473111315/p/12846921.html

        使用property取代getter和setter方法

https://www.cnblogs.com/hany-postq473111315/p/12846925.html

        使用types库修改函数

https://www.cnblogs.com/hany-postq473111315/p/12846932.html

        type 创建类,赋予类\静态方法等

https://www.cnblogs.com/hany-postq473111315/p/12846940.html

        迭代器实现斐波那契数列

https://www.cnblogs.com/hany-postq473111315/p/12846943.html

        创建生成器

https://www.cnblogs.com/hany-postq473111315/p/12846945.html

        动态给类的实例对象 或 类 添加属性

https://www.cnblogs.com/hany-postq473111315/p/12846946.html

        线程_同步应用

https://www.cnblogs.com/hany-postq473111315/p/12846947.html

        垃圾回收机制_合集

https://www.cnblogs.com/hany-postq473111315/p/12846950.html

        协程的简单实现

https://www.cnblogs.com/hany-postq473111315/p/12846953.html

        实现了__iter__和__next__的对象是迭代器

https://www.cnblogs.com/hany-postq473111315/p/12846956.html

        对类中私有化的理解

https://www.cnblogs.com/hany-postq473111315/p/12846958.html

        拷贝的一些生成式

https://www.cnblogs.com/hany-postq473111315/p/12846961.html

        查看 __class__属性 

https://www.cnblogs.com/hany-postq473111315/p/12846966.html

        运行过程中给类添加方法 types.MethodType

https://www.cnblogs.com/hany-postq473111315/p/12846970.html

        查看对象的引用计数及计数加一

https://www.cnblogs.com/hany-postq473111315/p/12846975.html

        浅拷贝和深拷贝

https://www.cnblogs.com/hany-postq473111315/p/12846978.html

        点format方式输出星号字典的值是键

https://www.cnblogs.com/hany-postq473111315/p/12846982.html

        类可以打印,赋值,作为实参和实例化

https://www.cnblogs.com/hany-postq473111315/p/12846988.html

        类可以在函数中创建,作为返回值(返回类)

https://www.cnblogs.com/hany-postq473111315/p/12846992.html

        查看某一个字符出现的次数

https://www.cnblogs.com/hany-postq473111315/p/12847012.html

        闭包函数

https://www.cnblogs.com/hany-postq473111315/p/12847006.html

        自定义创建元类

https://www.cnblogs.com/hany-postq473111315/p/12846996.html

        迪杰斯特拉算法(网上找的)

https://www.cnblogs.com/hany-postq473111315/p/12847011.html

        装饰器_上

https://www.cnblogs.com/hany-postq473111315/p/12846997.html

        装饰器_下

https://www.cnblogs.com/hany-postq473111315/p/12847002.html

        Django学习路7_注册app到能够在页面上显示app网页内容

https://www.cnblogs.com/hany-postq473111315/p/12849466.html

        Django学习路8_学生表和班级表级联并相互查询信息

https://www.cnblogs.com/hany-postq473111315/p/12852064.html

        Django学习路9_流程复习

https://www.cnblogs.com/hany-postq473111315/p/12856419.html

        设计模式_理解单例设计模式

https://www.cnblogs.com/hany-postq473111315/p/12856511.html

        设计模式_单例模式的懒汉式实例化

https://www.cnblogs.com/hany-postq473111315/p/12856586.html

        Django学习路10_创建一个新的数据库,指定列名并修改表名

https://www.cnblogs.com/hany-postq473111315/p/12857363.html

        Django学习路11_向数据库中添加  和 获取指定条件数据

https://www.cnblogs.com/hany-postq473111315/p/12859172.html

        Django学习路12_objects 方法(all,filter,exclude,order by,values)

https://www.cnblogs.com/hany-postq473111315/p/12859187.html

        数据库实验内容,不包括对表的增删改查

https://www.cnblogs.com/hany-postq473111315/p/12862517.html

        Django学习路13_创建用户登录,判断数据库中账号名密码是否正确

https://www.cnblogs.com/hany-postq473111315/p/12864746.html

        Django学习路14_获取数据库中用户名字并展示,获取指定条数

https://www.cnblogs.com/hany-postq473111315/p/12867317.html

        在类外创建函数,然后使用类的实例化对象进行调用

https://www.cnblogs.com/hany-postq473111315/p/12867420.html

        Django坑_02

https://www.cnblogs.com/hany-postq473111315/p/12868042.html

        Django学习路15_创建一个订单信息,并查询2020年\9月的信息都有哪些

https://www.cnblogs.com/hany-postq473111315/p/12868272.html

        Django学习路16_获取学生所在的班级名

https://www.cnblogs.com/hany-postq473111315/p/12868775.html

        Django学习路17_聚合函数(Avg平均值,Count数量,Max最大,Min最小,Sum求和)基本使用

https://www.cnblogs.com/hany-postq473111315/p/12868786.html

        Django学习路18_F对象和Q对象

https://www.cnblogs.com/hany-postq473111315/p/12870354.html

        PageRank算法

https://www.cnblogs.com/hany-postq473111315/p/12871080.html

        Python实现数据结构 图

https://www.cnblogs.com/hany-postq473111315/p/12871103.html

        Django学习路19_is_delete属性,重写类方法,显性隐性属性

https://www.cnblogs.com/hany-postq473111315/p/12881137.html

        Django学习路20_流程复习

https://www.cnblogs.com/hany-postq473111315/p/12881490.html

        Django学习路21_views函数中定义字典及html中使用类实例对象的属性及方法

https://www.cnblogs.com/hany-postq473111315/p/12882201.html

        Django学习路22_empty为空,forloop.counter 从1计数,.counter0 从0计数 .revcounter最后末尾数字是1,.revcounter0 倒序,末尾为 0

https://www.cnblogs.com/hany-postq473111315/p/12887676.html

        Django学习路23_if else 语句,if elif else 语句 forloop.first第一个元素 .last最后一个元素,注释

https://www.cnblogs.com/hany-postq473111315/p/12887789.html

        Django学习路24_乘法和除法

https://www.cnblogs.com/hany-postq473111315/p/12890956.html

        Django学习路25_ifequal 和 ifnotequal 判断数值是否相等及加减法 {{数值|add 数值}}

https://www.cnblogs.com/hany-postq473111315/p/12891104.html

        Django学习路26_转换字符串大小写 upper,lower

https://www.cnblogs.com/hany-postq473111315/p/12891253.html

        Django学习路27_HTML转义

https://www.cnblogs.com/hany-postq473111315/p/12898638.html

        Django学习路28_ .html 文件继承及<block 标签>,include 'xxx.html' 

https://www.cnblogs.com/hany-postq473111315/p/12898808.html

        Django学习路29_css样式渲染 h3 标签

https://www.cnblogs.com/hany-postq473111315/p/12899854.html

        Django学习路30_view中存在重复名时,取第一个满足条件的

https://www.cnblogs.com/hany-postq473111315/p/12899918.html

        Django学习路31_使用 locals 简化 context 写法,点击班级显示该班学生信息

https://www.cnblogs.com/hany-postq473111315/p/12909241.html

        分解质因数

https://www.cnblogs.com/hany-postq473111315/p/12910220.html

        计算皮球下落速度

https://www.cnblogs.com/hany-postq473111315/p/12910268.html

        给定年月日,判断是这一年的第几天

https://www.cnblogs.com/hany-postq473111315/p/12910344.html

        Django学习路32_创建管理员及内容补充+前面内容复习

https://www.cnblogs.com/hany-postq473111315/p/12911605.html

        Django学习路33_url 地址及删除元素 delete() 和重定向 return redirect('路径')

https://www.cnblogs.com/hany-postq473111315/p/12917747.html

        Django学习路34_models 文件创建数据表

https://www.cnblogs.com/hany-postq473111315/p/12920060.html

        Django学习路35_视图使用方法(复制的代码) + 简单总结

https://www.cnblogs.com/hany-postq473111315/p/12922362.html

        实验1-5

https://www.cnblogs.com/hany-postq473111315/p/12932750.html

        学生成绩表数据包括:学号,姓名,高数,英语和计算机三门课成绩,计算每个学生总分,每课程平均分,最高分和最低分

https://www.cnblogs.com/hany-postq473111315/p/12939916.html

        四位玫瑰数

https://www.cnblogs.com/hany-postq473111315/p/12940081.html

        四平方和

https://www.cnblogs.com/hany-postq473111315/p/12940256.html

        学生管理系统-明日学院的

https://www.cnblogs.com/hany-postq473111315/p/12941008.html

        定义函数,给定一个列表作为函数参数,将列表中的非数字字符去除

https://www.cnblogs.com/hany-postq473111315/p/12943097.html

        给定几位数,查看数根(使用函数实现)

https://www.cnblogs.com/hany-postq473111315/p/12943740.html

        水果系统(面向过程,面向对象)

https://www.cnblogs.com/hany-postq473111315/p/12950313.html

        matplotlib基础汇总_01

https://www.cnblogs.com/hany-postq473111315/p/12950862.html

        matplotlib基础汇总_02

https://www.cnblogs.com/hany-postq473111315/p/12950897.html

        matplotlib基础汇总_03

https://www.cnblogs.com/hany-postq473111315/p/12950922.html

        matplotlib基础汇总_04

https://www.cnblogs.com/hany-postq473111315/p/12951001.html

        根据列表的值来显示每一个元素出现的次数

https://www.cnblogs.com/hany-postq473111315/p/12951610.html

        钻石和玻璃球游戏(钻石位置固定)

https://www.cnblogs.com/hany-postq473111315/p/12952401.html

        小人推心图(网上代码)

https://www.cnblogs.com/hany-postq473111315/p/12952806.html

        0525习题

https://www.cnblogs.com/hany-postq473111315/p/12957586.html

        jieba尝鲜

https://www.cnblogs.com/hany-postq473111315/p/12957868.html

        inf 

https://www.cnblogs.com/hany-postq473111315/p/12968590.html

        读取文件进行绘图

https://www.cnblogs.com/hany-postq473111315/p/12968611.html

        双约束重力模型

https://www.cnblogs.com/hany-postq473111315/p/12968621.html

        未解决问题01

https://www.cnblogs.com/hany-postq473111315/p/12971205.html

        Sqlite3 实现学生信息增删改查

https://www.cnblogs.com/hany-postq473111315/p/12953587.html

        未解决问题02

https://www.cnblogs.com/hany-postq473111315/p/12971235.html

        简单绘图

https://www.cnblogs.com/hany-postq473111315/p/12971256.html

        钻石和玻璃球游戏(钻石位置不固定)

https://www.cnblogs.com/hany-postq473111315/p/12971307.html

        使用正则匹配数字

https://www.cnblogs.com/hany-postq473111315/p/12971958.html

        打包文件一闪而过及导入文件措施

https://www.cnblogs.com/hany-postq473111315/p/12976957.html

        文件简单操作

https://www.cnblogs.com/hany-postq473111315/p/12978701.html

        0528习题 1-5

https://www.cnblogs.com/hany-postq473111315/p/12978717.html

        Django学习路36_函数参数 反向解析 修改404 页面

https://www.cnblogs.com/hany-postq473111315/p/12980002.html

        关于某一爬虫实例的总结

https://www.cnblogs.com/hany-postq473111315/p/12980219.html

        小技巧_01

https://www.cnblogs.com/hany-postq473111315/p/12983898.html

        安装 kreas 2.2.4 版本问题

https://www.cnblogs.com/hany-postq473111315/p/12984543.html

        是否感染病毒

https://www.cnblogs.com/hany-postq473111315/p/12985989.html

        python文件操作

https://www.cnblogs.com/hany-postq473111315/p/12986729.html

        pandas 几个重要知识点

https://www.cnblogs.com/hany-postq473111315/p/12989028.html

        一千美元的故事(钱放入信封中)

https://www.cnblogs.com/hany-postq473111315/p/12989353.html

        给定两个列表,转换为 DataFrame 类型

https://www.cnblogs.com/hany-postq473111315/p/12989923.html

        通过文档算学生的平均分

https://www.cnblogs.com/hany-postq473111315/p/12990789.html

        0528习题 11-15

https://www.cnblogs.com/hany-postq473111315/p/12978739.html

        0528习题 6-10

https://www.cnblogs.com/hany-postq473111315/p/12978731.html

        0528习题 16-20

https://www.cnblogs.com/hany-postq473111315/p/12978750.html

        0528习题 21-25

https://www.cnblogs.com/hany-postq473111315/p/12978764.html

        0528习题 26-31

https://www.cnblogs.com/hany-postq473111315/p/12978781.html

        python 安装 0x000007b错误解决及VC++ 安装第三方库报红

https://www.cnblogs.com/hany-postq473111315/p/12993454.html

        读取 csv , xlsx 表格并添加总分列

https://www.cnblogs.com/hany-postq473111315/p/13020328.html

        matplotlib 显示中文问题

https://www.cnblogs.com/hany-postq473111315/p/13021265.html

        gbk codec can't encode character

https://www.cnblogs.com/hany-postq473111315/p/13024069.html

        cmd 安装第三方库问题

https://www.cnblogs.com/hany-postq473111315/p/13030558.html

        十进制转换

https://www.cnblogs.com/hany-postq473111315/p/13030835.html

        正则表达式巩固_从别的资料上弄下来的

https://www.cnblogs.com/hany-postq473111315/p/13030596.html

        pandas巩固

https://www.cnblogs.com/hany-postq473111315/p/13034160.html

        numpy巩固

https://www.cnblogs.com/hany-postq473111315/p/13035346.html

        Django学习路37_request属性

https://www.cnblogs.com/hany-postq473111315/p/12981943.html

        菜鸟教程的 mysql-connector 基础

https://www.cnblogs.com/hany-postq473111315/p/13037118.html

        爬虫流程(前面发过的文章的合集)巩固

https://www.cnblogs.com/hany-postq473111315/p/13040839.html

        matplotlib示例

https://www.cnblogs.com/hany-postq473111315/p/13047992.html

        matplotlib颜色线条及绘制直线

https://www.cnblogs.com/hany-postq473111315/p/13049034.html

        matplotlib绘制子图

https://www.cnblogs.com/hany-postq473111315/p/13054122.html

        下载数据到csv中(乱码),使用numpy , pandas读取失败 解决方案

https://www.cnblogs.com/hany-postq473111315/p/13054802.html

        查看一个数所有的因子及因子的和

https://www.cnblogs.com/hany-postq473111315/p/13059879.html

        输入 1,2,4,5,78 返回 (1, 78, 2, 4, 5, 90)  返回形式:最小值 最大值 其余值 及 总和

https://www.cnblogs.com/hany-postq473111315/p/13059889.html

        1000以内能被3或5整除但不能被10整除的数的个数为

https://www.cnblogs.com/hany-postq473111315/p/13059900.html

        输入数字判断是否是偶数,输出两个质数的和为该偶数的值

https://www.cnblogs.com/hany-postq473111315/p/13059922.html

        十进制转换为其他进制(不使用format)

https://www.cnblogs.com/hany-postq473111315/p/13059936.html

        字典元组列表常用方法

https://www.cnblogs.com/hany-postq473111315/p/13061946.html

        设置x 轴斜体(每次我都百度,这次单独为它发一个)

https://www.cnblogs.com/hany-postq473111315/p/13062468.html

        对字典进行排序

https://www.cnblogs.com/hany-postq473111315/p/13080091.html

        终于,我还是对自己的博客下手了

https://www.cnblogs.com/hany-postq473111315/p/13087385.html

        字符串常用函数总结

https://www.cnblogs.com/hany-postq473111315/p/13112074.html

        解决SyntaxError: Non-UTF-8 code starting with '\xbb'问题

https://www.cnblogs.com/hany-postq473111315/p/13113564.html

        获取列表中出现的值,并按降序进行排列

https://www.cnblogs.com/hany-postq473111315/p/13120968.html

        CSV文件指定页脚

https://www.cnblogs.com/hany-postq473111315/p/13130528.html

        重置spyder 解决 gbk 编码不能读取问题

https://www.cnblogs.com/hany-postq473111315/p/13159943.html

        使用 eval(input()) 的便利

https://www.cnblogs.com/hany-postq473111315/p/13159954.html

        flask的第一次尝试

https://www.cnblogs.com/hany-postq473111315/p/13161637.html

        条件表达式

https://www.cnblogs.com/hany-postq473111315/p/13167125.html

        安装fiddler 谷歌插件

https://www.cnblogs.com/hany-postq473111315/p/13168359.html

        绘图小结

https://www.cnblogs.com/hany-postq473111315/p/13169418.html

        今日成果:爬取百度贴吧

https://www.cnblogs.com/hany-postq473111315/p/13170170.html

        map,reduce,filter基础实现

https://www.cnblogs.com/hany-postq473111315/p/13171509.html

        数据分析小题

https://www.cnblogs.com/hany-postq473111315/p/13173809.html

        求最大公约数最小公倍数及整除求余数等

https://www.cnblogs.com/hany-postq473111315/p/13189664.html

        matplotlib 去掉坐标轴

https://www.cnblogs.com/hany-postq473111315/p/13193886.html

        字符串的三个函数

https://www.cnblogs.com/hany-postq473111315/p/13213108.html

        关于这学期的总结

https://www.cnblogs.com/hany-postq473111315/p/13228741.html

        转义字符

https://www.cnblogs.com/hany-postq473111315/p/13233072.html

        format格式

https://www.cnblogs.com/hany-postq473111315/p/13233095.html

        IPython magic命令

https://www.cnblogs.com/hany-postq473111315/p/13246648.html

        关键字

https://www.cnblogs.com/hany-postq473111315/p/13246716.html

        运算符

https://www.cnblogs.com/hany-postq473111315/p/13246732.html

        常用类型转换函数

https://www.cnblogs.com/hany-postq473111315/p/13246740.html

        数学方法内置函数

https://www.cnblogs.com/hany-postq473111315/p/13246749.html

        字符串常用函数

https://www.cnblogs.com/hany-postq473111315/p/13246759.html

        常用内置函数

https://www.cnblogs.com/hany-postq473111315/p/13246765.html

        math库常用函数

https://www.cnblogs.com/hany-postq473111315/p/13246775.html

        random随机数函数

https://www.cnblogs.com/hany-postq473111315/p/13246781.html

        time模块

https://www.cnblogs.com/hany-postq473111315/p/13246790.html

        datetime模块

https://www.cnblogs.com/hany-postq473111315/p/13246801.html

        列表字典集合常用函数

https://www.cnblogs.com/hany-postq473111315/p/13246827.html

        选择结构和循环结构

https://www.cnblogs.com/hany-postq473111315/p/13246839.html

        函数调用

https://www.cnblogs.com/hany-postq473111315/p/13246864.html

        jieba.lcut方法

https://www.cnblogs.com/hany-postq473111315/p/13246933.html

        turtle库常用函数

https://www.cnblogs.com/hany-postq473111315/p/13246941.html

        int转换sys,argv参数问题

https://www.cnblogs.com/hany-postq473111315/p/13246955.html

        文件基本用法

https://www.cnblogs.com/hany-postq473111315/p/13253872.html

        读/写docx文件

https://www.cnblogs.com/hany-postq473111315/p/13253912.html

        读/写xlsx文件

https://www.cnblogs.com/hany-postq473111315/p/13253937.html

        os模块常用方法

https://www.cnblogs.com/hany-postq473111315/p/13253955.html

        pandas属性和方法

https://www.cnblogs.com/hany-postq473111315/p/13254000.html

        numpy的random方法和常用数据类型

https://www.cnblogs.com/hany-postq473111315/p/13254012.html

        matplotlib常用基础知识

https://www.cnblogs.com/hany-postq473111315/p/13254039.html

        学习python的几个资料网站

https://www.cnblogs.com/hany-postq473111315/p/13265849.html

        机器学习网址

https://www.cnblogs.com/hany-postq473111315/p/13278370.html

        机器学习算法速查表

https://www.cnblogs.com/hany-postq473111315/p/13278528.html

        matpltlib 示例

https://www.cnblogs.com/hany-postq473111315/p/13278593.html

        statsmodels 示例

https://www.cnblogs.com/hany-postq473111315/p/13278607.html

        Biopython 第三方库示例

https://www.cnblogs.com/hany-postq473111315/p/13278677.html

        爬取三寸人间

https://www.cnblogs.com/hany-postq473111315/p/13306001.html

        爬取图虫网 示例网址 https://wangxu.tuchong.com/23892889/

https://www.cnblogs.com/hany-postq473111315/p/13306056.html

        爬虫基础巩固

https://www.cnblogs.com/hany-postq473111315/p/13306114.html

        老男孩Django笔记(非原创)

https://www.cnblogs.com/hany-postq473111315/p/13339511.html

当你用心看完这篇文章,相信你一定会有所收获! 加油

import requests
from fake_useragent import UserAgent
from lxml import etree
headers = {
'UserAgent':UserAgent().random
} title_xpath = "//a[@class='postTitle2 vertical-middle']/span/text()"
title_url_xpath = "//a[@class='postTitle2 vertical-middle']/@href"
url_list = ["https://www.cnblogs.com/hany-postq473111315/default.html?page={}".format(num) for num in range(49,0,-1) ]
file = open('随笔.txt','w')
num = 0
for url in url_list:
print("第{}页随笔获取完毕!".format(num + 1))
num = num + 1
response = requests.get(url,headers = headers)
e = etree.HTML(response.text)
title = e.xpath(title_xpath)
url = e.xpath(title_url_xpath)
for t,u in zip(title[::-1],url[::-1]):
file.write(t + '\n')
file.write(u + '\n')
file.close()

python 打包

在线生成图标网站

http://www.ico51.cn/

打包命令(带图标)

pyinstaller -F -i 图标名称.ico 文件名.py

不带图标

pyinstaller -F  文件名.py
将黑框去掉,使用 -w
pyinstaller -F -w -i favicon.ico 文件名.py

 注:此篇随笔为以前的随笔的总结,过于基础的并没有展现,只是将以前的随笔中的重点提取出来了


Python 语言遵循 GPL(GNU General Public Licence) 协议。
GPL 指的是GNU通用公共许可协议,该协议通过提供给作者软件版权保护和许可证保障作者的权益

查看关键字。

help("keywords")

关键字如下:(注:复制代码只复制上一行代码即可,下面为输出结果)
False class from or
None continue global pass
True def if raise
and del import return
as elif in try
assert else is while
async except lambda with
await finally nonlocal yield
break for not

Python具有可扩展性,能够导入使用 C/C++ 语言编写的程序,从而提高程序运行速度。
Python具有可嵌入性,可以把Python程序嵌入到 C/C++ 程序中。

Python支持 GUI 编程,提供多个图形开发界面的库,如 Tkinter ,wxPython ,Jython 等。

学了这么久,tkinter 接触的要明显多余 Jython  和 wxPython ..

在 Windows 中设置环境变量时,打开命令提示框,输入:

path %path%;python的安装目录

Python 的重要环境变量:

PYTHONPATH:使用 import 语句后会从该环境变量进行寻找。

PYTHONSTARTUP:在 Python 启动后,会执行此文件中变量指定的执行代码。

PYTHONCASEOK:写在这里面的环境变量,在导入时模块不区分大小写。

PYTHONHOME:通常存在于 PYTHONSTARTUP 和 PYTHONPATH 目录中,便于切换模块库。

使用 Python 在命令提示符中运行 .py 程序

python 文件名.py 参数 参数

补充:
参数可以使用 sys.args 进行获取
但是是从第2个开始进行获取参数

python2 中使用 

#-*-coding:UTF-8-*-

对中文进行编码

以单个下划线开头的变量或方法 _temp ,表示不能够直接访问的类属性,需要通过类提供的接口(函数)进行访问。
当使用 from xx import * 时,_temp不能够被导入。使用者不应该访问 _temp 的变量或方法。

以两个下划线开头的变量 __temp ,可以通过类提供的接口(函数)进行访问。
使用了__xxx 表示的变量或方法,实际上是实现了 名称转写 机制。 __temp 会被转写成 _classname__temp ,避免了使用者的错误访问。
使用 __temp 的类,在被子类继承时,能够避免子类中方法的命名冲突。
定义子类时,往往会使用到父类的 __init__构造方法,实际上为了避免冲突,调用的是_父类名 _initalize() 方法。

输出关键字的另一种方式

import keyword
print(keyword.kwlist) ['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break',
'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally',
'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal',
'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

类方法和静态方法在使用装饰器方面(好久没有使用了,有点生疏了)

@classmethod
def eat(cls):
pass @staticmethod
def eat():
pass

关于 finally 语句,最后一定会执行的问题,有一些遗忘

# 使用 try 语句
try :
pass
except Exception as e:
pass
finally:
pass

使用 ctime 获取当前时间

from time import ctime
print("{}".format(ctime()))

%c 格式化字符及其ASCII码
%s 格式化字符串
%d 格式化整数
%u 格式化无符号整型
%f 格式化浮点数字,可指定小数点后的精度
%e 用科学计数法格式化浮点数
%E 作用同%e,用科学计数法格式化浮点数

print("pi = %.*f" % (3,pi)) #用*从后面的元组中读取字段宽度或精度
# pi = 3.142 print('%010.3f' % pi) #用0填充空白
# 000003.142 print('%-10.3f' % pi) #使用 - 号左对齐
# 3.142

在 Python 中,变量不一定占用内存变量。变量就像是对某一处内存的引用,可以通过变量访问到其所指向的内存中的值,
并且可以让变量指向其他的内存。在 Python 中,变量不需要声明,但是使用变量之前,一定要先对变量进行赋值。
当创建了一个新对象时,Python 会向操作系统请求内存,Python 本身实现了内存分配系统。变量类型指的是变量所指向的内存中 对象 的类型。
Python 中变量赋值使用 = 等号,等号左面为创建的变量,等号右面为需要的值。

变量包含的内容主要包含四个方面:

    1.变量的名称:在对变量赋值时也就是创建变量时所使用的名字。注:根据标识符规则。

    2.变量保存的数据:通常为赋值时 = 等号 右面的对象。

     对象主要包括:

      ①.数字:int 、float 、complex 、bool、表达式、函数调用返回值等。

          数字: int 表示整数,包含正数,负数,0

            float 表示浮点数,带有小数点的数

            complex 表示复数,实部 + 虚部 J 或 j 均可
 bool 布尔类型,True 为真,False 为假
②.字符串:字符串变量、带有" "的字符串、表达式、函数调用的返回值等。           注:Python3 以 Unicode 编码方式编码。           使用双引号 " " 或单引号 ' ' 创建字符串或者进行强制转换 str 。
③.列表:列表变量、带有 [ ] 的对象、表达式、函数调用的返回值等。           使用了 [ ] 的,[ ] 内可以是数字,字符串,字典,元组,列表,集合,表达式等。
④.元组:元组变量、带有逗号的或被( )包围的多个变量或值、表达式、函数调用的返回值等。           空元组 ( )           创建一个只包含数字 1 的元素的元组 (1,) 注:一定要带有 , 号           创建包含多个元素的元组,可以直接用 (元素1,元素2,...,元素n) 赋值             或者元素1,元素2,...,元素n ,使用,逗号进行赋值 ⑤.集合:空集合 set( )、使用了{ }的内部为单个变量或值、表达式、函数调用的返回值等。           空集合 set( )           创建多个元素的集合,{元素1,元素2,...,元素n}           注:集合中元素不重复,可利用此特性判断别的序列对象是否存在重复元素。 ⑥.字典:字典变量、带有 {键:值} 的变量或值、表达式、函数调用的返回值等。           空字典 { }           创建多个元素的字典,变量名 = {键1:值1,键2:值2,...,键n:值n} ⑦.类:通常为类创建实例时,函数调用的返回值等。           class关键字声明。       ⑧.函数:函数名、函数调用等。           def 关键字声明,在函数前可能会有装饰器。另外,函数可以嵌套函数,当内部函数使用了外部函数的某些对象时称为闭包函数。       注:表达式是指关于对象之间的运算。

变量的地址,也就是所指向的内存中的地址。使用 id(变量名) 函数获取。

# 查看 a 的内存地址
a = 123
print(id(a))
# 140734656708688

字典: {键 : 值} 对的元素的集合。字典内部的元素是无序的,通过键来获取键所对应的值。字典中的键是不能够改变的,并且是唯一的。

# 创建一个元素的集合,可以不使用 ,
set_2 = {1}
print(set_2)
# {1}
print(type(set_2))
# <class 'set'>
set_3 = {1,}
print(set_3)
# {1}
print(type(set_3))
# <class 'set'>

集合中不能包含列表和字典对象

# 负数
num_int_3 = -226
print(type(num_int_3))
# <class 'int'>

# 扩大100倍
num_float_3 = 2.5e2
print(num_float_3)
# 250.0
print(type(num_float_3))
# <class 'float'>

关于复数 0 进行隐藏问题

num_complex_3 = 3.j
print(num_complex_3)
# 3j
print(type(num_complex_3))
# <class 'complex'> num_complex_4 = .6j
print(num_complex_4)
# 0.6j
print(type(num_complex_4))
# <class 'complex'>

有时判断条件是否成立使用 not True
比如说某一个元素不在列表中使用 not in

Unicode码:主要有三种,分别为 utf-8、utf-16、utf-32。utf-8 占用一到四个字节,utf-16 占用二到四个字节,utf-32 占用四个字节。

  Python 在访问时,使用方括号 [ 索引位置 ] ,进行访问。字符串可以进行拼接操作,就是将两个字符串进行拼接,然后产生新的字符串。可以进行切片操作 [ : ] ,(注:左闭右开)。

列表可以进行增加元素、删除元素、查询是否存在该元素、修改某一位置上的元素、查看列表的长度、确定最大最小元素以及对列表排序等。有时候,还可以通过强制转换修改元组。

Python 的元组与列表类似。元组使用小括号 ( ) 包含数据。元组可以通过索引下标进行访问元组中的值。元组中的值不是允许修改的,但是可以对元组进行拼接。

# 创建只包含一个元素的元组
tuple_2 = (1,)
print(type(tuple_1))
# <class 'tuple'> # 创建包含多个元素的元组
tuple_4 = 7,8,9
print(tuple_4)
# (7, 8, 9)
print(type(tuple_4))
# <class 'tuple'>

字典创建之后,可以使用 字典名['键名'] 进行访问。

增加字典元素,可以直接使用 字典名['新的键'] = 新的值

使用 del 可以将字典元素进行删除。

可以对字典求长度,强制转换,拷贝字典等操作。

注:当后来又添加了新的键,而原来有同名的键时,以后来的为准。

在字典中创建键值对,没有写明: 时,创建的是值

dic = {'a':123,888:'n',(4,5):[7,8]}
dic.keys()
# dict_keys(['a', 888, (4, 5)])
dic.values()
# dict_values([123, 'n', [7, 8]])

# 使用 dict 转化为字典
dic = dict(zip(['a','b','c'],[4,5,6]))
print(dic)
# {'a': 4, 'b': 5, 'c': 6}

  Python中的数据类型可以进行相互转换:

    1.将 float 浮点型转化成 int 长整型。int( )

    2. 将 2,3 转化为复数。complex(实部,虚部)

    3.将数字、列表、元组、字典转化为字符串类型。str( ) , json.dumps(字典)

    4.将字符串转化为数字类型。eval( )

    5.将列表转化成元组。tuple( )

    6.将元组转化成列表。list( )

    7.将列表转化成集合,用来消除多余重复元素。set( )

    8.将字符串转化为集合元素。set( )

       9.将整数转化为字符。 chr( )

    10.将字符转化为整数。ord( )

    11.将十进制整数转化为十六进制数。hex( )

    12.将十进制整数转化为八进制数。 oct( )

# 将整数转化为字符。
print(chr(65))
# A
print(chr(90))
# Z
print(chr(97))
# a
print(chr(122))
# z # 将字符转化为整数。
print(ord('A'))
# 65
# 将十进制整数转化为十六进制数。
print(hex(17))
# 0x11
# 将十进制整数转化为八进制数。
print(oct(9))
# 0o11

Python算术运算符。

  算术运算符:

    + :两个对象相加。

      -:得到负数 或 前一个数减去后一个数。

    * : 两个数进行相乘 或 重复字符串元素、列表元素、元组元素。

    / : 前一个数对后一个数进行除法操作,返回浮点数类型。

     %: 取模,返回前一个数除去后一个数的余数。

     ** : 返回前一个数的后面数次幂。

     // : 前一个数对后一个数整除,返回整数部分。

Python 比较运算符,多用于条件判断语句 if 中,返回值为 True (真)或 False (假):

  == : 等于,比较两个对象的值是否相等。

   != : 不等于,比较两个对象的值是否不相等。

  >   :  大于,前面一个数是否大于后面的数。

  <   :  小于,前面一个数是否小于后面的数。

    >= : 大于等于,前面的是是否大于等于后面的数。

    <=:  小于等于,前面的数是否小于等于后面的数。

Python赋值运算符:

  = : 赋值运算符

  += : 加法赋值运算符

  -= : 减法赋值运算符

  *= : 乘法赋值运算符 

   /= : 除法赋值运算符

 %= : 取模赋值运算符 ,当前面的数小于后面的数时,返回前一个数本身(数大于 0)。

  **= : 幂赋值运算符

  //= : 取整赋值运算符

 注:a 符号等于 b 等价于 a 等于 a 符号 (b)

# *= 乘法赋值运算符
a = 4
b = 5
a *= b #等价于 a = a * (b)
print("a = {0} , b = {1} ".format(a,b))
# a = 20 , b = 5 # /= 除法赋值运算符
a = 4
b = 5
a /= b #等价于 a = a / (b)
print("a = {0} , b = {1} ".format(a,b))
# a = 0.8 , b = 5 # %= 取模赋值运算符
a = 4
b = 5
a %= b #等价于 a = a % (b)
print("a = {0} , b = {1} ".format(a,b))
# a = 4 , b = 5 a = 6
b = 4
a %= b #等价于 a = a % (b)
print("a = {0} , b = {1} ".format(a,b))
# a = 2 , b = 4 # **= 幂赋值运算符
a = 4
b = 2
a **= b #等价于 a = a ** (b)
print("a = {0} , b = {1} ".format(a,b))
# a = 16 , b = 2 # //= 取整赋值运算符,返回整数
a = 4
b = 3
a //= b #等价于 a = a // (b)
print("a = {0} , b = {1} ".format(a,b))
# a = 1 , b = 3

Python位运算符:将 int 长整型数据看做二进制进行计算,主要是将前面的数和后面的数的对应位置上的数字 0,1 进行判断。

     & 按位与:如果对应位置上的两个数都为 1,那么得到的该结果的该位置上也为 1 。其他情况都为 0。

     | 按位或:如果对应位置上的两个数有一个为 1 或都为 1,则得到的该结果的该位置上也为 1 。其他情况都为 0。

    ^ 按位异或:如果对应位置上的两个数为 0 和 1 或 1 和 0,则得到的该结果的该位置上也为 1 。其他情况都为 0。

    ~ 按位取反:如果~后面为正数或 0,则结果为-(数+1),

          如果后面的数为负数,则结果为-(负数(带符号)+1)。

   << 左移运算符:将前面的数乘以 2 的(后面的数) 次幂。

   >> 右移运算符:将前面的数除以 2 的(后面的数) 次幂。

# ~ 按位取反:如果后面的为正数,则结果为-(正数+1)
print(~2)
# -3 # 如果后面的数为负数,则结果为-(负数(带符号)+1)。
print(~(-5))
# 4

注意返回的是对象,不是True 和 False

Python逻辑运算符:

  and 布尔‘与’: 当左面的对象为真时,返回右面的对象。

          当左面的对象不为真时,返回左面的对象。

   or 布尔‘或’: 当左面的对象为真时,返回左面的对象。

          当左面的对象不为真时,返回右面的对象。

  not 布尔'非': 如果后面的对象为True,则返回False。否则返回True。

Python成员运算符:

  in:如果左面的对象在右面的对象中,则返回 True,不在则返回 False。

 not in:如果左面的对象不在右面的对象中,则返回 True,在则返回 False。

a = 'a'
d = 'd'
dic = {'a':123,'b':456,'c':789}
# 判断 a 是否在 dic 中
# 字典主要是看,是否存在该键
print(a in dic)
# True
# 判断 d 是否在 s 中
print(d in dic)
# False
a = 'a'
d = 'd'
strs = 'abc' # 判断 a 是否不在 strs 中
print(a not in strs)
# False
# 判断 d 是否不在 strs 中
print(d not in strs)
# True

Python身份运算符:

  is :判断左右两个对象内存地址是否相等。

  is not :判断左右两个对象内存地址是否不相等。

  注:对于不可变类型数据,当引用自相同数据时,is 返回值为 True 。

      数字、字符串、元组。

    对于可变类型数据,当引用自相同数据时,is not 返回值为 True 。

      列表、字典、集合。

# 对于不可变类型数据,引用自相同数据时,is 为真
# 数字
num = 123
num_two = 123
# 输出 num 和 num_two 的地址
print("num地址为:{0},num_two地址为:{1}".format(id(num),id(num_two)))
# num地址为:140729798131792,num_two地址为:140729798131792
print(num is num_two)
# True ,num 和 num_two 指向同一块内存地址
print(num is not num_two)
# False

# 对于可变类型,即使引用自相同数据,内存地址也不相同。is not 为 True
# 列表
lst = [1,2,3]
lst_two = [1,2,3]
# 输出 lst 和 lst_two 的地址
print("lst地址为:{0},lst_two地址为:{1}".format(id(lst),id(lst_two)))
# lst地址为:2781811921480,lst_two地址为:2781811921992
print(lst is lst_two)
# False
print(lst is not lst_two)
# True

Python运算符优先级(从高到低、有括号则最先算括号):

 ** :指数

 ~ 按位取反

 * 乘法、/ 除法、% 取模、// 整除

 + 加法、- 减法

 >> 右移运算、<< 左移运算

 & 按位与

 ^ 按位异或、| 按位或

 <= 小于等于、< 小于、> 大于、>= 大于等于

 == 是否相等、!= 是否不相等

 = 赋值、%= 取模赋值、/= 除法赋值、//= 整除赋值、-= 减法赋值、+= 加法赋值、*= 乘法赋值、**= 幂赋值

 is 是、is not 不是  引用自同一地址空间

 in 是否在、not in 是否不在

 not 非、and 与、or 或

条件语句的几种情况

第一种:

'''
if 条件1:
条件1满足时,需要运行的内容
''' 第二种: '''
if 条件1:
条件1满足时,需要运行的内容
else:
条件1不满足时,需要运行的内容
''' 第三种: '''
if 条件1:
条件1满足时,需要运行的内容
elif 条件2:
条件1不满足时,条件2满足,需要运行的内容
''' 第四种: '''
if 条件1:
当条件1满足时,需要运行的内容
elif 条件2:
当条件1不满足,满足条件2时,需要运行的内容 ... elif 条件n:
前面的 n-1 条条件都不满足,第n条条件满足,需要运行的内容
else:
前面的所有条件都不满足时,需要运行的内容
'''

在 if 中常用的操作运算符:

  < 小于、<= 小于等于、> 大于、>= 大于等于、== 等于、!= 不等于

  注:可以配合 and、or、not 进行混合搭配。

Python 中的循环包括 for 循环和 while 循环。

while 循环,当给定的判断条件为 True 时,会执行循环体,否则退出循环。(可能不知道具体执行多少次)

for 循环,重复执行某一块语句,执行 n 次。

在 Python 中可以进行嵌套使用循环。while 中包含 for ,或 for 包含 while。

Python循环控制语句:主要有三种,break、continue 和 pass 语句。

  break   语句 :在语句块执行过程中,终止循环、并跳出整个循环。

  continue 语句  :在语句执行过程中,跳出本次循环,进行下一次循环。

  pass     语句  :空语句,用来保持结构的完整性。

Python while循环语句(代码块中要有使判断条件不成立的时候、否则会陷入无限循环):

第一种结构:

'''
while 判断条件:
一行语句 或 多行语句组
''' 第二种结构、else 表示只有程序正常运行才会进行使用的代码块: '''
while 判断条件:
一行语句 或 多行语句组
else:
一行语句 或 多行语句组
'''

Python 无限循环:在 while 循环语句中,可以通过让判断条件一直达不到 False ,实现无限循环。

Python while 循环中使用 else 语句:

  else:表示 while 中的语句正常执行完,然后执行 else 语句的部分。

示例:

while 判断条件:
一行语句 或 多行语句组
else:
一行语句 或 多行语句组

Python for 循环语句:遍历任何序列的项目,可以是字符串、列表、元组、字典、集合对象
for 中的 else 依旧为正常执行之后会进行输出的代码块

第一种:

'''
for 迭代对象 in 序列:
代码块(一行语句或多行代码)
'''
第二种: '''
for 迭代对象 in 序列:
代码块(一行语句或多行代码)
else:
代码块(一行语句或多行代码)
'''

Python for 循环通过序列索引迭代:

注:集合 和 字典 不可以通过索引进行获取元素,因为集合和字典都是无序的。

使用 len (参数) 方法可以获取到遍历对象的长度。

使用 range 方法(左闭右开):

range 函数参数如下,起始位置、终止位置(不包含)、步长。

  注:起始位置默认为 0 。

    步长可以为负,默认为 1。
lst = [i for i in range(5)]
print(lst) # 起始位置默认为 0
# [0, 1, 2, 3, 4] lst = [i for i in range(1,5)]
print(lst) # 不包含终止位置
# [1, 2, 3, 4] lst = [i for i in range(1,5,2)]
print(lst) #步长可以根据自己需要进行更改
# [1, 3] lst = [i for i in range(-5,-1,1)]
print(lst) # 起始位置和终止位置可以为负
# [-5, -4, -3, -2]

通过序列索引进行迭代操作程序:

字符串:
strs = "Hello World."
for i in range(len(strs)):
print(strs[i],end = " ")
# H e l l o W o r l d .

Python循环嵌套:将 for 循环和 while 循环进行嵌套。

示例:

while 循环嵌套 for 循环:

while True:
for i in range(3):
print("while 和 for 进行嵌套")
break
# while 和 for 进行嵌套
# while 和 for 进行嵌套
# while 和 for 进行嵌套
for 循环嵌套 while 循环、不推荐进行使用:

a = 1
for i in range(3):
while a < 3:
print("while 和 for 进行嵌套")
a += 1
# while 和 for 进行嵌套
# while 和 for 进行嵌套

关于 break 的理解

Python break语句:当运行到 break 语句时,终止包含 break 的循环语句。

注:无论判断条件是否达到 False 或 序列是否遍历完都会停止执行循环语句和该 break 下的所有语句。

  当使用循环嵌套时,break 语句将会终止最内层的 while 或 for 语句、然后执行外一层的 while 或 for 循环。

lst = [7,8,9,4,5,6]
for i in range(len(lst)):
if lst[i] == 4:
print("循环终止")
break #终止循环语句
print(lst[i],end = " ")
# 7 8 9 循环终止

关于 continue 的理解

当执行到 continue 语句时,将不再执行本次循环中 continue 语句接下来的部分,而是继续下一次循环

lst = [7,8,9,4,5,6]

for i in range(len(lst)):
if lst[i] == 9:
continue
#当运行到 continue 语句时,不执行本次循环中剩余的代码,而是继续下一层循环
print(lst[i],end = " ")
# 7 8 4 5 6

Python pass语句:空语句,主要用于保持程序结构的完整性 或者 函数想要添加某种功能,但是还没有想好具体应该怎么写。

Python数字类型转换:

  int(x):将 x 转换为一个整数

  float(x):将 x 转换为一个浮点数

  complex(x,y):将 x 和 y 转换为一个复数。x 为复数的实部,y 为复数的虚部。

  eval(x):将 x 转化为一个整数

  chr(x):x 为数字,将数字转化为对应的 ASCII 码。 65 -> A  、90 -> Z 

  ord(x):x 为单个字符,将字符转换为对应的整数。 a -> 97、122 -> z

# 将 2,3 转化为复数
num_complex = complex(2,3)
print(num_complex)
# (2+3j)
print(type(num_complex))
# <class 'complex'>

Python数学函数

abs(x)
返回数字的绝对值,如abs(-10) 返回 10
math.ceil(x)
返回数字的上入整数,如math.ceil(4.1) 返回 5
math.exp(x)
返回e的x次幂(ex),如math.exp(1) 返回2.718281828459045
fabs(x)
返回数字的绝对值,如math.fabs(-10) 返回10.0
floor(x)
返回数字的下舍整数,如math.floor(4.9)返回 4
log(x)
如math.log(math.e)返回1.0,math.log(100,10)返回2.0
log10(x)
返回以10为基数的x的对数,如math.log10(100)返回 2.0
max(x1, x2,...)
返回给定参数的最大值,参数可以为序列。
min(x1, x2,...)
返回给定参数的最小值,参数可以为序列。
modf(x)
返回x的整数部分与小数部分,两部分的数值符号与x相同,整数部分以浮点型表示。
pow(x, y)
x**y 运算后的值。
round(x [,n])
返回浮点数 x 的四舍五入值,如给出 n 值,则代表舍入到小数点后的位数。
其实准确的说是保留值将保留到离上一位更近的一端。
sqrt(x)
返回数字x的平方根。

# math.floor(x) 返回比 x 稍小的整数
print(math.floor(-5.9),math.floor(8.6))
# -6 8

# math.modf(x)  返回 x 的整数部分与小数部分,
# 两部分的数值符号与x相同,整数部分以浮点型表示。
print(math.modf(-5.9),math.modf(8.6))
# (-0.9000000000000004, -5.0) (0.5999999999999996, 8.0)

# math.round(x[,n]) # n 为保留的位数,将 x 进行四舍五入输出
print(round(-5.984,2),round(8.646,2))
# -5.98 8.65

# math.log(x) log 以 e 结尾,e 的 返回值 为 x
print(math.log(math.e),math.log(math.e ** 2))
# 1.0 2.0

Python随机数函数:

choice(seq)
从序列的元素中随机选出一个元素
randrange ([start,] stop [,step])
从指定范围内,在指定步长递增的集合中 获取一个随机数,步长默认为 1 。注:不包含 stop 值
random()
随机生成下一个实数,它在[0,1)范围内。
shuffle(lst)
将序列的所有元素随机排序,返回值为 None
uniform(x, y)
随机生成下一个实数,它在[x,y]范围内。

python 三角函数

math.sin(x)
返回的x弧度的正弦值。
math.asin(x)
返回x的反正弦弧度值。 math.cos(x)
返回x的弧度的余弦值。
math.acos(x)
返回x的反余弦弧度值。 math.tan(x)
返回x弧度的正切值。
math.atan(x)
返回x的反正切弧度值。 math.degrees(x)
将弧度转换为角度,如degrees(math.pi/2) , 返回90.0
math.radians(x)
将角度转换为弧度
math.hypot(x, y)
返回 sqrt(x*x + y*y) 的值。

import math

# π/2 的正弦值
print(math.sin(math.pi/2))
# 1.0 # 1 的反正弦值
print(math.asin(1))
# 1.5707963267948966 π/2 # π 的余弦值
print(math.cos(math.pi))
# -1.0 # -1 的反余弦值
print(math.acos(-1))
# 3.141592653589793 # 四分之三 π 的正切值
print(math.tan(math.pi*3/4))
# -1.0000000000000002 # 使用 math.degrees(x) 函数查看 四分之一 π 的角度
print(math.degrees(math.pi/4))
# 45.0 # 使用 math.radians(x) 函数查看 135° 对应的弧度制
print(math.radians(135))
# 2.356194490192345
print(math.pi*3/4)
# 2.356194490192345 # math.hypot(x, y) 查看 sqrt(x*x + y*y) 的值
print(math.hypot(3,4))
# 5.0
print(math.hypot(6,8))
# 10.0 print(math.sqrt(6*6 + 8*8))
# 10.0

Python数学常量:

  math.pi:π

  math.e:自然常数 e

# lg 函数中求值

a = math.e

b = math.e ** 5

print("ln(a)的值为:",math.log(a))
# ln(a)的值为: 1.0 print("ln(b)的值为:",math.log(b))
# ln(b)的值为: 5.0

Python创建字符串:

  一般情况下可以使用 ' 或 " 创建字符串 或 使用引用字符串变量 或 字符串表达式。

# 使用字符串表达式进行赋值
a = 'ABCD'
b = 'EFG'
c = a + b
print(c)

Python访问字符串中的值:

1.可以使用索引下标进行访问,索引下标从 0 开始:

# 使用索引下标进行访问,索引下标从 0 开始
strs = "ABCDEFG"
print(strs[0])
# A 2.使用切片操作获取字符串: 示例:[start:stop:step]   start :需要获取的字符串的开始位置,默认为 0 。(通常可以不写)   stop :需要获取的字符串的结束位置 的后一个位置。   step :步长,默认为 1 、当 start 大于 stop 时,step 为负数。 # 使用[start:stop:step]切片操作获取字符串 strs = "ABCDEFG" print(strs[:4])
# ABCD print(strs[:4:2])
# AC print(strs[2:6])
# CDEF print(strs[2:6:2])
# CE # 不包含结束位置
print(strs[6:2:-1])
# GFED print(strs[6:2:-2])
# GE 3.通过 for 循环进行获取字符串: strs = "ABCDEFG"
for i in strs:
# 其中 i 为字符串中的单个字母
# 注:此时的 i 不要用做索引下标
print(i,end =" ")
# A B C D E F G

Python字符串更新:截取字符串的某一部分 和 其他字符串进行拼接。

  注:可以修改字符串的值,但修改的不是内存中的值,而是创建新的字符串。

1.使用字符串常量进行更新:

# 使用字符串常量
strs = "hello,hey"
print(strs[:6] + "world.")
# hello,world. 2.使用切片操作(不包含结尾 stop)进行更新: strs = "hello,hey"
py = "Tom,Jerry" s_2 = strs[:5] + py[3:] print(strs[:5])
# hello
print(py[3:])
# ,Jerry
print("更新后的字符串:",s_2)
# 更新后的字符串: hello,Jerry 修改字符串: # 修改字符串,将 world 修改为 python
strs = "hello,world"
strs = strs[:6] + "python"
print("更新后的字符串:{0}".format(strs))
# 更新后的字符串:hello,python

Python转义字符:当需要在字符串中使用特殊字符时,使用 \ 转义字符。

注:转义字符在字符串中,注释也是字符串类型。

\(在行尾时):续行符
\\ :反斜杠符号
\' :单引号
\" :双引号
\a :响铃
\b :退格(Backspace)
\000:空
\n :换行
\v :纵向制表符
\t :横向制表符
\r :回车

Python字符串运算符:

  + :连接左右两端的字符串。

  *  :重复输出字符串。

  [ ] :通过索引获取字符串中的值。

  [start:stop:step]:开始,结束位置的后一个位置,步长。

  in  :判断左端的字符是否在右面的序列中。

  not in:判断左端的字符是否不在右面的序列中。

  r/R :在字符串开头使用,使转义字符失效。

Python字符串格式化:

字符串中符号:

  %c :单个字符

  %s :字符串

  %d :整数

  %u :无符号整数

  %o :无符号八进制数

  %x :无符号十六进制数

  %X :无符号十六进制数(大写)

  %f  :浮点数,可指定小数点后的精度

  %e  :对浮点数使用科学计数法,可指定小数点后的精度。%E 与 %e 作用相同

  %g  :%f 和 %e 的简写,%G 与 %g 作用相同

注:%o 为八进制(oct)、%x 为十六进制(hex)。

# %o 八进制数

num = 11
print("%o"%(num))
# 13 1*8**1 + 3*8**0 = 11 print(oct(11))
# 0o13 # %x 十六进制数
num = 18
print("%x"%(num))
# 12 1*16**1 + 2*8**0 = 18 print(hex(num))
# 0o12

# %e 科学计数法
num = 120000
print("%e"%(num))
# 1.200000e+05 print("%.2e"%(num))
# 1.20e+05 print("%E"%(num))
# 1.200000E+05 print("%.2E"%(num))
# 1.20E+05 # %g : %f 和 %e 的简写
num = 31415926
print("%g"%(num))
# 3.14159e+07 print("%G"%(num))
# 3.14159E+07

格式化操作符的辅助指令:

  * :定义宽度 或 小数点精度

  - : 左对齐

  + : 使正数显示符号

  <sp>:在正数前显示空格

   # :在八进制前显示 0 ,在十六进制前显示 0x 或 0X

   0 :显示的数字前面填充 '0' 

  % :%%输出单个%

 (var) :字典参数映射变量

 m.n. :m是显示的宽度,n 是小数点后的位数

Python三引号:多用作注释、数据库语句、编写 HTML 文本

UTF-8 编码将英文字母编码成一个字节,汉字通常是三个字节。适用于存在大量英文字符时,节省空间

Python字符串内建函数:

注:汉字属于字符(既是大写又是小写)、数字可以是: Unicode 数字,全角数字(双字节),罗马数字,汉字数字。

1.capitalize( ):
将字符串第一个字母大写 # 使用 字符串.capitalize() 方法将字符串首字母大写
strs = 'abc'
print(strs.capitalize())
# Abc
2.center(width[,fillchar]) :
让字符串在 width 长度居中,两边填充 fillchar 字符(默认是空格) # center(width,fillchar)
# 使用 字符串.center() 方法,将字符串在 width 长度居中,两边补充 fillchar
strs = 'abcdefgh'
print(strs.center(20,'-'))
#------abcdefgh------
3.count(str,start=0,end=len(string)):
返回 str 在字符串从 start 到 end 范围内出现的次数(不包含end)。 # 使用 字符串.count(str) 方法,返回 str 在 字符串中出现的次数
strs = 'abcdefghabcd'
print(strs.count('c'))
#2 # 使用 字符串.count(str) 方法,返回 str 在 字符串中出现的次数
strs = 'abcdefghabcd' # a 的索引位置为 0,8
print(len(strs)) # 12
print(strs.count('a',2,8))
# 0
print(strs.count('a',2,9))
# 1 4.bytes.decode(encoding="UTF-8"):
将字节码转换为字符串 strs_bytes = b'\xe6\xac\xa2\xe8\xbf\x8e'
print(strs_bytes.decode(encoding='UTF-8'))
# 欢迎
5.encode(encoding='UTF-8'):
将字符串转换为字节码 strs = '欢迎'
print(strs.encode(encoding='UTF-8'))
# b'\xe6\xac\xa2\xe8\xbf\x8e'
6.endswith(str[,start[,end]]): 判断字符串在 start 到 end 是否以 str结尾 # 字符串.endswith(str[,start[,end]])
strs = 'ABCDEFG'
print(strs.endswith('G'))
# True
print(strs.endswith('F',0,6))
# True
7.expandtabs(tabsize = 4):
将字符串中的 tab 符号转换为空格,tabsize 为替换的空格数 # 字符串.expandtabs(tabsize = 4)
# 将字符串中的 tab 符号转换为空格,tabsize 为替换的空格数
strs = 'ABCD EFG'
print(strs.expandtabs(tabsize = 4))
# ABCD EFG
8.find(str,start = 0,end = len(string)): 在 start 到 end 范围内寻找 str 元素,如果找到则返回 str 元素的索引位置,否则返回 -1。 # find(str,start = 0,end = len(string)):
# 在 start 到 end 范围内寻找 str 元素,如果找到则返回 str 元素的索引位置,否则返回 -1
strs = 'ABCDEFG' #索引位置,从 0 开始
print(strs.find('E'))
# 4
print(strs.find('K'))
# -1 9.index(str,start = 0,end = len(string)):
在 start 到 end 范围内寻找 str 元素,如果找到则返回 str 元素的索引位置,找不到则会报错。 # index(str,start = 0,end = len(string)):
# 在 start 到 end 范围内寻找 str 元素,如果找到则返回 str 元素的索引位置,找不到则返回-1。
strs = 'ABCDEFG'
print(strs.index('F'))
# 5
10.isalnum( ):
如果字符串所有字符都是 字母 或者 数字 则返回 True,否则返回 False。 # isalnum( ):
# 如果字符串所有字符都是 字母 或者 数字 则返回 True,否则返回 False。
strs = 'abcd123'
print(strs.isalnum())
# True strs = '好的'
print(strs.isalnum())
# True strs = 'abc_'
print(strs.isalnum())
# False 11.isalpha( ):
如果字符串中所有字符都是字母则返回 True,否则返回 False。 # isalpha( ):
# 如果字符串中所有字符都是字母则返回 True,否则返回 False。
strs = 'ABCD汉字'
print(strs.isalpha())
# True strs_two = 'ABCD123'
print(strs_two.isalpha())
# False 12.isdigit( ):
如果字符串中所有字符都是数字则返回True,否则返回 False。 # isdigit( ):
# 如果字符串中所有字符都是数字则返回True,否则返回 False。
# 注: ① 也是数字
strs = '①②12'
print(strs.isdigit())
# True strs_two = 'ABCD123'
print(strs_two.isdigit())
# False 13.islower( ):
如果字符串中所有能够区分大小写的字符都是小写的,则返回True。否则返回 False。 # islower( ):
# 如果字符串中所有字符都是小写的,则返回True。否则返回 False。
strs = 'abcd'
print(strs.islower())
# True
strs_two = 'abc123'
print(strs.islower())
# True
strs_three = 'Abcd'
print(strs_three.islower())
# False 14.isnumeric( ):
如果字符串只包含数字字符,则返回 True。否则返回 False。 # isnumeric( ):
# 如果字符串只包含数字字符,则返回 True。否则返回 False。
strs = '123456'
print(strs.isnumeric())
#True
strs_two = '½⅓123①②ⅡⅣ❶❷'
print(strs_two.isnumeric())
# True
strs_three = 'abc123A'
print(strs_three.isnumeric())
# False 15.isspace( ):
如果字符串只包含空格,则返回True。否则返回False。 # isspace( ):
# 如果字符串只包含空格,则返回True。否则返回False。
strs = ' '
print(strs.isspace())
# True strs = ' 1'
print(strs.isspace())
# False 16.istitle( ):
如果所有被空格分割成的子字符串的首字母都大写,则返回 True。否则返回 False。 # istitle( )
# 如果所有被空格分割成的子字符串的首字母都大写,则返回 True。否则返回 False。
strs = 'Hello World'
print(strs.istitle())
# True
strs_two = 'Welcome to Harbin'
print(strs_two.istitle())
# False
strs_three = 'World T12'
print(strs_three.istitle())
# True 17.isupper( ) :
如果字符串中所有能够区分大小写的字符都是大写的,则返回True。否则返回 False。 # isupper( ) :
# 如果字符串中所有能够区分大小写的字符都是大写的,则返回True。否则返回 False。
strs = 'ABCD123汉字'
print(strs.isupper())
# True strs_two = 'ABCabc汉字'
print(strs_two.isupper())
# False

Python字符串内建函数:

1.join(str) :
使用调用的字符串对 str 进行分割,返回值为字符串类型 # join(str) :
# 使用调用的字符串对 str 进行分割。
strs = "Hello"
strs_two = ' '.join(strs)
print(strs_two)
# H e l l o
print(','.join(strs))
# H,e,l,l,o 2.len(string):
返回字符串的长度 # len(string):
# 返回字符串的长度 strs = 'happy'
print(len(strs))
# 5 3.ljust(width[,fillchar]): 之前的是 center 函数,也可以进行填充。
字符串左对齐,使用 fillchar 填充 width 的剩余部分。 # ljust(width[,fillchar]):
# 字符串左对齐,使用 fillchar 填充 width 的剩余部分。
strs = 'Hello'
print(strs.ljust(20,'-'))
# Hello--------------- # fillchar 默认为空
print(strs.ljust(20))
# Hello 4.lower( ):注:使用了 lower 函数后,原来的字符串不变。
将字符串所有能够区分大小写的字符都转换为小写字符。 # lower( ):
# 将字符串所有能够区分大小写的字符都转换为小写字符。
strs = 'Hello 123'
print(strs.lower())
# hello 123 print(type(strs.lower()))
# <class 'str'> # 原来的字符串没有发生改变
print(strs)
# Hello 123 # 使用字符串接收 lower 函数的返回值
strs = strs.lower()
print(strs)
# hello 123 5.lstrip(str):
将字符串最左面的 str 部分去除,输出剩余的部分(str 默认为空格)。 #lstrip( ):
# 将字符串左面的空格部分去除,输出剩余的部分。
strs = ' hello'
print(strs.lstrip())
# hello
print(strs)
# hello # 使用 lstrip('过滤的参数') 函数,将最左面的 a 过滤掉
strs = 'abcd'
print(strs.lstrip('a'))
# bcd 6.maketrans(参数1,参数2):调用后,使用字符串.translate函数对字符串进行替换。
创建字符映射的转换表。
  参数 1 是需要转换的字符
  参数 2 是转换的目标 # maketrans(参数1,参数2):
# 创建字符映射的转换表。
#   参数 1 是需要转换的字符
#   参数 2 是转换的目标
# 将 abcd 使用 1234 替换
keys = 'abcd'
values = '1234'
tran = str.maketrans(keys,values)
print(type(tran)) #<class 'dict'>
# 使用 字符串.translate(接收了 maketrans 函数的对象)
strs = 'abcdef'
print(strs.translate(tran))
# 1234ef 7.max(str):
返回 str 中最大的字母,小写字母的 Unicode 编码比大写字母的 Unicode 编码大。 # max(str):
# 返回 str 中最大的字母
strs = 'abcABC'
print(max(strs))
# c
strs = 'ABCDE'
print(max(strs))
# E 8.min(str):
返回 str 中最小的字母,大写字母的 Unicode 编码比小写字母的 Unicode 编码小。 # min(str):
# 返回 str 中最小的字母
strs = 'abcABC'
print(min(strs))
# A
strs = 'ABCDE'
print(min(strs))
# A 9.replace(old,new[,num]):
将旧字符串替换为新的字符串,num 为最多替换的次数。(默认为全部替换) # replace(old,new[,num]):
# 将旧字符串替换为新的字符串,num 为替换的次数。
strs = 'abc abc abc abc'
print(strs.replace('abc','ABC'))
# ABC ABC ABC ABC # 替换 3 次
print(strs.replace('abc','ABC',3))
# ABC ABC ABC abc 10.rfind(str,start = 0,end = len(string)):
从字符串的最右面查找 str # rfind(str,start = 0,end = len(string)):
# 从字符串的最右面查找 str,不包含end
strs = 'happy happy' # h 的索引位置分别为 0,6
print(strs.rfind('h'))
# 6
# y 的索引位置分别为 4,10
# 在 索引位置 2 到 11 之间进行查找
print(strs.rfind('y',2,11))
# 10 11.rindex(str,start = 0,end = len(string)):
从字符串右面开始寻找 str ,返回索引值、找不到则报错。 # rindex(str,start = 0,end = len(string)):
# 从字符串右面开始寻找 str ,返回索引值
strs = 'happy happy' # a 的索引位置为 1,7
print(strs.rindex('a'))
# 7 12.rjust(width[,fillchar]):
返回一个以字符串右对齐,使用 fillchar 填充左面空余的部分的字符串 # rjust(width[,fillchar]):
# 返回一个以字符串右对齐,使用 fillchar 填充左面空余的部分的字符串
strs = 'hello'
print(strs.rjust(20))
# hello
print(strs.rjust(20,'*'))
# ***************hello 13.rstrip(str):
删除字符串最右面的 str 字符,str默认为空格 注:遇到不是 str 字符才停止删除 # rstrip(str):
# 删除字符串最右面的 str 字符,str默认为空格
strs = 'hello '
print(strs.rstrip())
# hello strs = 'hello aaaaa'
print(strs.rstrip('a'))
# hello 14.split(str,num):
对字符串使用 str 进行分割,如果 num有指定值,则分割 num次(默认为全部分割) # split(str=" ",num=string.count(str)):
# 对字符串使用 str 进行分割,如果 num有指定值,则分割 num次(默认为全部分割)
strs = 'hahahah'
print(strs.split('a'))
# ['h', 'h', 'h', 'h'] # 对字符串进行切割两次
print(strs.split('a',2))
# ['h', 'h', 'hah'] 15.splitlines(is_keep):
按照 回车\r 、换行\n 对字符串进行分割。   is_keep :当 is_keep 为 True 时,返回值保留换行符。        当 is_keep 为 False 时,返回值不包留换行符。 # splitlines(is_keep):
#
# 按照 回车\r 、换行\n 对字符串进行分割。
#   is_keep :当 is_keep 为 True 时,返回值保留换行符。
#        当 is_keep 为 False 时,返回值不包留换行符。
strs = "a\r\nb\nc"
# True则保留换行符和回车,False则不保存
print(strs.splitlines(True))
# ['a\r\n', 'b\n', 'c']
print(strs.splitlines())
# ['a', 'b', 'c'] 16.startswith(str,start = 0,end = len(string)):
查看在字符串的 start 到 end-1 的区间中,是否以 str 开头。 # startswith(str,start = 0,end = len(string)):
# 查看在字符串的 start 到 end-1 的区间中,是否以 str 开头。
strs = 'hello , hey , world'
print(strs.startswith('hello'))
# True
print(strs.startswith('hey',8,13))
# True
print(strs[8:13])
# hey , 17.strip(str):
返回在最左端和最右端都删除 str 的字符串。 注:遇到其他字符则停止。 # strip(str):
# 返回在最左端和最右端都删除 str 的字符串。
# 注:遇到其他字符则停止,只要是 str 进行删除、不限次数。
strs = 'ABCDEABCD'
print(strs.strip('A'))
# BCDEABCD
# 右端字符因为遇到了D,所以停止了。 strs = 'ABCDEABCDAAA'
print(strs.strip('A'))
# BCDEABCD strs = 'ABCDEABCD'
# 如果最左和最右两端都没有 str ,则不进行删除
print(strs.strip('E'))
# ABCDEABCD 18.swapcase( ):
将能够区分大小写的字符,大小写互换。 # swapcase( ):
# 将能够区分大小写的字符,大小写互换。
strs = 'ABCDabcdefg'
print(strs.swapcase())
# abcdABCDEFG
19.title( ):
将字符串变为每一个单词都是大写字母开头,其余字母为小写或数字。 # title( ):
# 将字符串变为每一个单词都是大写字母开头,其余字母为小写或数字。
strs = 'hello world abc123'
print(strs.title())
# Hello World Abc123 20.translate(字典 或 接收了字符串.maketrans(被替换元素,替换元素)的对象):
将字符串按照参数进行转换 # translate(字典 或 接收了字符串.maketrans(被替换元素,替换元素)的对象):
# 将字符串按照参数进行转换
keys = 'abcd'
values = '1234'
tran = str.maketrans(keys,values)
print(type(tran)) #<class 'dict'>
# 使用 字符串.translate(接收了 maketrans 函数的对象)
strs = 'abcdef'
print(strs.translate(tran))
# 1234ef 21.upper( ):
将所有能够区分大小写的字符都转换为大写 # upper():
# 将所有能够区分大小写的字符都转换为大写
strs = 'hello World'
print(strs.upper())
# HELLO WORLD 22.zfill(width):
返回长度为 width 的字符串,在左端填充 0 # zfill(width):
# 返回长度为 width 的字符串,在左端填充 0
strs = 'hello'
print(strs.zfill(10))
# 00000hello 23.isdecimal( ):
字符串是否只包含十进制数,其余进制都返回False。 # isdecimal( ):
# 字符串是否只包含十进制数
# 二进制
strs_bin = '0b11'
print(strs_bin.isdecimal())
# False
# 八进制
strs_oct = '0o56'
print(strs_oct.isdecimal())
# 十六进制
strs_hex = '0xa4'
print(strs_hex.isdecimal())
# False
strs_int = '123'
print(strs_int.isdecimal())
# True

Python列表:在 [ ] 中括号 中添加元素 或者 通过 list 转换其他类型。

列表(个人总结):

  1.列表是可变类型,即可以使用列表内置方法对列表进行增删查改排序操作

    常用的增删查改排序方法:

        增   :append、extend、insert、+ 连接、

        删   :pop、remove、clear、del

        查   : in、not in、for循环迭代等

        改   : 列表变量[索引下标] = 元素、切片修改

        排序: sort、sorted 

  2.列表是序列对象,即列表的索引下标从 0 开始,依次递增,最后一个元素为-1,从右向左依次递减

  3.列表可以包含所有数据类型:数字、字符串、列表、元组、集合、字典

  4.列表是可迭代对象,即可以进行 for 循环(推荐使用列表推导式)

  5.列表可以进行切片操作 [start:end:step] (不包含end)

  6.列表可以查看元素出现的次数 count 和 元素的位置(索引下标) index

  7.获取列表的长度 len

注:列表还有很多其他的 用法 和 功能,以上只是常见的

访问列表元素:

通过索引下标:

# 通过索引下标获取列表元素
lst = [1,4,7,2,5,8]
print("lst 的第一个元素是",lst[0])
# lst 的第一个元素是 1
print("lst 的第四个元素是",lst[3])
# lst 的第四个元素是 2 通过切片进行获取: # 切片 [start(默认为 0 ),end(一直到 end-1 位置),step(默认为 1 )]
# 默认的都可以省略不写
# 列表翻转
lst = [1,4,7,2,5,8] # 8 的索引位置是 5
print(lst[::-1])
# [8, 5, 2, 7, 4, 1] print(lst[2:5]) #不包含 5
# [7, 2, 5] print(lst[2:5:2]) #不包含 5
# [7, 5]

Python更新列表:

使用索引下标进行更新:

# 修改列表的第 6 个元素为 d
lst = ['a','b','c',1,2,3]
lst[5] = 'd'
print(lst)
# ['a', 'b', 'c', 1, 2, 'd'] 使用切片对列表进行更新: # 修改列表的第2个元素到最后为 hey
lst = ['a','b','c',1,2]
lst[2:] = 'hey'
print(lst)
# ['a', 'b', 'h', 'e', 'y'] # 修改列表的第3个元素到第8个元素为 hello
lst = [1,2,3,'a','b','c','d','e',4,5,6]
lst[3:8] = 'hello'
print(lst)
# [1, 2, 3, 'h', 'e', 'l', 'l', 'o', 4, 5, 6] 使用 append 方法增加元素: # 使用 append 方法对列表进行更新
lst = ['a','b','c',1,2,3]
lst.append('d')
print(lst)
# ['a', 'b', 'c', 1, 2, 3, 'd']

Python删除列表元素:

pop( ):
删除最后一个元素,返回该元素的值 # 使用 pop 方法删除列表中的元素
lst = ['a','b','c',1,2,3]
print(lst.pop())
# 3 ,pop方法删除最后一个元素并返回它的值
print(lst)
# ['a', 'b', 'c', 1, 2] remove(str):
在列表中删除 str 元素,无返回值
注:当列表中不存在 str 元素时,则会报错 lst = ['A','B','C','D']
print(lst.remove('C'))
# None
print(lst)
# ['A', 'B', 'D']
remove(str):
# 将 str 从列表中删除一次
# 当列表中存在多个 str 元素时,只删除一次
lst = ['a','b','c','d','a']
lst.remove('a')
print(lst)
# ['b', 'c', 'd', 'a'] del 元素: 删除列表中的元素 或 整个列表 # 使用 del 方法删除列表的第 3 个元素
lst = ['A','B','C','D']
del lst[2]
print(lst)
# ['A', 'B', 'D'] # 删除整个 lst 列表
del lst

Python列表脚本操作符:

len(列表名):
查看列表长度 列表对象 1 + 列表对象 2 :
将两个列表进行组合,有时可用于赋值 lst = [1,2,3,4]
lst_two = [7,8,9]
print(lst + lst_two)
# [1, 2, 3, 4, 7, 8, 9] 成员运算符 in 、not in:
判断左端元素是否在右端列表中 lst = ['a','b','c']
print('a' in lst)
# True 将列表用作可迭代对象
lst = [1,2,3,'a','b','c']
for i in lst:
print(i,end = " ")
# 1 2 3 a b c
# 注:此时的 i 不是数字,而是列表中的元素,不要用于索引下标

Python列表函数和方法:

函数:

len(列表名):

返回列表长度

# len(列表名):
# 返回列表长度
lst = [1,2,3,'a','b','c']
print("lst 列表的长度为 %d"%(len(lst)))
# lst 列表的长度为 6
max(列表名): 返回列表元素的最大值 注:列表内的元素一定要是同一类型,都是字母 或 数字 # max(列表名):
# 返回列表元素的最大值
# 注:列表内的元素一定要是同一类型,都是字母 或 数字
lst = [8,4,5,6,9]
print(max(lst))
# 9
lst = ['a','b','c','A','B','C']
print(max(lst))
# c min(列表名): 返回列表元素的最小值 注:列表内的元素一定要是同一类型,都是字母 或 数字 # min(列表名):
# 返回列表元素的最小值
# 注:列表内的元素一定要是同一类型,都是字母 或 数字
lst = [8,4,5,6,9]
print(min(lst))
# 4
lst = ['a','b','c','A','B','C']
print(min(lst))
# A 使用 list 将元组转换为列表对象(通常用来修改元组的值): # 使用 list 将元组转换为列表对象(通常用来修改元组的值)
tup = (1,2,3,'a','b')
tuple_lst = list(tup)
print(tuple_lst)
# [1, 2, 3, 'a', 'b']
方法: append(对象): 在列表末尾添加该对象 # append(对象):
# 在列表末尾添加该对象
lst = ['A','B','C']
lst.append('D')
print(lst)
# ['A', 'B', 'C', 'D'] # 如果添加元素为列表,则将列表当做一个元素进行添加
lst = ['A','B','C']
lst_two = ['a','b','c']
lst.append(lst_two)
print(lst)
# ['A', 'B', 'C', ['a', 'b', 'c']] count(str): 返回列表中 str 出现的次数 # count(str):
# 返回列表中 str 出现的次数
lst = [1,2,1,2,1,3]
print(lst.count(1))
# 3
# 如果不存在该元素,则返回 0
print(lst.count(4))
# 0 extend(序列对象): 在列表末尾添加所有序列对象中的元素,返回值为空。多用来扩展原来的列表 # extend(序列对象):
# 在列表末尾添加所有序列对象中的元素,多用来扩展原来的列表
lst = [1,2,3]
lst_two = [7,8,9]
lst.extend(lst_two)
print(lst)
# [1, 2, 3, 7, 8, 9] index(str): 返回 str 在列表中第一次出现的索引位置,str 不在列表中则报错 # index(str):
# 返回 str 在列表中第一次出现的索引位置
lst = ['a','b','c','d','e','c']
print(lst.index('c'))
# 2
insert(index,对象): 在列表的 index 位置添加对象,原 index 位置及后面位置的所有元素都向后移 # insert(index,对象):
# 在列表的 index 位置添加对象
lst = ['a','b','c','d']
lst.insert(2,'q')
print(lst)
# ['a', 'b', 'q', 'c', 'd']
pop(index = -1): 默认删除列表的最后一个元素,并返回值 当 index 为 2 时,删除第 3 个元素 # pop(index):
# 默认删除列表的最后一个元素,并返回值
lst = ['a','b','c','d']
lst.pop()
print(lst)
# ['a', 'b', 'c'] lst = ['a','b','c','d']
# 删除第二个元素
lst.pop(2)
print(lst)
# ['a', 'b', 'd'] remove(str): 将 str 从列表中删除,注:如果列表中有多个 str 元素,只删除一次 str # remove(str):
# 将 str 从列表中删除一次
lst = ['a','b','c','d','a']
lst.remove('a')
print(lst)
# ['b', 'c', 'd', 'a'] lst = ['a','b','c','d']
lst.remove('b')
print(lst)
# ['a', 'c', 'd'] reverse( ): 翻转列表的元素 # reverse( ):
# 翻转列表的元素
lst = [1,7,3,2,5,6]
lst.reverse()
print(lst)
# [6, 5, 2, 3, 7, 1]
sort(key = None,reverse = False): 对列表进行排序,无返回值 # sort(key = None,reverse = False):
# key 用来接收函数,对排序的数据进行修改
# 对列表进行排序 lst = [1,4,5,2,7,6]
lst.sort()
print(lst)
# [1, 2, 4, 5, 6, 7] lst = [1,4,5,2,7,6]
# 先排序后翻转
lst.sort(reverse = True)
print(lst)
# [7, 6, 5, 4, 2, 1] lst = [5,4,1,2,7,6]
# 不满足 key 的放前面,满足 key 的放后面
lst.sort(key = lambda x : x % 2 == 0)
print(lst)
# [1, 5, 7, 4, 2, 6] clear( ): 清空列表 # clear( ):
# 清空列表,无返回值
lst = ['a','c','e']
lst.clear()
print(lst)
# []
copy( ): 复制一份列表元素 # copy( ):
# 复制列表
lst = ['a','b','c']
print(lst.copy())
# ['a', 'b', 'c']
print(lst)
# ['a', 'b', 'c']

Python元组:元组与字典类似,不同之处在于元组的值不能够修改。

  使用 ( ) 或 tuple 强制转换 可以得到元祖

创建空元组:

# 创建空元祖
tuple_1 = ()
print(type(tuple_1))
# <class 'tuple'>
print(tuple_1)
# () 创建只包含一个元素的元组: 注:一定要使用,逗号,否则会被当做是一个常量值 # 创建只包含一个元素的元组
tuple_2 = (1,)
print(type(tuple_1))
# <class 'tuple'>
print(tuple_2)
# (1,) 创建包含有多个元素的元组: # 创建包含多个元素的元组
tuple_3 = (4,5)
print(type(tuple_3))
# <class 'tuple'>
print(tuple_3)
# (4, 5) # 使用 tuple 对列表进行强制转换
lst = [1,2,3,4,5]
tuple_4 = tuple(lst)
print(type(tuple_4))
# <class 'tuple'>
print(tuple_4)
# (1, 2, 3, 4, 5) 不使用括号,创建包含多个元素的元组: # 创建包含多个元素的元组
tuple_4 = 7,8,9
print(tuple_4)
# (7, 8, 9)
print(type(tuple_4))
# <class 'tuple'>

Python访问元组:

使用索引下标进行访问元组:

# 通过索引下标进行访问
tuple_1 = ('a','b','c','d','e','f','g')
# 输出元组中的第一个值
print(tuple_1[0])
# a # 输出元组中的第六个值
print(tuple_1[5])
# f # 输出元组的最后一个元素
print(tuple_1[-1])
# g 通过切片访问元组: # 使用切片对元组进行输出 [start:end:step] 注:不包含 end 位置
tuple_1 = ('a','b','c',4,5,6,7)
# 输出所有元组的元素
print(tuple_1[::])
# ('a', 'b', 'c', 4, 5, 6, 7) # 输出元组的第三个元素到第五个元素
print(tuple_1[2:5]) #不包含 end
# ('c', 4, 5) # 步长可以进行修改
print(tuple_1[2:5:2])
# ('c', 5) # 将元组倒序输出
print(tuple_1[::-1])
# (7, 6, 5, 4, 'c', 'b', 'a')

Python访问元组:

使用索引下标进行访问元组:

# 通过索引下标进行访问
tuple_1 = ('a','b','c','d','e','f','g')
# 输出元组中的第一个值
print(tuple_1[0])
# a # 输出元组中的第六个值
print(tuple_1[5])
# f # 输出元组的最后一个元素
print(tuple_1[-1])
# g 通过切片访问元组: # 使用切片对元组进行输出 [start:end:step] 注:不包含 end 位置
tuple_1 = ('a','b','c',4,5,6,7)
# 输出所有元组的元素
print(tuple_1[::])
# ('a', 'b', 'c', 4, 5, 6, 7) # 输出元组的第三个元素到第五个元素
print(tuple_1[2:5]) #不包含 end
# ('c', 4, 5) # 步长可以进行修改
print(tuple_1[2:5:2])
# ('c', 5) # 将元组倒序输出
print(tuple_1[::-1])
# (7, 6, 5, 4, 'c', 'b', 'a') 注:
元组没有 reverse 方法

修改元组的元素

# 将元组中的 'c' 改为 'd'
tuple_1 = ('a','b','c',4,5,6,7)
# c 的索引位置是 2
# 修改元组的值,可先将元组转换为列表类型,然后再转变为元组类型
lst = list(tuple_1)
lst[2] = 'd' #进行修改
tuple_1 = tuple(lst) #重新转换为元组类型
print(tuple_1)
# ('a', 'b', 'd', 4, 5, 6, 7)

删除元组中的某一个元素,可以使用del 进行删除,也可以利用切片进行相加

# 方法1
# 将元组中的 'b' 删除
tuple_1 = ('a','b','c',4,5,6,7)
# b 的索引位置是 1
lst = list(tuple_1)
del lst[1]
tuple_1 = tuple(lst) #重新转换为元组类型
print(tuple_1)
# ('a', 'c', 4, 5, 6, 7) # 方法2
# 将元组中的 'b' 删除
tuple_1 = ('a','b','c',4,5,6,7)
# b 的索引位置是 1
tuple_1 = tuple_1[:1] + tuple_1[2:]
print(tuple_1)
# ('a', 'c', 4, 5, 6, 7)

del 语句 删除元组:

# 删除元组
tuple_1 = ('a','b','c')
del tuple_1

Python元组运算符:

len(元组名):
返回元组对象的长度 tuple_1 = (1,4,5,2,3,6)
print(len(tuple_1))
# 6 + 连接: tuple_1 = (1,4,5)
tupel_2 = (3,5,4)
print(tuple_1 + tupel_2)
# (1, 4, 5, 3, 5, 4) * 重复: tuple_1 = (1,4,5)
num = 3
print(num * tuple_1)
# (1, 4, 5, 1, 4, 5, 1, 4, 5) 成员运算符 in ,not in: tuple_1 = (1,4,5,2,3,6)
print(4 in tuple_1)
# True print(8 in tuple_1)
# False print(4 not in tuple_1)
# False print(8 not in tuple_1)
# True 将元组作为可迭代对象: # 将元组作为可迭代对象
tuple_1 = ('a','b','c')
for i in tuple_1:
print(i , end = " ")
# a b c

Python元组索引、截取:

索引下标:
tuple_1 = ('a','b','c','d','e','f','g','h')
print(tuple_1[0])
# a
print(tuple_1[3])
# d
print(tuple_1[7])
# h # 当索引下标为负数时,-1表示最右端元素,从右向左依次递减
print(tuple_1[-1])
# h
print(tuple_1[-4])
# e 切片操作:
# 使用切片进行截取列表元素 tuple_1 = (1,2,3,4,5,6,7,8,9,10)
print(tuple_1[::])
# (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
print(tuple_1[2:8])
# (3, 4, 5, 6, 7, 8)
print(tuple_1[2:8:3])
# (3, 6) , 不包含end
print(tuple_1[2::-1])
# (3, 2, 1)
print(tuple_1[8:1:-1])
# (9, 8, 7, 6, 5, 4, 3)
print(tuple_1[8:1:-2])
# (9, 7, 5, 3) print(tuple_1[-1:-5:-1])
# (10, 9, 8, 7)

Python元组内置函数:

len(元组名):
返回元组长度 tuple_1 = (1,2,3,'a','b','c')
print("tuple_1 元组的长度为 %d"%(len(tuple_1)))
# tuple_1 元组的长度为 6 max(元组名):
返回元组元素的最大值
注:元组内的元素一定要是同一类型,都是字母 或 数字 tuple_1 = (8,4,5,6,9)
print(max(tuple_1))
# 9
tuple_1 = ('a','b','c','A','B','C')
print(max(tuple_1))
# c min(元组名):
返回元组元素的最小值
注:元组内的元素一定要是同一类型,都是字母 或 数字 tuple_1 = (8,4,5,6,9)
print(min(tuple_1))
# 4
tuple_1 = ('a','b','c','A','B','C')
print(min(tuple_1))
# A 使用 tuple 将列表转换为元组对象
# 使用 tuple 将列表转换为元组对象
tuple_1 = (1,2,3,'a','b')
lst = list(tuple_1)
print(lst)
# [1, 2, 3, 'a', 'b']

Python字典:{键:值},多个键值对使用 , 进行分隔。

创建空字典:

dic = {}
print(type(dic))
# <class 'dict'>
print(dic)
# {} 创建只有一个元素的字典: dic = {'a':123}
print(dic)
# {'a': 123} 创建包含多个元素的字典: dic = {'a':123,888:'n',(4,5):[7,8]}
print(dic)
# {'a': 123, 888: 'n', (4, 5): [7, 8]}
# 键一定是不可变类型 使用 dict 转化为字典: dic = dict(zip(['a','b','c'],[4,5,6]))
print(dic)
# {'a': 4, 'b': 5, 'c': 6}

Python访问字典中的值:

# 使用字典 ['键'] 获取字典中的元素
dic = {'a':123,'b':456,'c':789}
print(dic['a'])
# 123 修改字典元素: dic = {'a': 123, 'b': 456, 'c': 789}
dic['b'] = 14
print(dic)
# {'a': 123, 'b': 14, 'c': 789} 增加字典元素: dic = {'a':123,'b':456,'c':789}
dic['d'] = 555
print(dic)
# {'a': 123, 'b': 456, 'c': 789, 'd': 555} 删除操作:
dic = {'a': 123, 'b': 14, 'c': 789}
# 删除字典元素
del dic['b']
print(dic)
# {'a': 123, 'c': 789} # 清空字典
dic = {'a': 123, 'b': 14, 'c': 789}
dic.clear()
print(dic)
# {} # 删除字典
dic = {'a': 123, 'b': 14, 'c': 789}
del dic

Python字典内置函数和方法:
注:使用了 items、values、keys 返回的是可迭代对象,可以使用 list 转化为列表。 len(字典名):
返回键的个数,即字典的长度 dic = {'a':123,'b':456,'c':789,'d':567}
print(len(dic))
# 4 str(字典名): 将字典转化成字符串 dic = {'a':123,'b':456,'c':789,'d':567}
print(str(dic))
# {'a': 123, 'b': 456, 'c': 789, 'd': 567} type(字典名):
查看字典的类型 dic = {'a':123,'b':456,'c':789,'d':567}
print(type(dic))
# <class 'dict'>

字典的内置方法
clear( ):
删除字典内所有的元素 dic = {'a':123,'b':456,'c':789,'d':567}
dic.clear()
print(dic)
# {} copy( ):
浅拷贝一个字典 dic = {'a':123,'b':456,'c':789,'d':567}
dic_two = dic.copy()
print(dic)
# {'a': 123, 'b': 456, 'c': 789, 'd': 567}
print(dic_two)
# {'a': 123, 'b': 456, 'c': 789, 'd': 567} fromkeys(seq[,value]):
创建一个新字典,seq作为键,value为字典所有键的初始值(默认为None) # fromkeys(seq[,value]):
# 创建一个新字典,seq作为键,value为字典所有键的初始值(默认为None)
dic = dict.fromkeys('abcd')
# 默认为 None
print(dic)
# {'a': None, 'b': None, 'c': None, 'd': None} dic = dict.fromkeys('abc',1)
print(dic)
# {'a': 1, 'b': 1, 'c': 1} get(key,default = None):
返回指定的键的值,如果键不存在,则返会 default 的值 dic = {'a':1,'b':2,'c':3,'d':4}
print(dic.get('b'))
# 2
print(dic.get('e',5))
# 5 成员运算符 in、not in:
查看 键 是否在字典中: dic = {'a':1,'b':2,'c':3,'d':4}
print('a' in dic)
# True
print('a' not in dic)
# False items( ):
返回键值对的可迭代对象,使用 list 可转换为 [(键,值)] 形式 dic = {'a':1,'b':2,'c':3,'d':4}
print(dic.items())
# dict_items([('a', 1), ('b', 2), ('c', 3), ('d', 4)])
print(list(dic.items()))
# [('a', 1), ('b', 2), ('c', 3), ('d', 4)] keys( ):
返回一个迭代器,可以使用 list() 来转换为列表 dic = {'a':1,'b':2,'c':3,'d':4}
print(dic.keys())
# dict_keys(['a', 'b', 'c', 'd'])
print(list(dic.keys()))
# ['a', 'b', 'c', 'd'] setdefault(key,default = None):
如果键存在于字典中,则不修改键的值
如果键不存在于字典中,则设置为 default 值 dic = {'a':1,'b':2,'c':3,'d':4}
dic.setdefault('a',8)
print(dic)
# {'a': 1, 'b': 2, 'c': 3, 'd': 4} # 如果键不存在于字典中,则设置为 default 值
dic = {'a':1,'b':2,'c':3,'d':4}
dic.setdefault('e',5)
print(dic)
# {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5} update(字典对象):
将字典对象更新到字典中 dic = {'a':1,'b':2,'c':3,'d':4}
dic_two = {'f':6}
dic.update(dic_two)
print(dic)
# {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'f': 6} values( ):
返回一个可迭代对象,使用 list 转换为字典中 值 的列表 dic = {'a':1,'b':2,'c':3,'d':4}
print(list(dic.values())) pop(key[,default]):
删除字典中 key 的值,返回被删除的值。key 值如果不给出,则返回default的值 dic = {'a':1,'b':2,'c':3,'d':4}
print(dic.pop('a',6))
# 1 , 返回删除的值
print(dic)
# {'b': 2, 'c': 3, 'd': 4}
print(dic.pop('e','字典中没有该值'))
# 字典中没有该值 , 如果字典中不存在该键,则返回 default 的值
print(dic)
# {'b': 2, 'c': 3, 'd': 4} popitem( ):
随机返回一个键值对(通常为最后一个),并删除最后一个键值对 dic = {'a':1,'b':2,'c':3,'d':4}
print(dic.popitem())
# ('d', 4)
print(dic)
# {'a': 1, 'b': 2, 'c': 3}
print(dic.popitem())
# ('c', 3)
print(dic)
# {'a': 1, 'b': 2}

time 模块

time.time()
# 1595600800.6158447 Python函数使用一个元组装起来的 9 组数字,专门用来处理时间使用  tm_year  年:使用 4 位数字 、2020  tm_mon  月:1~12 、6月  tm_mday 日:1~31 、25号  tm_hour 小时:0~23 、16时  tm_min 分钟:0~59 、33分  tm_sec  秒:0~60 或 0~61 、61 是闰秒  tm_wday 一周的第几日:0~6 、0 是周一  tm_yday 一年的第几日:1~365 或 1~366 、366是闰年  tm_isdst 夏令时:1 为夏令时、0 非夏令时、-1 未知
获取当前时间

#使用 time.localtime(time.time())
import time
times = time.time()
print(times) # 表示自 1970 年 1月 1 日 过去了多久
# 1595601014.0598545
localtime = time.localtime(times)
print(localtime)
# time.struct_time(tm_year=2020, tm_mon=7, tm_mday=24, tm_hour=22, tm_min=30,
# tm_sec=14, tm_wday=4, tm_yday=206, tm_isdst=0)
获取格式化时间:
time.time() 获取到1970年1月1日的秒数 -> time.localtime() 转变为当前时间 -> time.asctime() 将时间格式化 #获取格式化时间
import time
times = time.time()
print(times)
# 1595601087.3453288 local_times = time.localtime(times)
print(local_times)
# time.struct_time(tm_year=2020, tm_mon=7, tm_mday=24, tm_hour=22,
# tm_min=31, tm_sec=27, tm_wday=4, tm_yday=206, tm_isdst=0) # 使用 asctime 将得到的 local_times 转化为有格式的时间
local_time_asctimes = time.asctime(local_times)
print(local_time_asctimes)
# Fri Jul 24 22:31:27 2020
格式化日期:

  %y :两位数的年份表示 (00~99)

  %Y :四位数的年份表示 (000~9999)

  %m :月份(01~12)

  %d  :月份中的某一天(0~31)

  %H :某时,24小时制(0~23)

  %I   :某时,12小时制(01~12)

  %M :某分(0~59)

  %S  :某秒(00~59)

  %a  :周几的英文简称

  %A  :周几的完整英文名称

  %b  :月份的英文简称

  %B  :月份的完整英文名称

  %c   :本地相应的日期表示和时间表示

  %j    :年内的某一天(001~366)

  %p   :本地 A.M. 或 P.M.的等价符

  %U  :一年中的星期数(00~53)注:星期天为星期的开始

  %w  :星期(0~6)注:星期天为星期的开始

  %W :一年中的星期数(00~53)注:星期一为星期的开始

  %x   :本地相应的日期表示

  %X  :本地相应的时间表示

  %Z  : 当前时区的名称

  %% :输出一个%

time.strftime(format[,t]) 参数为日期格式

# 格式化日期
# time.strftime(format[,t]) 参数为日期格式
import time
times = time.time()
local_time = time.localtime(times) # Y 年 - m 月 - d 日 H 时 - M 分 - S 秒
print(time.strftime("%Y-%m-%d %H:%M:%S",local_time))
# 2020-07-24 22:33:43 # Y 年 - b 月份英文简称 - d 日期 - H 时 - M 分 - S 秒 - a 周几的英文简称
print(time.strftime("%Y %b %d %H:%M:%S %a",local_time))
# 2020 Jul 24 22:33:43 Fri

获取某月的日历:

calendar.month(year,month):

获取 year 年 month 月的日历

import calendar

cal = calendar.month(2020,2)
print("以下为2020年2月的日历")
print(cal)
# February 2020
# Mo Tu We Th Fr Sa Su
# 1 2
# 3 4 5 6 7 8 9
# 10 11 12 13 14 15 16
# 17 18 19 20 21 22 23
# 24 25 26 27 28 29

Python Time模块:

altzone:
注:我们在格林威治的东部,返回负值。对启动夏令时的地区才能使用
返回格林威治西部的夏令时地区的偏移秒数,如果是在东部(西欧),则返回负值 import time
print(time.altzone)
# -32400 time( ):
返回当前时间的时间戳 import time
times = time.time()
print(times)
# 1595601470.093444 asctime(时间元组):时间元组:如使用了 gmtime 或 localtime 函数的对象
接受时间元组并返回可读形式,时间元组可以是使用了 time.localtime(time.time()) 的对象 import time
times = time.time()
print(times)
# 1595601496.8390365 local_time = time.localtime(times)
print(local_time)
# time.struct_time(tm_year=2020, tm_mon=7, tm_mday=24,
# tm_hour=22, tm_min=38, tm_sec=16, tm_wday=4, tm_yday=206, tm_isdst=0) # 使用 asctime 转变为可读形式
print(time.asctime(local_time))
# Fri Jul 24 22:38:16 2020 perf_counter( ):
返回系统的运行时间 import time
# 使用 perf_counter()函数查看系统运行时间
print(time.perf_counter())
# 2.0289763 process_time( ):
查看进程运行时间 import time
# 使用 process_time() 查看进程运行时间
print(time.process_time())
# 0.0625 gmtime(secs):secs 时间戳:从1970年1月1日到现在的秒数
查看格林威治的时间元组 # 使用 gmtime(时间戳) 查看格林威治的时间元组
import time
times = time.time()
# 查看格林威治的当前时间元组
print(time.gmtime(times))
# time.struct_time(tm_year=2020, tm_mon=2, tm_mday=10, tm_hour=5,
# tm_min=18, tm_sec=7, tm_wday=0, tm_yday=41, tm_isdst=0) localtime(secs):secs 时间戳:从1970年1月1日到现在的秒数
返回 secs 时间戳下的时间元组 import time
times = time.time()
print(times)
# 1581312227.3952267 , 时间戳
local_time = time.localtime()
print(local_time)
# time.struct_time(tm_year=2020, tm_mon=2, tm_mday=10, tm_hour=13,
# tm_min=23, tm_sec=47, tm_wday=0, tm_yday=41, tm_isdst=0) mktime(使用了 gmtime 或 localtime 函数的对象):
返回时间戳 import time
times = time.time()
print(times)
# 1581312492.6350465
local_time = time.localtime()
print(time.mktime(local_time))
# 1581312492.0 ctime( ):
返回可读形式的当前时间 # ctime()
# 返回可读形式的当前时间
import time
print(time.ctime())
# Mon Feb 10 13:32:22 2020
sleep(seconds):
使程序延迟 seconds 秒运行 import time
print(time.ctime())
# Mon Feb 10 13:34:38 2020
time.sleep(5)
print(time.ctime())
# Mon Feb 10 13:34:43 2020 strftime(format,时间元组):format表示时间显示格式,如 %Y-%m-%d %H-%M-%S 将时间元组转换为 format 形式 import time
times = time.time()
local_time = time.localtime()
time_strftime = time.strftime("%Y-%m-%d %H-%M-%S",local_time)
print(time_strftime)
# 2020-02-10 13-42-09 strptime(str,format):
按照 format 格式将 str 解析为一个时间元组,如果 str 不是 format 格式,则会报错 import time
str = '2020-02-10 13-42-09'
str_time = time.strptime(str,"%Y-%m-%d %H-%M-%S")
print(str_time)
# time.struct_time(tm_year=2020, tm_mon=2, tm_mday=10, tm_hour=13,
# tm_min=42, tm_sec=9, tm_wday=0, tm_yday=41, tm_isdst=-1) 属性: timezone: 当前地区距离格林威治的偏移秒数 import time
print(time.timezone)
# -28800 tzname:
属性time.tzname包含一对字符串,分别是带夏令时的本地时区名称,和不带的。 # 使用 time.tzname 查看 带夏令时的本地时区名称 与 不带夏令时的本地时区名称
import time
print(time.tzname)
# ('中国标准时间', '中国夏令时')

Python日历模块 calendar:

0:星期一是第一天

6:星期日是最后一天

注:形参 w,I,c 可以不写,正常使用,使用默认形参即可

calendar(year,w=2,I=1,c=6):
返回一个多行字符格式的 year 年年历,3个月一行,间隔距离为 c。每日宽度间隔为 w 个字符 # calendar(year,w = 2,I = 1,c = 6)
import calendar
# calendar(年份,天与天之间的间隔,周与周之间的间隔,月与月之间的间隔)
print(calendar.calendar(2020,2,1,6))
# 2020
#
# January February March
# Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
# 1 2 3 4 5 1 2 1
# 6 7 8 9 10 11 12 3 4 5 6 7 8 9 2 3 4 5 6 7 8
# 13 14 15 16 17 18 19 10 11 12 13 14 15 16 9 10 11 12 13 14 15
# 20 21 22 23 24 25 26 17 18 19 20 21 22 23 16 17 18 19 20 21 22
# 27 28 29 30 31 24 25 26 27 28 29 23 24 25 26 27 28 29
# 30 31
#
# April May June
# Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
# 1 2 3 4 5 1 2 3 1 2 3 4 5 6 7
# 6 7 8 9 10 11 12 4 5 6 7 8 9 10 8 9 10 11 12 13 14
# 13 14 15 16 17 18 19 11 12 13 14 15 16 17 15 16 17 18 19 20 21
# 20 21 22 23 24 25 26 18 19 20 21 22 23 24 22 23 24 25 26 27 28
# 27 28 29 30 25 26 27 28 29 30 31 29 30
#
# July August September
# Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
# 1 2 3 4 5 1 2 1 2 3 4 5 6
# 6 7 8 9 10 11 12 3 4 5 6 7 8 9 7 8 9 10 11 12 13
# 13 14 15 16 17 18 19 10 11 12 13 14 15 16 14 15 16 17 18 19 20
# 20 21 22 23 24 25 26 17 18 19 20 21 22 23 21 22 23 24 25 26 27
# 27 28 29 30 31 24 25 26 27 28 29 30 28 29 30
# 31
#
# October November December
# Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
# 1 2 3 4 1 1 2 3 4 5 6
# 5 6 7 8 9 10 11 2 3 4 5 6 7 8 7 8 9 10 11 12 13
# 12 13 14 15 16 17 18 9 10 11 12 13 14 15 14 15 16 17 18 19 20
# 19 20 21 22 23 24 25 16 17 18 19 20 21 22 21 22 23 24 25 26 27
# 26 27 28 29 30 31 23 24 25 26 27 28 29 28 29 30 31
# 30 firstweekday( ):
返回当前每周起始日期的设置,默认返回 0 、周一为 0 import calendar
print(calendar.firstweekday())
# 0 isleap(year):
如果是闰年则返回 True,否则返回 False import calendar
print(calendar.isleap(2020))
# True leapdays(year1,year2):
返回 year1 到 year2 之间的闰年数量 import calendar
print(calendar.leapdays(2001,2100))
# 24 month(year,month,w = 2,I = 1):
返回 year 年 month 月日历,两行标题,一周一行。
注:每天与每天的宽度间隔为 w 个字符,i 是每个星期与每个星期的间隔的空数 import calendar
print(calendar.month(2020,3,2,1))
# March 2020
# Mo Tu We Th Fr Sa Su
# 1
# 2 3 4 5 6 7 8
# 9 10 11 12 13 14 15
# 16 17 18 19 20 21 22
# 23 24 25 26 27 28 29
# 30 31 monthcalendar(year,month):
以列表形式返回,每一周为内嵌列表,没有日子则为 0 import calendar
print(calendar.monthcalendar(2020,4))
# [[0, 0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11, 12],
# [13, 14, 15, 16, 17, 18, 19], [20, 21, 22, 23, 24, 25, 26],
# [27, 28, 29, 30, 0, 0, 0]] monthrange(year,month):
返回(这个月的第一天是星期几,这个月有多少天) import calendar
print(calendar.monthrange(2020,2))
# (5, 29) prcal(year,w = 2,I = 1,c = 6):
输出 year 年的日历 import calendar
calendar.prcal(2020)
# print(calendar.prcal(2020,2,1,6))
# 2020
#
# January February March
# Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
# 1 2 3 4 5 1 2 1
# 6 7 8 9 10 11 12 3 4 5 6 7 8 9 2 3 4 5 6 7 8
# 13 14 15 16 17 18 19 10 11 12 13 14 15 16 9 10 11 12 13 14 15
# 20 21 22 23 24 25 26 17 18 19 20 21 22 23 16 17 18 19 20 21 22
# 27 28 29 30 31 24 25 26 27 28 29 23 24 25 26 27 28 29
# 30 31
#
# April May June
# Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
# 1 2 3 4 5 1 2 3 1 2 3 4 5 6 7
# 6 7 8 9 10 11 12 4 5 6 7 8 9 10 8 9 10 11 12 13 14
# 13 14 15 16 17 18 19 11 12 13 14 15 16 17 15 16 17 18 19 20 21
# 20 21 22 23 24 25 26 18 19 20 21 22 23 24 22 23 24 25 26 27 28
# 27 28 29 30 25 26 27 28 29 30 31 29 30
#
# July August September
# Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
# 1 2 3 4 5 1 2 1 2 3 4 5 6
# 6 7 8 9 10 11 12 3 4 5 6 7 8 9 7 8 9 10 11 12 13
# 13 14 15 16 17 18 19 10 11 12 13 14 15 16 14 15 16 17 18 19 20
# 20 21 22 23 24 25 26 17 18 19 20 21 22 23 21 22 23 24 25 26 27
# 27 28 29 30 31 24 25 26 27 28 29 30 28 29 30
# 31
#
# October November December
# Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
# 1 2 3 4 1 1 2 3 4 5 6
# 5 6 7 8 9 10 11 2 3 4 5 6 7 8 7 8 9 10 11 12 13
# 12 13 14 15 16 17 18 9 10 11 12 13 14 15 14 15 16 17 18 19 20
# 19 20 21 22 23 24 25 16 17 18 19 20 21 22 21 22 23 24 25 26 27
# 26 27 28 29 30 31 23 24 25 26 27 28 29 28 29 30 31
# 30 prmonth(year,month,w = 2,I = 1):
输出 year 年 month 月的日历 import calendar
calendar.prmonth(2020,12)
# December 2020
# Mo Tu We Th Fr Sa Su
# 1 2 3 4 5 6
# 7 8 9 10 11 12 13
# 14 15 16 17 18 19 20
# 21 22 23 24 25 26 27
# 28 29 30 31 setfirstweekday(weekday):
设置每周的起始日期,0为周一,6为周日 import calendar
calendar.setfirstweekday(2) timegm(时间元组):
返回该时刻的时间戳 import time
import calendar
print(calendar.timegm(time.localtime(time.time())))
# 1581362983 weekday(year,month,day):
查看 year 年 month 月 day 日 星期几 import calendar
print(calendar.weekday(2020,2,10))
# 0 周一

Python函数:实现某种功能的代码段

定义一个函数需要遵循的规则:

  1.使用 def 关键字 函数名和( ),括号内可以有形参

    匿名函数使用 lambda 关键字定义

  2.任何传入参数和自变量必须放在括号中

  3.函数的第一行语句可以使用字符串存放函数说明

  4.函数内容以冒号开始,函数内的代码块缩进

  5.使用了 return [表达式] 函数会返回一个值,如果不使用 return 没有返回值
def 函数名([参数,根据函数运行需要,如果函数不需要参数,可以不写]):
函数声明(可不写,如果怕忘记函数功能,最好是写) 函数语句 ... return 需要返回的值(有些函数可以不使用 return,只进行某些操作,返回为None) 定义一个 say_hello 函数,使函数能够说出 hello
def say_hello():
print("hello") 定义一个求长方形面积的函数 area,要求具有返回值且参数为 height 和 width
def area(height,width):
return height * width 定义一个函数,输出 欢迎,接收到的参数 name
def huanying(name):
print("欢迎",name) 定义一个求 1~10 的总和的函数,函数名为 one_to_ten,要求具有返回值
def one_to_ten():
sum = 0
for i in range(1,11):
sum += i
return sum

函数名可以赋值给变量,使用变量进行调用(变量此时相当于函数名)

def add(num_1,num_2):
print(num_1 + num_2) a = add
print(type(a))
# <class 'function'>
a(3,5)
# 8

函数内可以进行定义类:

def run():
class student(object):
pass

Python函数调用:

函数定义后,使用函数名(实参)进行调用,如果具有返回值,则使用变量接收。

示例:
无参数,无返回值 # 程序:定义一个 say_hello 函数,使函数能够说出 hello
def say_hello():
print("hello") say_hello()
# hello 有参数,无返回值
# 定义一个函数,输出 欢迎,接收到的参数 name
def huanying(name):
print("欢迎",name)
huanying("小明")
# 欢迎 小明 无参数,有返回值
#定义一个求 1~10 的总和的函数,函数名为 one_to_ten,要求具有返回值
def one_to_ten():
sum = 0
for i in range(1,11):
sum += i
return sum sum_1 = one_to_ten()
print(sum_1)
# 55 有参数,有返回值
# 定义一个求长方形面积的函数,要求具有返回值且参数为 height 和 width
def area(height,width):
return height * width mianji = area(5,4)
print(mianji)

Python按值传递参数和按引用传递参数:

按值传递参数:
使用一个变量的值(数字,字符串),放到实参的位置上
注:传递过去的是变量的副本,无论副本在函数中怎么变,变量的值都不变 传递常量: # 定义一个函数,输出 欢迎,接收到的参数 name
def huanying(name):
print("欢迎",name)
huanying("小明")
# 欢迎 小明 # 定义一个求长方形面积的函数,要求具有返回值且参数为 height 和 width
def area(height,width):
return height * width mianji = area(5,4)
print(mianji)
# 20 传递变量: # 定义一个函数,输出 欢迎,接收到的参数 name
def huanying(name):
print("欢迎",name)
strs_name = "小明"
huanying(strs_name)
# 欢迎 小明 # 定义一个求长方形面积的函数,要求具有返回值且参数为 height 和 width
def area(height,width):
return height * width
height = 5
width = 4
mianji = area(height,width)
print(mianji)
# 20

按引用传递:
输出 列表、元组 和 集合所有元素的总和 lst = [1,2,3,4]
tuple_1 = (4,5,7)
se = {9,6,5,8} def add(args):
'''将 args 中的元素总和输出'''
print(sum(args)) add(lst)
# 10
add(tuple_1)
# 16
add(se)
# 28 # 输出程序的注释
print(add.__doc__)
# 将 args 中的元素总和输出 使用函数,将参数引用传递输出参数中的奇数 lst = [1,2,3,4]
tuple_1 = (4,5,7) def jishu(args):
'''将 args 中的奇数输出'''
for i in range(len(args)):
if args[i] % 2 == 1:
print(args[i], end = " ")
print()
jishu(lst)
# 1 3
jishu(tuple_1)
# 5 7 # 输出程序的注释
print(jishu.__doc__)
# 将 args 中的奇数输出

如果参数发生修改

  1.原参数如果为可变类型(列表、集合、字典)则也跟着修改

  2.原参数如果为不可变类型(数字、字符串、元组)则不发生改变

示例:
列表在函数中调用方法,列表本身发生改变 def add_elem(args,string):
'''将 lst_num 添加到 args 中''' args.append(string)
# args += string
# args.extend(string)
# args.insert(len(args),string)
return args lst = [1,2,3,4] string = "ABC" print(add_elem(lst,string))
# [1, 2, 3, 4, 'ABC']
print(lst)
# [1, 2, 3, 4, 'ABC'] 在函数中,如果列表作为实参,形参发生修改时,列表值发生改变 def add_elem(args,string):
'''将 lst_num 添加到 args 中''' args[len(args)-1] = string return args lst = [1,2,3,4] string = "ABC" print(add_elem(lst,string))
# [1, 2, 3, 'ABC']
print(lst)
# [1, 2, 3, 'ABC']

Python函数参数:

注:变量没有类型,有类型的是变量指向的内存空间中的值

可变类型:列表、集合、字典

不可变类型:数字、字符串、元组

可变类型在函数中发生改变时,原变量也会跟着发生变化

列表使用 赋值,+=,append,extend,insert 方法均会使列表的值发生改变

位置参数:

实参必须以正确的顺序传入函数,调用时的数量必须与声明时一致
# 必需参数 def hello(name):
'''输出欢迎信息'''
print("hello {0}".format(name)) name = "XiaoMing"
hello(name)
# hello XiaoMing
# hello() 会报错,因为没有传入参数
关键字参数:
函数在调用时使用关键字确定传入参数的值(可以不根据参数位置)
注:关键字即为函数定义时使用的形参名
对应关键字名进行传递: def add(num_1,num_2):
'''将两个数字进行相加'''
print("num_1:",num_1)
print("num_2:",num_2)
print("num_1 + num_2",num_1 + num_2) add(num_2 = 6,num_1 = 8)
# num_1: 8
# num_2: 6
# num_1 + num_2 14

默认参数:

当调用函数时,如果没有传递参数,则会使用默认参数

       如果传递了参数,则默认参数不起作用

注:程序有时会设置好已经修改好的默认参数,调用只需要传入不是默认参数的参数即可

# 默认参数

def add(num_1,num_2 = 10):
'''将两个数字进行相加'''
print("num_1:",num_1)
print("num_2:",num_2)
print("num_1 + num_2",num_1 + num_2) # add(15)
# # num_1: 15
# # num_2: 10
# # num_1 + num_2 25 # 不传入 num_2 的值,使用 num_2 的默认参数
add(num_1 = 15)
# num_1: 15
# num_2: 10
# num_1 + num_2 25 # 传入 num_2 的值,不使用 num_2 的默认参数
add(num_2 = 6,num_1 = 8)
# num_1: 8
# num_2: 6
# num_1 + num_2 14

不定长参数:
当需要的参数不确定,又还想使用参数进行函数内的运算时,可以考虑不定长参数 不定长参数:
* 变量
  1.形参使用 *变量名: 实参通常为传入的多余变量(可以是字典变量)、 列表 或 元组 等
      如果实参使用了 *列表,*元组,则函数中接收的是列表或元组中的所有的元素值
  2.形参使用 **变量名:通常为 **字典变量 或 字典元素(键值对) 等 示例:
# 不定长参数 def print_info(*vartuple):
print(vartuple) # 调用 printinfo 函数
print_info(70, 60, 50)
# (70, 60, 50) 当不定长参数前面存在位置参数时:
传入参数的值先传递给位置参数,剩余的传递给不定长参数
注:如果没有剩余的实参,则不定长参数没有值 不使用位置参数:
可以接收所有数据类型数据,除了 a = 2 这种键值对,**字典变量等
注:如果实参中使用了 *列表,*元组,则函数接收的为列表或元组的所有元素值 def print_info(*vartuple): # print(type(vartuple)) print(vartuple) # for i in vartuple:
# print(i,end =" ")
# print(type(vartuple[5]))
#
# print(vartuple[5])# 不定长参数
# 不使用位置参数
def print_info(*vartuple): # print(type(vartuple)) print(vartuple) # for i in vartuple:
# print(i,end =" ")
# print(type(vartuple[5]))
#
# print(vartuple[5]) # 调用 printinfo 函数
print_info(70,12.3,5+9j,True,"hello",[1,2,3],(7,8,9),{'a':123})
# (70, 12.3, (5+9j), True, 'hello', [1, 2, 3], (7, 8, 9), {'a': 123})
print_info([1,2,3])
# # ([1, 2, 3],)
print_info(*[1,2,3],'a')
# (1, 2, 3, 'a')
print_info((1,2,3))
# # ((1, 2, 3),)
print_info(*(1,2,3),'a')
# (1, 2, 3, 'a')

使用位置参数:

def print_info(num,*vartuple):

    print(num)
print(vartuple) # 调用 printinfo 函数
print_info(70, 60, 50)
# 70
# (60, 50) ** 变量名:
  形参使用 **变量名:实参可以使用 a = 2、**字典对象 # ** 变量名:
#   形参使用 **变量名:实参可以使用 a = 2、**字典元素
def print_info(**attrs): print(attrs)
print(type(attrs))
# <class 'dict'>
dic = {'a':123}
print_info(**dic,b = 4,c = 6)
# {'a': 123, 'b': 4, 'c': 6} 在形参中使用 * 和 ** 参数接收: def print_info(num,*vartuple,**attrs): print(num)
print(vartuple)
print(attrs) # 调用 printinfo 函数 print_info(70, 60, 50,{'a':123},b = 456,c = 789)
# 70
# (60, 50, {'a': 123})
# {'b': 456, 'c': 789}

Python匿名函数:

使用 lambda 关键字创建匿名函数:

lambda 定义的函数只是一个表达式,而不是代码块

lambda 函数拥有自己的命名空间,不能够访问参数列表之外的 或 全局命名空间的参数

# 使 lambda 实现输出 x 的 y 次方
# 使用变量接收函数
cifang = lambda x,y:x**y
# 匿名函数的调用:使用接收到的变量进行调用
print(cifang(3,2))
# 9 # 拓展:使用变量接收函数名,然后可以使用变量进行函数调用
def print_info():
print("hello")
pr = print_info
pr()
# hello

return语句:

在函数内当遇到 return 语句时,退出函数并返回 return 后面的对象,可以是表达式 或 值

不带 return 语句的函数返回值为None,也就是没有返回值

def add_num(num_1, num_2):
# 返回 num_1 和 num_2 的和"
total = num_1 + num_2
print(total)
return total # 调用 add_num 函数 # 使用变量接收函数的返回值
total = add_num(10, 20)
print(total)
# 30
# 30

Python变量作用域
运行 Python 程序时,Python会在作用域中依次寻找该变量,直到找到为止,否则会报错(未定义) Python定义的变量并不是任意一个位置都可以进行访问的,主要根据变量的作用域。 局部作用域:比如在一个函数内部 全局作用域:一个 .py 文件中只要不是在函数内部,都是全局变量 内建作用域:
import builtins
print(dir(builtins))
Python 中只有模块(module),类(class)以及函数(def、lambda)才会引入新的作用域

Python全局变量和局部变量:

定义在函数内的为局部变量,在外部访问局部变量会出现未定义的错误

定义在函数外的变量称为全局变量,可以在整个函数范围内访问

当函数中存在与全局变量重名的变量,以函数中的局部变量为准

定义在函数中的局部变量的作用域只在函数中

# 定义全局变量 total
total = 0 def add(num1,num2):
# 定义局部变量 total
total = num1 + num2
# 输出局部变量
print(total) add(4,6)
# 10
print(total)
# 0

在函数内部,如果想要修改外部变量时,可以使用 global 关键字

global 全局变量名

在函数中使用全局变量,可以对全局变量进行修改。

注:如果只是在函数中使用了和全局变量相同的名字,则只是局部变量

# 定义全局变量 total
total = 0 def add(num1,num2):
# 使用 global 关键字声明全局变量 total
global total
total = num1 + num2
# 输出全局变量
print(total) add(4,6)
# 10
# 输出全局变量
print(total)
# 10

nonlocal 嵌套作用域中的变量(嵌套函数,外层函数与内层函数之间):

修改嵌套函数之间的变量

def func_out():
num = 5
def func_inner(): # 使用嵌套函数中的 num 值
nonlocal num num = 10
print("最内部函数中的 num 的值:",num)
func_inner()
print("嵌套函数中的 num 的值:",num)
func_out()
# 最内部函数中的 num 的值: 10
# 嵌套函数中的 num 的值: 10

Python模块:包含了所有定义的函数和变量的文件,后缀名为 .py

将某些方法存放在文件中,当某些脚本 或 交互式需要使用的时候,导入进去。

导入的文件,就称为模块。导入之后就可以使用导入的文件的函数等功能

import math
# 导入 math 库
print(math.exp(1) == math.e)
# 导入 exp() 和 e
# True import 语句: import 模块名 或 包:调用方法,使用 模块名.方法 当解释器遇到 import 语句时,如果模块在 搜索路径 中,则模块会被导入 注:搜索路径是解释器进行搜索的所有目录名的一个列表。 在一个 .py 文件中创建函数,在另一个 .py 文件中导入 func.py # 在 func 模块中定义一个 print_info 函数
def print_info():
print("我是在 func 模块内部的")
test.py # 导入 func 模块
import func
# 调用自定义的模块函数
func.print_info()
# 我是在 func 模块内部的
一个模块只会被导入一次,无论使用多少次 import 语句,都只导入一次

from 模块名 import 语句

from 模块名 import 子模块 或 函数 或 类 或 变量 

导入的不是整个模块,而是 import 后面的对象

注:在调用导入的模块函数使,不使用模块名.函数名 而是 直接使用函数名进行调用

func.py

# 在 func 模块中定义一个 print_info 函数
def print_info():
print("我是在 func 模块内部的") def get_info():
print("获取到了 func 模块的信息")
test.py # 导入 func 模块
from func import get_info
# 调用自定义的模块函数 get_info()
# 获取到了 func 模块的信息
注:没有导入 print_info 方法,使用会报错

from 模块名 import * 

使用 函数 或 变量 直接进行使用

将模块内的所有内容都导入到当前的模块中,但是不会将该模块的子模块的所有内容也导入

导入语句遵循如下规则:如果包定义文件 __init__.py 存在一个叫做 __all__ 的列表变量,
那么在使用 from package import * 的时候就把这个列表中的所有名字作为包内容导入 from func import *
# 调用自定义的模块函数
print_info()
# 我是在 func 模块内部的
get_info()
# 获取到了 func 模块的信息
注:不导入 _ 单个下划线开头的变量或方法

接着上一篇随笔,继续进行整理总结
注:以下内容基本都在以前的随笔中可以找到

Python定位模块:

导入模块时,系统会根据搜索路径进行寻找模块:

  1.在程序当前目录下寻找该模块

  2.在环境变量 PYTHONPATH 中指定的路径列表寻找

  3.在 Python 安装路径中寻找

搜索路径是一个列表,所以具有列表的方法
使用 sys 库的 path 可以查看系统路径

import sys
# 以列表方式输出系统路径,可以进行修改
print(sys.path)
增加新目录到系统路径中

sys.path.append("新目录路径")
sys.path.insert(0,"新目录路径")
添加环境变量

set PYTHONPATH=安装路径\lib;
Python 会在每次启动时,将 PYTHONPATH 中的路径加载到 sys.path中。

Python命名空间和作用域:

变量拥有匹配对象的名字,命名空间包含了变量的名称(键)和所指向的对象(值)。

Python表达式可以访问局部命名空间和全局命名空间

注:当局部变量和全局变量重名时,使用的是局部变量

每个函数和类都具有自己的命名空间,称为局部命名空间

如果需要在函数中使用全局变量,可以使用 global 关键字声明,声明后,Python会将该关键字看作是全局变量

# global 全局变量名:
# 在函数中使用全局变量,可以对全局变量进行修改。
# 注:如果只是在函数中使用了和全局变量相同的名字,则只是局部变量
# 定义全局变量 total total = 0 def add(num1,num2):
# 使用 global 关键字声明全局变量 total
global total
total = num1 + num2
# 输出全局变量
print(total) add(4,6)
# 10
# 输出全局变量
print(total)
# 10

Python dir(模块名 或 模块名.方法名):
查看 模块名 或 模块名.方法名 的所有可以调用的方法 # 导入 math 库
import math
# 查看 math 可以调用的方法
print(dir(math))
'''
['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']
''' import urllib.request
print(dir(urllib.request)) '''
['AbstractBasicAuthHandler', 'AbstractDigestAuthHandler', 'AbstractHTTPHandler', 'BaseHandler', 'CacheFTPHandler', 'ContentTooShortError', 'DataHandler', 'FTPHandler', 'FancyURLopener', 'FileHandler', 'HTTPBasicAuthHandler', 'HTTPCookieProcessor', 'HTTPDefaultErrorHandler', 'HTTPDigestAuthHandler', 'HTTPError', 'HTTPErrorProcessor', 'HTTPHandler', 'HTTPPasswordMgr', 'HTTPPasswordMgrWithDefaultRealm', 'HTTPPasswordMgrWithPriorAuth', 'HTTPRedirectHandler', 'HTTPSHandler', 'MAXFTPCACHE', 'OpenerDirector', 'ProxyBasicAuthHandler', 'ProxyDigestAuthHandler', 'ProxyHandler', 'Request', 'URLError', 'URLopener', 'UnknownHandler', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '__version__', '_cut_port_re', '_ftperrors', '_have_ssl', '_localhost', '_noheaders', '_opener', '_parse_proxy', '_proxy_bypass_macosx_sysconf', '_randombytes', '_safe_gethostbyname', '_splitattr', '_splithost', '_splitpasswd', '_splitport', '_splitquery', '_splittag', '_splittype', '_splituser', '_splitvalue', '_thishost', '_to_bytes', '_url_tempfiles', 'addclosehook', 'addinfourl', 'base64', 'bisect', 'build_opener', 'contextlib', 'email', 'ftpcache', 'ftperrors', 'ftpwrapper', 'getproxies', 'getproxies_environment', 'getproxies_registry', 'hashlib', 'http', 'install_opener', 'io', 'localhost', 'noheaders', 'os', 'parse_http_list', 'parse_keqv_list', 'pathname2url', 'posixpath', 'proxy_bypass', 'proxy_bypass_environment', 'proxy_bypass_registry', 'quote', 're', 'request_host', 'socket', 'ssl', 'string', 'sys', 'tempfile', 'thishost', 'time', 'unquote', 'unquote_to_bytes', 'unwrap', 'url2pathname', 'urlcleanup', 'urljoin', 'urlopen', 'urlparse', 'urlretrieve', 'urlsplit', 'urlunparse', 'warnings'] '''

Python globals和locals函数_reload函数:

globals( ):

返回所有能够访问到的全局名字

num = 5
sum = 0 def add(num):
func_sum = 0
func_sum += num
return func_sum print(globals())
'''
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x000001F5F98CC2E0>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'G:/code/time/1.py', '__cached__': None, 'num': 5, 'sum': 0, 'add': <function add at 0x000001F5F97E51F0>} ''' 注:我此处的代码是停留在昨天的time模块中,所以在路径下会出现G:/code/time/1.py

locals( ):

在函数中使用 locals ,返回形参和局部变量

num = 5
sum = 0 def add(num):
func_sum = 0
func_sum += num
print(locals())
return func_sum add(num)
# {'num': 5, 'func_sum': 5}

reload(模块名):reload 在 importlib 模块中

重新导入之前导入过的模块

注:当一个模块导入到另一个脚本时,模块顶层部分的代码只会被执行一次

# 重新导入模块
import func
# 导入自定义的模块
from importlib import reload
# reload 函数在 importlib 模块中
reload(func)
# 重新导入 func 模块
from func import get_info
get_info()
# 获取到了 func 模块的信息 使用reload的前提,是reload的 模块,之前已经使用import或者from导入过,否则会失败 import 导入的模块,使用模块名.方法的方式,reload会强制运行模块文件,然后原来导入的模块会被新使用的导入语句覆盖掉 from 导入的模块,本质是一个赋值操作
即在当前文件中(即执行 from 语句的文件)进行 attr = module.attr 注:reload 函数对 reload 运行之前的from语句没有影响

Python包:

包是一种管理 Python 模块命名空间的形式,采用 “点模块名称”

例:A.B  表示 A 模块的 B子模块

当不同模块间存在相同的变量名时,一个是使用 模块名.变量名 另一个是 变量名

当导入一个包时,Python 会根据 sys 模块的 path 变量中寻找这个包

目录中只有一个 __init__.py 文件才会被认为是一个包

导包常见的几种方式:

  import 模块名 或 包:调用方法,使用 模块名.方法

  from 模块名 import 子模块(子模块 或 函数 或 类 或 变量):使用函数调用

  from 模块名 import * :使用函数进行调用

  注:如果 __init__.py 文件中存在 __all__变量,则导入 __all__变量的值,在更新包的时候,注意修改__all__的值

  __all__ = ["echo", "surround", "reverse"] 导入 * 时,从 __all__ 导入

包还提供 __path__ ,一个目录列表,每一个目录都有为这个包服务的 __init__.py 文件,先定义,后运行其他的__init__.py文件

__path__ :主要用于扩展包里面的模块

float_num = 2.635
print("输出的是%f"%(float_num))
# 输出的是2.635000 # 保留两位小数
print("输出的是%.2f"%(float_num))
# 输出的是2.63 float_num = 5.99
print("输出的数字是{}".format(float_num))
# 输出的数字是5.99 # 指定参数名
float_num = 5.99
strs = 'happy'
print("输出的数字是{num},输出的字符串是{str}".format(num = float_num,str = strs))
# 输出的数字是5.99,输出的字符串是happy

input(字符串):字符串通常为提示语句

输入语句

注:通常使用变量进行接收,input 返回字符串类型,如果输入的为数字,可使用 int 转化为数字

# input(字符串):字符串通常为提示语句
# 输入语句
# 注:通常使用变量进行接收,input 返回字符串类型,如果输入的为数字,可使用 eval 转化为数字 strs = input("请输入一个字符串")
print(type(strs))
print("输入的字符串为:",strs) # 请输入一个字符串HELLO
# <class 'str'>
# 输入的字符串为: HELLO num = eval(input("请输入一个数字"))
print(type(num))
print("输入的数字为:",num) # 请输入一个数字56
# <class 'int'>
# 输入的数字为: 56
注:
input 使用 eval 进行转换时,如果输入的是
[1,2,3] 那么转换的就是 [1,2,3] 为列表类型

Python打开和关闭文件:

open(文件名,打开文件的模式[,寄存区的缓冲]):

  文件名:字符串值  

    注:文件名带有后缀名

# 打开创建好的 test.txt 文件
f = open("test.txt",'r') # 输出文件所有的内容
print(f.readlines( ))
# ['hello,world.\n'] # 关闭文件
f.close()

打开文件的模式: 
r
以只读方式打开文件。文件的指针将会放在文件的开头。这是默认模式。
rb
以二进制格式打开一个文件用于只读。文件指针将会放在文件的开头。这是默认模式。
r+
打开一个文件用于读写。文件指针将会放在文件的开头。
rb+
以二进制格式打开一个文件用于读写。文件指针将会放在文件的开头。
w
打开一个文件只用于写入。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。
wb
以二进制格式打开一个文件只用于写入。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。
w+
打开一个文件用于读写。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。
wb+
以二进制格式打开一个文件用于读写。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。
a
打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是说,新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。
ab
以二进制格式打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是说,新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。
a+
打开一个文件用于读写。如果该文件已存在,文件指针将会放在文件的结尾。文件打开时会是追加模式。如果该文件不存在,创建新文件用于读写。
ab+
以二进制格式打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。如果该文件不存在,创建新文件用于读写。

寄存区的缓冲:

    小于 0 的整数:系统默认设置寄存区的大小

    0:不进行寄存

    1:进行寄存

    大于 1 的整数:整数即为寄存区的缓冲区大小

Python read和write方法:

read():
从文件中读取字符串 注:Python 字符串可以是二进制数据,而不仅仅是文字。 语法: 文件对象.read([count])
count:打开文件需要读取的字符数 注:read 函数不使用 count 会尽可能多地读取更多的内容,通常一直读取到文件末尾
# 使用 count
# 打开创建好的 test.txt 文件
f = open("test.txt",'r')
# 输出文件的前 11 个字符
print(f.read(11)) # 关闭文件
f.close()
# hello,world

文件位置:

tell():

返回文件内当前指向的位置
f = open("test.txt",'r')
# 输出文件的前 11 个字符
f.read(11) print(f.tell())
# 11

seek(offset [,from]):

改变当前文件的位置

  offset:表示要移动的字节数

  from :指定开始移动字节的参考位置。

    0:文件开头

    1:当前位置

    2:文件末尾
# 打开创建好的 test.txt 文件
f = open("test.txt",'r')
# 输出文件的前 11 个字符
print(f.read(11))
# hello,world
# 返回文件内当前指向的位置
print(f.tell())
# 11
print(f.seek(0,0))
# 0
print(f.tell())
# 0
print(f.read(11))
# hello,world
# 关闭文件
f.close()

write( ):

将任意字符串写入一个文件中

注:Python字符串可以是二进制数据 和 文字,换行符('\n') 需要自己添加

语法:
文件对象.write(字符串) 程序: # write 方法
# 打开创建好的 test.txt 文件 f = open("test.txt",'w')
# 在开头,添加文件内容
f.write('hey boy') # 关闭文件
f.close()

PythonFile对象的属性:
一个文件被打开后,使用对象进行接收,接收的对象即为 File 对象 file.closed
返回true如果文件已被关闭,否则返回false file.mode
返回被打开文件的访问模式 file.name
返回文件的名称 file = open("test.txt",'r')
# file.name 返回文件的名称
print(file.name)
# test.txt # file.closed 如果文件未关闭返回 False
print(file.closed)
# False # file.mode 返回被打开文件的访问模式
print(file.mode)
# r # file.closed 如果文件已关闭返回 True
file.close()
print(file.closed)
# True

os 的方法:

mkdir(目录名):
在当前目录下创建新的目录 程序: import os
# 创建新的目录-包结构
os.mkdir('新目录-test') getcwd()方法:
显示当前的工作目录。 程序:
import os print(os.getcwd())
# G:\code\time chdir(修改的目录名):
修改当前的目录名为 修改的目录名 程序: import os
# 创建新的目录-包结构
print(os.getcwd())
# D:\见解\Python\Python代码\vacation\备课\新目录-test
os.chdir('新目录-test2')
print(os.getcwd())
# D:\见解\Python\Python代码\vacation\备课\新目录-test\新目录-test2 rmdir(目录名):
删除目录
注:目录名下为空,没有其他文件 程序: import os
os.rmdir('新目录-test2')
删除后包文件消失

rename(当前的文件名,新文件名):
将当前的文件名修改为新文件名 程序: # os.rename('旧名字',’新名字‘) import os
os.rename('旧名字.txt','新名字.txt') remove(文件名):
删除文件 程序:
import os
os.remove('名称.txt')

'''
编程完成一个简单的学生管理系统,要求如下:
(1)使用自定义函数,完成对程序的模块化
(2)学生信息至少包括:姓名、性别及手机号
(3)该系统具有的功能:添加、删除、修改、显示、退出系统
设计思路如下:
(1) 提示用户选择功能序号
(2) 获取用户选择的能够序号
(3) 根据用户的选择,调用相应的函数,执行相应的功能
'''
stu_lst = [[],[],[],[],[]]
# 创建存储五个学生的容器
def show_gn():
'''展示学生管理系统的功能'''
print("==========================")
print("学生管理系统v1.0")
print("1.添加学生信息(请先输入1)")
print("2.删除学生信息")
print("3.修改学生信息")
print("4.显示所有学生信息")
print("0.退出系统")
print("==========================") def tj_gn(num):
'''添加功能'''
stu_lst[num].append(input("请输入新学生的名字:"))
# 第一个参数为新学生的名字
stu_lst[num].append(input("请输入新学生的性别:"))
# 第二个参数为新学生的性别
stu_lst[num].append(input("请输入新学生的手机号:"))
# 第三个参数为新学生的手机号
stu_lst[num].append(num)
# 第四个参数为新学生的默认学号(从 0 开始) def sc_gn():
'''删除功能'''
stu_xlh = int(eval(input("请输入需要删除的学生序列号:")))
xs_gn_returni = xs_gn(stu_xlh)
pd_str = input("请问确定删除吗? 请输入全小写字母 yes / no ? ")
# pd_str 判断是否删除学生信息
if pd_str == 'yes':
del stu_lst[xs_gn_returni]
print("删除完毕")
if pd_str == 'no':
print("删除失败") def xg_gn():
'''修改功能'''
stu_xlh = int(eval(input("请输入需要修改的学生序列号:")))
xs_gn_returni = xs_gn(stu_xlh)
# xs_gn_returni 接收的是如果存在输入的学生序列号,则返回经过确认的索引下标
xg_str = input("请问需要修改该名学生哪一处信息,请输入提示后面的小写字母 (姓名)name,(性别)sex,(手机号)sjh")
if xg_str in ['name','sex','sjh']:
if xg_str == 'name':
stu_lst[xs_gn_returni][0] = input("请输入新的姓名值")
elif xg_str == 'sex':
stu_lst[xs_gn_returni][1] = input("请输入新的性别值")
else:
stu_lst[xs_gn_returni][2] = input("请输入新的手机号值")
else:
print("输入错误") def xs_gn(stu_xlh = -1):
'''显示功能'''
print("姓名性别手机号序列号信息如下")
if stu_xlh == -1:
for i in stu_lst:
if i != []:
print(i)
else:
for i in range(len(stu_lst)):
if stu_xlh in stu_lst[i] and i != []:
print("该学生信息如下:")
print(stu_lst[i])
return i show_gn() gn_num = int(eval(input("请输入功能对应的数字:")))
# gn_num 功能对应的数字 num = 0
while 0 <= gn_num < 1000:
if gn_num == 1:
tj_gn(num)
num += 1
gn_num = int(eval(input("请输入功能对应的数字:")))
elif gn_num == 2:
sc_gn()
gn_num = int(eval(input("请输入功能对应的数字:")))
elif gn_num == 3:
xg_gn()
gn_num = int(eval(input("请输入功能对应的数字:")))
elif gn_num == 4:
xs_gn()
gn_num = int(eval(input("请输入功能对应的数字:")))
elif gn_num == 0:
print("退出系统")
exit()
else:
print("请重新运行该程序,输入的数字不在 0~4 之中")
exit()

a = 10
b = 8 print("a>b") if a>b else pass
pass 为何报错问题:
第一部分:print
第二部分:("a>b") if a>b else pass 第一种情况 print ("a>b") 第二种情况 print(pass)
pass 关键字,不用于输出,导致出错

Python 实现分层聚类算法

'''
1.将所有样本都看作各自一类
2.定义类间距离计算公式
3.选择距离最小的一堆元素合并成一个新的类
4.重新计算各类之间的距离并重复上面的步骤
5.直到所有的原始元素划分成指定数量的类 程序要点:
1.生成测试数据
sklearn.datasets.make_blobs
2.系统聚类算法
sklearn.cluster.AgglomerativeClustering
3.必须满足该条件不然会报错(自定义函数中的参数)
assert 1 <= n_clusters <= 4
4.颜色,红绿蓝黄
r g b y
5. o * v +
散点图的形状
6.[] 内可以为条件表达式,输出数组中满足条件的数据
data[predictResult == i]
7.访问 x 轴,y 轴坐标
subData[:,0] subData[:,1]
8.plt.scatter(x轴,y轴,c,marker,s=40)
colors = "rgby"
markers = "o*v+"
c 颜色 c=colors[i]
marker 形状 marker=markers[i]
9.生成随机数据并返回样本点及标签
data,labels = make_blobs(n_samples=200,centers=4)
make_blobs 为 sklearn.datasets.make_blobs 库
n_samples 为需要的样本数量
centers 为标签数
'''
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
from sklearn.cluster import AgglomerativeClustering
def AgglomerativeTest(n_clusters):
assert 1 <= n_clusters <= 4
predictResult = AgglomerativeClustering(
n_clusters=n_clusters,
affinity='euclidean',
linkage='ward'
).fit_predict(data)
# 定义绘制散点图时使用的颜色和散点符号
colors = "rgby"
markers = "o*v+"
# 依次使用不同的颜色和符号绘制每个类的散点图
for i in range(n_clusters):
subData = data[predictResult == i]
plt.scatter(
subData[:,0],
subData[:,1],
c = colors[i],
marker = markers[i],
s = 40
)
plt.show()
# 生成随机数据,200个点,4类标签,返回样本及标签
data , labels = make_blobs(n_samples=200,centers=4)
print(data)
AgglomerativeTest(2)


KNN算法基本原理与sklearn实现

'''
KNN 近邻算法,有监督学习算法
用于分类和回归
思路:
1.在样本空间中查找 k 个最相似或者距离最近的样本
2.根据这 k 个最相似的样本对未知样本进行分类
步骤:
1.对数据进行预处理
提取特征向量,对原来的数据重新表达
2.确定距离计算公式
计算已知样本空间中所有样本与未知样本的距离
3.对所有的距离按升序进行排列
4.选取与未知样本距离最小的 k 个样本
5.统计选取的 k 个样本中每个样本所属类别的出现概率
6.把出现频率最高的类别作为预测结果,未知样本则属于这个类别
程序要点:
1.创建模型需要用到的包
sklearn.neighbors.KNeighborsClassifier
2.创建模型,k = 3
knn = KNeighborsClassifier(n_neighbors = 3)
n_neighbors 数值不同,创建的模型不同
3.训练模型,进行拟合
knn.fit(x,y)
x 为二维列表数据
x = [[1,5],[2,4],[2.2,5],
[4.1,5],[5,1],[5,2],[5,3],[6,2],
[7.5,4.5],[8.5,4],[7.9,5.1],[8.2,5]]
y 为一维分类数据,将数据分为 0 1 2 三类
y = [0,0,0,
1,1,1,1,1,
2,2,2,2]
4.进行预测未知数据,返回所属类别
knn.predict([[4.8,5.1]])
5.属于不同类别的概率
knn.predict_proba([[4.8,5.1]])
'''
from sklearn.neighbors import KNeighborsClassifier
# 导包
x = [[1,5],[2,4],[2.2,5],
[4.1,5],[5,1],[5,2],[5,3],[6,2],
[7.5,4.5],[8.5,4],[7.9,5.1],[8.2,5]]
# 设置分类的数据
y = [0,0,0,
1,1,1,1,1,
2,2,2,2]
# 对 x 进行分类,前三个分为 0类,1类和2类
knn = KNeighborsClassifier(n_neighbors=3)
# 创建模型 k = 3
knn.fit(x,y)
# 开始训练模型
'''
KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',
metric_params=None, n_jobs=None, n_neighbors=3, p=2,
weights='uniform')
'''
knn.predict([[4.8,5.1]])
# array([1]) 预测 4.8,5.1 在哪一个分组中
knn = KNeighborsClassifier(n_neighbors=9)
# 设置参数 k = 9
knn.fit(x,y)
'''
KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',
metric_params=None, n_jobs=None, n_neighbors=9, p=2,
weights='uniform')
'''
knn.predict([[4.8,5.1]])
# array([1])
knn.predict_proba([[4.8,5.1]])
# 属于不同类别的概率
# array([[0.22222222, 0.44444444, 0.33333333]])
# 返回的是在不同组的概率
'''
总结:
knn = KNeighborsClassifier(n_neighbors=3)
使用 KNeighborsClassifier 创建模型 n_neighbors 为 k
使用 knn.fit() 进行预测
第一个参数为 二维列表
第二个参数为 一维列表
使用 predict_proba([[num1,num2]])
查看num1,num2 在模型中出现的概率
'''

'''
程序要点:import numpy as np
1.查看 e 的 多少次方
np.exp(参数)
2.查看参数的平方根
np.sqrt(参数)
3.生成三维四列的随机值(-1,1)之间
np.random.random((3,4))
4.向下取整
a = np.floor(参数)
5.将矩阵拉平
a.ravel()
6.修改矩阵的形状
a.shape(6,2)
7.将矩阵转置
a.T
8.将矩阵横行进行拼接
a = np.floor(参数)
b = np.floor(参数)
np.hstack((a,b))
9.将矩阵纵行进行拼接
np.vstack((a,b))
10.按照行进行切分数组,切分为3份
np.hsplit(a,3)
注:第二个参数可以为元组(3,4)
11.按照列进行切分数组,切分为2份
np.vsplit(a,2) '''
import numpy as np
a = np.floor(10 *np.random.random((3,4)))
'''
array([[6., 8., 0., 9.],
[9., 3., 7., 4.],
[2., 4., 9., 1.]])
'''
np.exp(a)
'''
array([[4.03428793e+02, 2.98095799e+03, 1.00000000e+00, 8.10308393e+03],
[8.10308393e+03, 2.00855369e+01, 1.09663316e+03, 5.45981500e+01],
[7.38905610e+00, 5.45981500e+01, 8.10308393e+03, 2.71828183e+00]])
'''
np.sqrt(a)
'''
array([[2.44948974, 2.82842712, 0. , 3. ],
[3. , 1.73205081, 2.64575131, 2. ],
[1.41421356, 2. , 3. , 1. ]])
'''
a.ravel()
'''array([6., 8., 0., 9., 9., 3., 7., 4., 2., 4., 9., 1.])'''
a = np.floor(10 *np.random.random((3,4)))
b = np.floor(10 *np.random.random((3,4)))
np.hstack((a,b))
'''
array([[8., 2., 3., 8., 0., 1., 8., 6.],
[7., 1., 0., 3., 1., 9., 6., 0.],
[6., 0., 6., 6., 4., 3., 6., 3.]])
'''
np.vstack((a,b))
'''
array([[8., 2., 3., 8.],
[7., 1., 0., 3.],
[6., 0., 6., 6.],
[0., 1., 8., 6.],
[1., 9., 6., 0.],
[4., 3., 6., 3.]])
'''
a = np.floor(10 *np.random.random((2,12)))
np.hsplit(a,3)
'''
[array([[6., 4., 2., 2.],
[6., 2., 2., 8.]]), array([[1., 8., 8., 8.],
[1., 9., 3., 6.]]), array([[2., 6., 0., 8.],
[7., 1., 4., 3.]])]
'''
a = np.floor(10 *np.random.random((12,2)))
np.vsplit(a,3)
'''
[array([[2., 6.],
[7., 1.],
[4., 7.],
[3., 1.]]), array([[2., 5.],
[4., 6.],
[2., 0.],
[4., 4.]]), array([[9., 5.],
[7., 1.],
[2., 1.],
[5., 1.]])]
'''

注:上中下的这三篇随笔,在分类方面都会比较乱.

不同复制操作对比(三种)

'''
1.b = a
b 发生变化 a 也会发生变化
2.浅复制
c = a.view()
c.shape 发生变化,a.shape 不会发生变化
c 和 a 共用元素值,id 指向不同
c[1,0] = 1234 , a 的值也会发生变化
3.深复制
d = a.copy()
d[0,0] = 999
d 发生改变,a 不会发生改变
''' import numpy as np
a = np.arange(1,8)
# array([1, 2, 3, 4, 5, 6, 7])
b = a
b[2] = 999
b
# array([ 1, 2, 999, 4, 5, 6, 7])
a
# array([ 1, 2, 999, 4, 5, 6, 7]) a = np.arange(1,9)
c = a.view()
c.shape = 4,2
'''
array([[1, 2],
[3, 4],
[5, 6],
[7, 8]])
'''
a
# array([1, 2, 3, 4, 5, 6, 7, 8])
d = a.copy()
d[3] = 888
d
# array([ 1, 2, 3, 888, 5, 6, 7, 8])
a
# array([1, 2, 3, 4, 5, 6, 7, 8])

'''
1.查看列上最大索引的位置
data.argmax(axis = 0)
2.输出索引位置上的元素
data[index,range(data.shape[1])]
使用 range 输出几个元素
3.对numpy 对象进行扩展
a = np.array([4,5,6,2])
np.tile(a,(2,3))
4.对数组按行排序,从小到大
a = np.array([[4,3,5],[1,7,6]])
np.sort(a,axis = 1)
5.对数组元素进行排序,返回索引下标
a = np.array([4,3,1,2])
j = np.argsort(a)
a[j]
''' import numpy as np
data = np.array([
[4,5,6,8],
[7,4,2,8],
[9,5,4,2]
])
data.argmax(axis = 0)
# array([2, 0, 0, 0], dtype=int64) data.argmax(axis = 1)
# array([3, 3, 0], dtype=int64) index = data.argmax(axis = 0)
# array([9, 5, 6, 8]) a = np.array([4,5,6,2])
np.tile(a,(2,3))
'''
array([[4, 5, 6, 2, 4, 5, 6, 2, 4, 5, 6, 2],
[4, 5, 6, 2, 4, 5, 6, 2, 4, 5, 6, 2]])
''' a = np.array([[4,3,5],[1,7,6]])
'''
array([[4, 3, 5],
[1, 7, 6]])
''' np.sort(a,axis = 1)
'''
array([[3, 4, 5],
[1, 6, 7]])
''' np.sort(a,axis = 0)
'''
array([[1, 3, 5],
[4, 7, 6]])
''' a = np.array([4,3,1,2])
j = np.argsort(a)
# array([1, 2, 3, 4])

正则表达式
1. /b 和 /B
# /bthe 匹配任何以 the 开始的字符串
# /bthe/b 仅匹配 the
# /Bthe 任何包含但并不以 the 作为起始的字符串
2. [cr] 表示 c 或者 r
[cr][23][dp][o2]
一个包含四个字符的字符串,第一个字符是“c”或“r”,
然后是“2”或“3”,后面 是“d”或“p”,最后要么是“o”要么是“2”。
例如,c2do、r3p2、r2d2、c3po等
3.["-a]
匹配在 34-97 之间的字符
4.Kleene 闭包
* 匹配 0 次或 多次
+ 匹配一次或 多次
? 匹配 0 次或 1 次
5.匹配全部有效或无效的 HTML 标签
</?[^>]+>
6.国际象棋合法的棋盘移动
[KQRBNP][a-h][1-8]-[a-h][1-8]
从哪里开始走棋-跳到哪里
7.信用卡号码
[0-9]{15,16}
8.美国电话号码
\d{3}-/d{3}-/d{4}
800-555-1212
9.简单电子邮件地址
\w+@\w+\.com
XXX@YYY.com
10.使用正则表达式,使用一对圆括号可以实现
对正则表达式进行分组
匹配子组
对正则表达式进行分组可以在整个正则表达式中使用重复操作符
副作用:
匹配模式的子字符串可以保存起来
提取所匹配的模式
简单浮点数的字符串:
使用 \d+(\.\d*)?
0.004 2 75.
名字和姓氏
(Mr?s?\.)?[A-Z][a-z]*[A-Za-z-]+
11.在匹配模式时,先使用 ? ,实现前视或后视匹配 条件检查
(?P<name>) 实现分组匹配
(?P:\w+\.)* 匹配以 . 结尾的字符串
google twitter. facebook.
(?#comment) 用作注释
(?=.com) 如果一个字符串后面跟着 .com 则进行匹配
(?<=800-) 如果一个字符串之前为 800- 则进行匹配
不使用任何输入字符串
(?<!192\.168\.) 过滤掉一组 C 类 IP 地址
(?(1)y|x) 如果一个匹配组1(\1) 存在,就与y匹配,否则与x匹配

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


 

Pandas 复习

1.导包
import pandas as pd 2.数据读取,文件在该代码文件夹内
food_info = pd.read_csv('food_info.csv') 3.查看类型
food_info.dtypes 4.查看前五条数据
food_info.head()
查看前三条数据
food_info.head(3) 5.查看后四行数据
food_info.tail(4) 6.查看列名
food_info.columns 7.查看矩阵的维度
food_info.shape 8.取出第 0 号数据
food_info.loc[0]
使用切片获取数据
food_info.loc[3:6]
使用索引获取数据
food_info.loc['Name']
获取多个列
columns = ["Price","Name"]
food_info.loc[columns] 9.将列名放入到列表中
col_names = food_info.columns.tolist() 10.查看以 d 结尾的列
for c in col_names:
c.endswith("d") 11.将商品价格打一折
food_info["Price"]/10 12.最大值 最小值 均值
food_info["Price"].max()
food_info["Price"].min()
food_info["Price"].mean() 13.根据某一列进行排序
升序:
food_info.sort_values["Price",inplace=True]
降序:
food_info.sort_values["Price",inplace=True,ascending=False] 14.查看该数值是否为 NaN
price = food_info["Price"]
price_is_null = pd.isnull(price)
food_info[price_is_null]

Pandas 复习2

import pandas as pd
import numpy as np
food_info = pd.read_csv('food_info.csv')
1.处理缺失值(可使用平均数,众数填充)
查看非缺失值的数据:
price_is_null = pd.isnull(food_info["Price"])
price = food_info["Price"][price_is_null==False]
使用 fillna 填充
food_info['Price'].fillna(food_info['Price'].mean(),inplace = True) 2.求平均值
food_info["Price"].mean() 3.查看每一个 index 级,values 的平均值
food_info.pivot(index = "",values = "",aggfunc = np.mean) 4.查看总人数
food_info.pivot(index = "",values = ["",""],aggfunc = np.sum) 5.丢弃缺失值
dropna_columns = food_info.dropna(axis = 1)
将 Price 和 Time 列存在 NaN 的行去掉
new_food_info = food_info.dropna(axis = 0,subset = ["Price","Time"]) 6.定位具体值到 83
row_index_83_price = food_info.loc[83,"Price"] 7.进行排序(sort_values 默认升序)
new_food_info.sort_values("Price") 8.将索引值重新排序,使用 reset_index
new_food_info.reset_index(drop = True) 9.使用 apply 函数
new_food_info.apply(函数名) 10.查看缺失值的个数
def not_null_count(column):
column_null = pd.isnull(column)
# column_null 为空的布尔类型
null = column[column_null]
# 将为空值的列表传递给 null
return len(null)
column_null_count = food_info.apply(not_null_count) 11.划分等级:年龄 成绩
def which_class(row):
pclass = row["Pclass"]
if pd.isnull(pclass):
return "未知等级"
elif pclass == 1:
return "第一级"
elif pclass == 2:
return "第二级"
elif pclass == 3:
return "第三级"
new_food_info.apply(which_class,axis = 1) 12.使用 pivot_table 展示透视表
new_food_info.pivot_table(index = " ",values=" ")

Series结构(常用)

1.创建 Series 对象
fandango = pd.read_csv("xxx.csv")
series_rt = fandango["RottenTomatoes"]
rt_scores = series_rt.values
series_film = fandango["FILM"]
# 获取数据 使用 .values
film_names = series_film.values
series_custom = Series(rt_scores,index = film_names) 2.使用切片获取数据
series_custom[5:10] 3.转换为列表
original_index = series_custom.index.tolist() 4.进行排序,此时的 original_index 是一个列表
sorted_index = sorted(original_index) 5.排序索引和值
series_custom.sort_index()
series_custom.sort_values() 6.将大于 50 的数据输出
series_custom[series_custom > 50] 7.设置索引值
fandango.set_index("FILM",drop = False) 8.显示索引值
fandango.index 9.显示数据类型
series_film.dtypes 10.在 apply 中使用匿名函数
series_film.apply(lambda x : np.std(x),axis = 1)

部分画图

import pandas as pd
unrate = pd.read_csv("unrate.csv")
1.转换日期时间
unrate["date"] = pd.to_datetime(unrate["DATE"]) import matplotlib.pyplot as plt
2.画图操作
plt.plot()
传递 x y 轴,绘制折线图
plt.plot(unrate["date"],unrate["values"]) 3.展示
plt.show() 4.对 x 轴上的标签倾斜 45 度
plt.xticks(rotation = 45) 5.设置 x 轴 y 轴标题
plt.xlabel("xxx")
plt.ylabel("xxx") 6.设置名字
plt.title("xxx")
fig = plt.figure() 7.绘制子图
fig.add_subplot(行,列,x)
x 表示 在第几个模块
fig.add_subplot(4,3,1) 8.创建画图区域时指定大小
fig = plt.figure((3,6))
长为 3 宽为 6 9.画线时指定颜色
plt.plot(x,y,c="颜色") 10.将每一年的数据都显示出来
fig = plt.figure(figsize = (10,6))
colors = ['red','blue','green','orange','black']
# 设置颜色
for i in range(5):
start_index = i*12
end_index = (i+1)*12
# 定义开始和结束的位置
subset = unrate[start_index:end_index]
# 使用切片
label = str(1948 + i)
# 将标签 动态命名
plt.plot(subset['month'],subset['value'],c = colors[i],label = label)
# 进行绘制
plt.legend(loc = 'best')
loc = upper left 显示在左上角
# 打印右上角的数据
plt.show()
# 展示 11.柱状图:
明确:柱与柱之间的距离,柱的高度
高度:
cols = ['FILM','XXX','AAA','FFF','TTT','QQQ']
norm_reviews = reviews[cols]
num_cols = ['A','B','C','D','E','F']
bar_heights = norm_reviews.ix[0,num_cols].values
# 将每一列的高度都存起来
位置(距离 0 的距离):
bar_positions = arange(5) + 0.75
fig,ax = plt.subplots()
ax.bar(bar_positions,bar_heights,0.3)
先写位置后写距离
0.3 表示柱的宽度
ax.barh() 表示横着画
plt.show() 12.散点图
fig,ax = plt.subplots()
# 使用 ax 进行画图,ax 画图的轴,fig 图的样子
ax.scatter(norm_reviews['A'],norm_reviews['B'])
ax.set_xlabel('x')
ax.set_ylabel('y')
plt.show() # 使用 add_subplot 绘制子图
fig = plt.figure(figsize = (5,10))
ax1 = fig.add_subplot(2,2,1)
ax2 = fig.add_subplot(2,2,2)
ax1.scatter(x,y)
ax1.set_xlabel('x')
ax1.set_ylabel('y')
ax2.scatter(x2,y2)
ax2.set_xlabel('x')
ax2.set_ylabel('y')
plt.show() 13.当存在多个值时,可以指定区间进行画图
# 指定 bins 默认为 10
fig,ax = plt.subplots()
ax.hist(unrate['Price'],bins = 20)
ax.hist(unrate['Price'],range(4,5),bins = 20) 14.设置 x y 区间
sey_xlim(0,50)
set_ylim(0,50) 15.盒图:
num_cols = ['AA','BB','CC','DD']
fig,ax = plt.subplots()
ax.boxplot(norm_reviews[num_cols].values)
# 绘制盒图
ax.set_xticklabels(num_cols,rotation = 45)
# 设置 x 轴标签,并倾斜45度
ax.set_ylim(0,5)
# 设置 y 的区间
plt.show() 16.去掉图标后的尺
ax.tick_params(bottom = "off",top = "off",left = "off",right = "off") 17.展示在右上角
ax.legend(loc = "upper right")

提取txt文本有效内容
from re import sub

from jieba import cut

def getWordsFromFile(txtFile):
# 获取每一封邮件中的所有词语
words = []
# 将所有存储邮件文本内容的记事本文件都使用 UTF8 编码
with open(txtFile,encoding = "utf8") as fp:
for line in fp:
# 遍历每一行,删除两端的空白字符
line = line.strip()
# 过滤掉干扰字符
line = sub(r'[.【】 0-9、-。,!~\*]','',line)
# 对 line 进行分词
line = cut(line)
# 过滤长度为 1 的词
line = filter(lambda word:len(word) > 1 ,line)
# 将文本预处理得到的词语添加到 words 列表中
words.extend(line)
return words

chain 和 Counter
from collections import Counter
from itertools import chain 1.使用 chain 对 allwords 二维列表进行解包
解包: chain(*allwords)
将 allwords 里面的子列表解出来 2.获取有效词汇的数目
freq = Counter(chain(*allwords)) 3.Counter 返回的是可迭代对象出现的次数
使用 most_common 方法返回出现次数最多的前三个
.most_common(3)
Counter ("dadasfafasfa")
Counter({'a': 5, 'f': 3, 'd': 2, 's': 2})
Counter ("dadasfafasfa").most_common(2)
[('a', 5), ('f', 3)]

1.特征向量
每一个有效词汇在邮件中出现的次数(使用一维列表方法)
word 词汇出现的次数
一维列表.count(word)
2.将列表转换为数组形式 array(参数)
创建垃圾邮件,正常邮件训练集
array(列表对象 或 表达式)
3.使用 朴素贝叶斯算法
model = MultinomialNB()
4.进行训练模型 model.fit
model.fit(array数组,array数组)
5.对指定 topWords 数据使用函数
map(lambda x:words.count(x),topWords)
6.预测数据 model.predict ,返回值为 0 或 1
result = model.predict(array数组.reshape(1,-1))[0]
7.查看在不同区间的概率
model.predict_proba(array数组.reshape(1,-1))
8.条件语句,预测的结果便于区分 1 为垃圾邮件,0 为 正常邮件
return "垃圾邮件" if result == 1 else "正常邮件"

函数进阶1

'''
1.print "a>b" if a>b else pass 出错问题
pass 不可以被输出,导致报错 2.定义函数:
def 函数名():
return 可选 3.print 输出时会运行函数
print func_name()
注:func_name 中有 print 后,最好不要再使用 print 输出
会返回两个结果 4.最好让函数拥有返回值,便于维护
没有返回值会返回 None 5.如何制造函数:
抽象需求,注意可维护性
当创造方法时,注意可维护性和健壮性 6.参数使用 * 号,函数内为元组对象 7.可选参数存在默认值,必选参数没有默认值 8.健壮性:
直到函数会返回什么(异常处理,条件判断)
返回的结果是你需要的 9.测试时使用断言 assert 程序:''' def func_name():
return 1
print(func_name())
# 1 def func_name2():
print("hello")
print(func_name2())
# hello
# None def add(num1,num2):
return num1 + num2
print(add(5,6))
# 11 def add(*num):
d = 0
for i in num:
d += i
return d
print(add(1,2,3,4))
# 10 def add(num1,num2 = 4):
return num1 + num2
print(add(5))
# 9
print(add(5,8))
# 13 def add(num1,num2):
# 健壮性
if isinstance(num1,int) and isinstance(num2,int):
return num1 + num2
else:
return "Error"
print(add('a',(1,2,3)))
# Error
print(add(3,4))
# 7

'''
1.在循环中不要使用 排序函数
2.解决问题先要有正确的思路
写出伪代码
第一步做什么
第二步做什么
...
慢慢实现 3.使用 filter 函数
当函数中参数类型为 int 时才进行比较
def func(*num):
num = filter(lambda x:isinstance(x,int),num) 4.参数为 module ,将参数输出
print("doc %s"%module) 5.不要将代码复杂化,让人一看到就知道实现了什么功能 6.os.path.exists(file) 作为条件判断语句,看是否存在该 file 文件 7.检测函数 assert:
类型断言、数据断言 8.将问题实现的越简单越好,测试完整 9.使用下划线或驼峰命名函数名
get_doc getDoc 10.伪代码:
将思路写出来 11.默认值的好处:
省事,方便配置,多写注释
传入参数的数据类型
返回的数据的类型 12.测试 程序:'''
def function(*num):
# 输出 最大值和最小值
num = filter(lambda x : isinstance(x,int),num)
# 过滤掉不是 int 类型的数据
a = sorted(num)
return "max:",a[-1],"min:",a[0] print(function(5,6,"adaf",1.2,99.5,[4,5])) # ('max:', 6, 'min:', 5)

Python异常及异常处理:

当程序运行时,发生的错误称为异常

例:

  0 不能作为除数:ZeroDivisionError

  变量未定义:NameError

  不同类型进行相加:TypeError

异常处理:

'''
try:
执行代码
except:
发生异常时执行的代码 执行 try 语句:
如果发生异常,则跳转到 except 语句中 如果没有异常,则运行完 try 语句,继续 except 后面的语句 '''

False:布尔类型,假。当条件判断不成立时,返回False。

# == 判断两个对象的值是否相等
print('' == False)
# False print(None == False)
# False print([] == False)
# False print(() == False)
# False print({} == False)
# False # is 判断两个对象是否引用自同一地址空间 print('' is False)
# False print(None is False)
# False print([] is False)
# False print(() is False)
# False print({} is False)
# False

正则表达式补充2

 

 

 


正则表达式基础1

'''
1.指定 eval()调用一个代码对象会提高性能 2.在进行模式匹配之前,正则表达式的模式必须编译为正则表达式对象
匹配时需要进行多次匹配,进行预编译可以提升速度
re.compile(pattern,flags = 0) 3.消除缓存
re.purge() 4.使用 re.S 后 . 可以匹配换行符 \n 5.使用了match() 和 search() 方法之后返回的对象称为匹配对象
匹配对象常用 group() 和 groups() 方法
group() 返回整个匹配对象 或 特定子组
groups() 返回一个包含唯一 或 全部子组的元组 6.match() 方法 对字符串的起始部分进行模式匹配
成功:返回一个匹配对象
失败:返回 None 7.search() 方法 对字符串任意位置进行模式匹配
成功:返回匹配对象
失败:返回 None 8.可以使用 pos , endpos 参数指定目标字符串的搜索范围 9.使用 . 匹配任何单个字符
.end 不能匹配 end
不能匹配 \n 换行符
使用 \. 匹配 . 10.创建字符集 [ ]
[cr][23] 表示 匹配第一个字符 c 或者 r
第二个字符 2 或者 3 程序:'''
import re
# match(pattern,string,flags = 0) 匹配示例
m = re.match('foo','foo')
if m is not None:
# 如果匹配成功则输出匹配内容
print(m.group())
# print(m.groups()) 返回空元组 因为没有子组
# foo
# 匹配对象具有 group() 和 groups() 方法
print(m)
# <re.Match object; span=(0, 3), match='foo'> # search(pattern,string,flags = 0) 匹配示例
m = re.search('foo','sea food')
if m is not None:
print(m.group())
# foo
print(m)
# <re.Match object; span=(4, 7), match='foo'>
# 返回第四个元素的位置 # | 或 示例
bt = 'bat|bet'
m = re.match(bt,'bat')
# match 只匹配开头
m2 = re.search(bt,'abat')
# search 从开始到结尾
print(m.group())
# bat
print(m2.group())
# bat # . 匹配任意字符示例
anyend = '.end'
m = re.match(anyend,'aend')
print(m.group())
# aend
m2 = re.search(anyend,'abcdend')
print(m2.group())
# dend pi_pattern = '3.14'
m = re.match(pi_pattern,'3.14')
print(m.group())
# 3.14
pi_pattern = '3\.14'
# 将 . 转义
m = re.match(pi_pattern,'3.14')
print(m.group())
# 3.14 # [ ] 创建字符集
m = re.match('[cr][23][dp][po]','c3po')
print(m.group())
# c3po m = re.match('[cr][23][dp][po]','c2do')
print(m.group())
# c2do

两数相加(B站看视频总结)

'''
两数相加:
给出两个 非空 的链表用来表示两个非负的整数
各自的位数是按照逆序的方式存储的 每一个节点只能保存 一位数
示例:
输入:(2->4->3) + (5->6->4)
输出:7->0->8
原因:342 + 465 = 807 ''' class ListNode:
def __init__(self,x):
# 在类声明时进行调用
self.val = x
self.next = None
# self 指的是自身,在类中声明函数需要添加才可以访问自身元素和其他函数
a = ListNode(10086)
# print(a,a.val)
# <__main__.ListNode object at 0x000001F76D46A148> 10086 # # 实现尾部元素指向头部元素
# move = a
# for i in range(4):
# temp = ListNode(i)
# # temp 为 ListNode 节点
# move.next = temp
# # move 下面的节点为 temp
# move = move.next
# # 将节点向下移动
# move.next = a
# # 重新指向头节点 a
class Solution1:
def addTwoNumbers(self,l1:ListNode,l2:ListNode) ->ListNode:
res = ListNode(10086)
move = res
carry = 0
# 进位
while l1 != None or l2 != None:
if l1 == None:
l1,l2 = l2,l1
# 替换位置,将 l1 作为输出
if l2 == None:
carry,l1.val = divmod((l1.val + carry),10)
# 对 l1 进行刷新
move.next = l1
# 设置数据
l1,l2,move = l1.next,l2.next,move.next
# 将数据向下移动
else:
carry,l1.val = divmod((l1.val + l2.val + carry),10)
# 如果都不为 None,则对应位置进行相加,然后进行求余
move.next = l1
# 更新数据
l1,move = l1.next,move.next
# 向下移动
if carry == 1:
move.next = ListNode(carry)
return res.next

class ListNode:
def __init__(self,x):
# 在类声明时进行调用
self.val = x
self.next = None
# self 指的是自身,在类中声明函数需要添加才可以访问自身元素和其他函数
# a = ListNode(10086) # 使用迭代写法
class Solution1:
def addTwoNumbers(self,l1:ListNode,l2:ListNode) -> ListNode:
def recursive(n1,n2,carry = 0):
if n1 == None and n2 == None:
return ListNode(1) if carry == 1 else None
# 如果存在进位 则 输出 1
if n1 == None:
n1,n2 = n2,n1
# 当 n1 为空时 将位置替换
return recursive(n1,None,carry)
# 进行递归 使用 n1 进行递归
if n2 == None:
carry,n1.val = divmod((n1.val + carry),10)
# 返回值为 进位和数值 将 n1 的值进行替换
n1.next = recursive(n1.next,None,carry)
# 对 n1 接下来的数据继续进行调用,更新 n1 链表
return n1
carry,n1.val = divmod((n1.val + n2.val + carry),10)
# 当不存在空值时,进行相加,更新 n1 值
n1.next = recursive(n1.next,n2.next,carry)
# 设置 n1 接下来的值为 所有 n1 和 n2 接下来的运算调用
return n1
return recursive(l1,l2)
# 返回到内部函数中

猜数字游戏

'''
分析:
参数->指定整数范围,最大次数
在指定范围内随机产生一个整数,让用户猜测该数
提示,猜大,猜小,猜对
给出提示,直到猜对或次数用尽
'''
import random def fail(os_num):
'''输入数字范围错误,没有猜数字次数'''
print("猜数失败")
print("系统随机的数为:", os_num)
print("游戏结束,欢迎下次再来玩")
return def cxsr(count):
'''重新输入一个数'''
count -= 1
print("提示:您还有 %d 次机会" % (count))
if count == 0:
fail(os_num)
else:
user_cs = int(eval(input("请重新输入一个 0~8 之间的整数:\n")))
csz(os_num,count,user_cs) def csz(os_num,count,user_cs,num_range = 8):
'''这是一个猜数字的函数'''
# num_range 是整数范围,count为最大次数,user_cs 为用户猜到的数
if user_cs > num_range or user_cs < 0 :
print("请重新运行,输入错误~")
return
if count == 0:
fail()
else:
if os_num > user_cs:
print("您猜的数字比系统产生的随机数小")
cxsr(count) elif os_num < user_cs:
print("您猜的数字比系统产生的随机数大")
cxsr(count) else:
print("恭喜您,猜对了~")
print("欢迎下次再玩!")
os_num = random.randint(0,8)
# os_num 为系统产生的随机数
print("游戏开始~")
user_cs = int(eval(input("这是一个猜数字的游戏(您有三次猜数字的机会),请输入一个 0~8 之间的整数\n")))
# user_cs 为用户猜到的数
csz(os_num,3,user_cs)

由于以前的随笔中的习题和我手里现存的题太多了,大约1000多道左右,所以此处不做整理了.只做一些知识点上的积累

函数进阶3

'''
1.考虑可维护性
一行代码尽量简单
列表推导式 lambda 匿名函数 2.断言语句用于自己测试,不要写在流程控制中
assert 不要写在 for 循环中 3.程序的异常处理 参数处理
try 异常处理 ,参数类型是什么 4.函数->尽量不要在特定环境下使用 5.断言就是异常->出错了就会抛出异常 6.局部变量和全局变量的区别:
当局部变量与全局变量重名时,生成一个在局部作用域中的变量
使用 global 声明 可以让局部变量修改为全局变量 7.参数为可变参数时,使用索引下标会修改原数据 程序:'''
def func1(num1,num2):
return num1 + num2 # 打印变量名
print(func1.__code__.co_varnames)
# ('num1', 'num2')
print(func1.__code__.co_filename)
# 文件名 # 第六点:
arg = 6
def add(num = 3):
arg = 4
return arg + num
print(add())
# 7 arg = 6
def add(num = 3):
# 使用 global 声明
global arg
return arg + num
print(add())
# 9

函数进阶4
'''
1.匿名函数:
一个表达式,没有 return
没有名称
执行很小的功能 2.判断参数是否存在 如果不存在会怎样->给出解决办法 3.可以使用 filter 和 lambda 进行使用
如果不进行 list 转换,则只返回 filter 对象 4.参数:
位置匹配:
func(name)
关键字匹配:
func(key = value)
从最右面开始赋予默认值
收集匹配:
元组收集
func(name,arg1,arg2)
func(*args)
字典收集
func(name,key1 = value1,key2 = value2)
func(**kwargs)
参数顺序 5.递归:
递归就是调用自身 程序:'''
# 第一点:
d = lambda x:x+1
print(d(2))
# 3 d = lambda x:x + 1 if x > 0 else "不大于0"
print(d(3))
# 4
print(d(-3))
# 不大于0 # 列表推导
g = lambda x:[(x,i) for i in range(5)]
print(g(5))
# 只传递了一个参数
# [(5, 0), (5, 1), (5, 2), (5, 3), (5, 4)] # 第三点:
t = [1,4,7,8,5,3,9]
g = list(filter(lambda x: x > 5,t))
# 使用 list 将结果转换为列表
print(g)
# [7, 8, 9] # 第四点:
# 对应位置传递参数
def func(arg1,arg2,arg3):
return arg1,arg2,arg3 print(func(1,2,3))
# (1, 2, 3) # 关键字匹配,不按照位置进行匹配
def func(k1 = '',k2 = '',k3 = ''):
return k1,k2,k3 print(func(k2 = 4 , k3 = 5 , k1 = 3))
# (3, 4, 5) # 收集匹配
# 元组
def func(*args):
return args
print(func(5,6,7,8,[1,2,3]))
# (5, 6, 7, 8, [1, 2, 3]) def func(a,*args):
# 先匹配 a 后匹配 *args
return args
print(func(5,6,7,8,[1,2,3]))
# (6, 7, 8, [1, 2, 3]) # 字典
def func(**kwargs):
return kwargs
print(func(a = 5,b = 8))
# {'a': 5, 'b': 8}

列表推导式

lst = [1,2,3]
lst2 = [1,2,3,5,1]
lst3 = [1,2]
lst4 = [1,2,3,65,8]
lst5 = [1,2,3,59,5,1,2,3] def length(*args):
# 返回长度
lens = []
lens = [len(i) for i in args]
return lens
print(length(lst,lst2,lst3,lst4,lst5))
# [3, 5, 2, 5, 8] dic = dict(zip(['a','b','c','d','e'],[4,5,6,7,8]))
lst = ["%s : %s" %(key,value) for key,value in dic.items()]
print(lst)
# ['a : 4', 'b : 5', 'c : 6', 'd : 7', 'e : 8']

关于类和异常的笔记
原文链接:https://www.runoob.com/python/python-object.html 面向对象技术简介
类(Class): 用来描述具有相同的属性和方法的对象的集合。它定义了该集合中每个对象所共有的属性和方法。对象是类的实例。
类变量:类变量在整个实例化的对象中是公用的。类变量定义在类中且在函数体之外。类变量通常不作为实例变量使用。
数据成员:类变量或者实例变量, 用于处理类及其实例对象的相关的数据。
方法重写:如果从父类继承的方法不能满足子类的需求,可以对其进行改写,这个过程叫方法的覆盖(override),也称为方法的重写。
局部变量:定义在方法中的变量,只作用于当前实例的类。
实例变量:在类的声明中,属性是用变量来表示的。这种变量就称为实例变量,是在类声明的内部但是在类的其他成员方法之外声明的。
继承:即一个派生类(derived class)继承基类(base class)的字段和方法。继承也允许把一个派生类的对象作为一个基类对象对待。例如,有这样一个设计:一个Dog类型的对象派生自Animal类,这是模拟"是一个(is-a)"关系(例图,Dog是一个Animal)。
实例化:创建一个类的实例,类的具体对象。
方法:类中定义的函数。
对象:通过类定义的数据结构实例。对象包括两个数据成员(类变量和实例变量)和方法。
创建类
使用 class 语句来创建一个新类,class 之后为类的名称并以冒号结尾: class ClassName:
'类的帮助信息' #类文档字符串
class_suite #类体
类的帮助信息可以通过ClassName.__doc__查看 class_suite 由类成员,方法,数据属性组成 实例
以下是一个简单的 Python 类的例子: 实例
#!/usr/bin/python
# -*- coding: UTF-8 -*- class Employee:
'所有员工的基类'
empCount = 0 def __init__(self, name, salary):
self.name = name
self.salary = salary
Employee.empCount += 1 def displayCount(self):
print "Total Employee %d" % Employee.empCount def displayEmployee(self):
print "Name : ", self.name, ", Salary: ", self.salary
empCount 变量是一个类变量,它的值将在这个类的所有实例之间共享。你可以在内部类或外部类使用 Employee.empCount 访问。 第一种方法__init__()方法是一种特殊的方法,被称为类的构造函数或初始化方法,当创建了这个类的实例时就会调用该方法 self 代表类的实例,self 在定义类的方法时是必须有的,虽然在调用时不必传入相应的参数。 self代表类的实例,而非类
类的方法与普通的函数只有一个特别的区别——它们必须有一个额外的第一个参数名称, 按照惯例它的名称是 self。 class Test:
def prt(self):
print(self)
print(self.__class__) t = Test()
t.prt()
以上实例执行结果为: <__main__.Test instance at 0x10d066878>
__main__.Test
从执行结果可以很明显的看出,self 代表的是类的实例,代表当前对象的地址,而 self.__class__ 则指向类。 self 不是 python 关键字 "创建 Employee 类的第一个对象"
emp1 = Employee("Hany", 9000) 访问属性
可以使用点号 . 来访问对象的属性。使用如下类的名称访问类变量: emp1.displayEmployee()
emp2.displayEmployee()
print "Total Employee %d" % Employee.empCount 可以添加,删除,修改类的属性,如下所示: del emp1.age # 删除 'age' 属性 你也可以使用以下函数的方式来访问属性: getattr(obj, name[, default]) : 访问对象的属性。
hasattr(obj,name) : 检查是否存在一个属性。
setattr(obj,name,value) : 设置一个属性。如果属性不存在,会创建一个新属性。
delattr(obj, name) : 删除属性。
hasattr(emp1, 'age') # 如果存在 'age' 属性返回 True。
getattr(emp1, 'age') # 返回 'age' 属性的值
setattr(emp1, 'age', 8) # 添加属性 'age' 值为 8
delattr(emp1, 'age') # 删除属性 'age'
Python内置类属性
__dict__ : 类的属性(包含一个字典,由类的数据属性组成)
__doc__ :类的文档字符串
__name__: 类名
__module__: 类定义所在的模块(类的全名是'__main__.className',如果类位于一个导入模块mymod中,那么className.__module__ 等于 mymod)
__bases__ : 类的所有父类构成元素(包含了一个由所有父类组成的元组)
Python内置类属性调用实例如下: python对象销毁(垃圾回收)
Python 使用了引用计数这一简单技术来跟踪和回收垃圾。 在 Python 内部记录着所有使用中的对象各有多少引用。
一个内部跟踪变量,称为一个引用计数器。 当对象被创建时, 就创建了一个引用计数, 当这个对象不再需要时, 也就是说, 这个对象的引用计数变为0 时, 它被垃圾回收。但是回收不是"立即"的,
由解释器在适当的时机,将垃圾对象占用的内存空间回收。 a = 40 # 创建对象 <40>
b = a # 增加引用, <40> 的计数
c = [b] # 增加引用. <40> 的计数 del a # 减少引用 <40> 的计数
b = 100 # 减少引用 <40> 的计数
c[0] = -1 # 减少引用 <40> 的计数
垃圾回收机制不仅针对引用计数为0的对象,同样也可以处理循环引用的情况。循环引用指的是,两个对象相互引用,但是没有其他变量引用他们。
这种情况下,仅使用引用计数是不够的。Python 的垃圾收集器实际上是一个引用计数器和一个循环垃圾收集器。
作为引用计数的补充, 垃圾收集器也会留心被分配的总量很大(及未通过引用计数销毁的那些)的对象。 在这种情况下, 解释器会暂停下来, 试图清理所有未引用的循环。 析构函数 __del__ ,__del__在对象销毁的时候被调用,当对象不再被使用时,__del__方法运行: 类的继承
面向对象的编程带来的主要好处之一是代码的重用,实现这种重用的方法之一是通过继承机制。 通过继承创建的新类称为子类或派生类,被继承的类称为基类、父类或超类。 继承语法 class 派生类名(基类名)
...
在python中继承中的一些特点: 1、如果在子类中需要父类的构造方法就需要显式的调用父类的构造方法,或者不重写父类的构造方法。详细说明可查看: python 子类继承父类构造函数说明。
2、在调用基类的方法时,需要加上基类的类名前缀,且需要带上 self 参数变量。区别在于类中调用普通函数时并不需要带上 self 参数
3、Python 总是首先查找对应类型的方法,如果它不能在派生类中找到对应的方法,它才开始到基类中逐个查找。(先在本类中查找调用的方法,找不到才去基类中找)。
如果在继承元组中列了一个以上的类,那么它就被称作"多重继承" 。 语法: 派生类的声明,与他们的父类类似,继承的基类列表跟在类名之后,如下所示: class SubClassName (ParentClass1[, ParentClass2, ...]):
...
实例
#!/usr/bin/python
# -*- coding: UTF-8 -*- class Parent: # 定义父类
parentAttr = 100
def __init__(self):
print "调用父类构造函数" def parentMethod(self):
print '调用父类方法' def setAttr(self, attr):
Parent.parentAttr = attr def getAttr(self):
print "父类属性 :", Parent.parentAttr class Child(Parent): # 定义子类
def __init__(self):
print "调用子类构造方法" def childMethod(self):
print '调用子类方法' c = Child() # 实例化子类
c.childMethod() # 调用子类的方法
c.parentMethod() # 调用父类方法
c.setAttr(200) # 再次调用父类的方法 - 设置属性值
c.getAttr() # 再次调用父类的方法 - 获取属性值
以上代码执行结果如下: 调用子类构造方法
调用子类方法
调用父类方法
父类属性 : 200
你可以继承多个类 class A: # 定义类 A
..... class B: # 定义类 B
..... class C(A, B): # 继承类 A 和 B
.....
可以使用issubclass()或者isinstance()方法来检测。 issubclass() - 布尔函数判断一个类是另一个类的子类或者子孙类,语法:issubclass(sub,sup)
isinstance(obj, Class) 布尔函数如果obj是Class类的实例对象或者是一个Class子类的实例对象则返回true。
方法重写
如果你的父类方法的功能不能满足你的需求,你可以在子类重写你父类的方法: 下表列出了一些通用的功能:
1 __init__ ( self [,args...] )
构造函数
简单的调用方法: obj = className(args)
2 __del__( self )
析构方法, 删除一个对象
简单的调用方法 : del obj
3 __repr__( self )
转化为供解释器读取的形式
简单的调用方法 : repr(obj)
4 __str__( self )
用于将值转化为适于人阅读的形式
简单的调用方法 : str(obj)
类属性与方法
类的私有属性
__private_attrs:两个下划线开头,声明该属性为私有,不能在类的外部被使用或直接访问。在类内部的方法中使用时 self.__private_attrs。 类的方法
在类的内部,使用 def 关键字可以为类定义一个方法,与一般函数定义不同,类方法必须包含参数 self,且为第一个参数 类的私有方法
__private_method:两个下划线开头,声明该方法为私有方法,不能在类的外部调用。在类的内部调用 self.__private_methods
Python 通过改变名称来包含类名: Traceback (most recent call last):
File "test.py", line 17, in <module>
print counter.__secretCount # 报错,实例不能访问私有变量
AttributeError: JustCounter instance has no attribute '__secretCount'
Python不允许实例化的类访问私有数据,但可以使用 object._className__attrName( 对象名._类名__私有属性名 )访问属性: 单下划线、双下划线、头尾双下划线说明:
__foo__: 定义的是特殊方法,一般是系统定义名字 ,类似 __init__() 之类的。 _foo: 以单下划线开头的表示的是 protected 类型的变量,即保护类型只能允许其本身与子类进行访问,不能用于 from module import * __foo: 双下划线的表示的是私有类型(private)的变量, 只能是允许这个类本身进行访问了。
原文链接:https://www.runoob.com/python3/python3-class.html

Python中的类提供了面向对象编程的所有基本功能:类的继承机制允许多个基类,派生类可以覆盖基类中的任何方法,方法中可以调用基类中的同名方法。

对象可以包含任意数量和类型的数据。

类定义
语法格式如下: class ClassName:
<statement-1>
.
.
.
<statement-N>
类实例化后,可以使用其属性,实际上,创建一个类之后,可以通过类名访问其属性。 类对象
类对象支持两种操作:属性引用和实例化。 属性引用使用和 Python 中所有的属性引用一样的标准语法:obj.name。 类对象创建后,类命名空间中所有的命名都是有效属性名: 类有一个名为 __init__() 的特殊方法(构造方法),该方法在类实例化时会自动调用 self代表类的实例,而非类
类的方法与普通的函数只有一个特别的区别——它们必须有一个额外的第一个参数名称, 按照惯例它的名称是 self。 继承
Python 同样支持类的继承,如果一种语言不支持继承,类就没有什么意义。派生类的定义如下所示: class DerivedClassName(BaseClassName1):
<statement-1>
.
.
.
<statement-N>
BaseClassName(示例中的基类名)必须与派生类定义在一个作用域内。除了类,还可以用表达式,基类定义在另一个模块中时这一点非常有用: class DerivedClassName(modname.BaseClassName): 多继承
Python同样有限的支持多继承形式。多继承的类定义形如下例: class DerivedClassName(Base1, Base2, Base3):
<statement-1>
.
.
.
<statement-N>
需要注意圆括号中父类的顺序,若是父类中有相同的方法名,而在子类使用时未指定,python从左至右搜索 即方法在子类中未找到时,从左到右查找父类中是否包含方法。 方法重写
如果你的父类方法的功能不能满足你的需求,你可以在子类重写你父类的方法: 类的私有属性
__private_attrs:两个下划线开头,声明该属性为私有,不能在类的外部被使用或直接访问。在类内部的方法中使用时 self.__private_attrs。 类的方法
在类的内部,使用 def 关键字来定义一个方法,与一般函数定义不同,类方法必须包含参数 self,且为第一个参数,self 代表的是类的实例。 类的私有方法
__private_method:两个下划线开头,声明该方法为私有方法,只能在类的内部调用 ,不能在类的外部调用。self.__private_methods。 类的实例化对象不能访问类中的私有属性 类的专有方法:
__init__ : 构造函数,在生成对象时调用
__del__ : 析构函数,释放对象时使用
__repr__ : 打印,转换
__setitem__ : 按照索引赋值
__getitem__: 按照索引获取值
__len__: 获得长度
__cmp__: 比较运算
__call__: 函数调用
__add__: 加运算
__sub__: 减运算
__mul__: 乘运算
__truediv__: 除运算
__mod__: 求余运算
__pow__: 乘方

Python 子类继承父类构造函数说明
原文链接:https://www.runoob.com/w3cnote/python-extends-init.html 如果在子类中需要父类的构造方法就需要显式地调用父类的构造方法,或者不重写父类的构造方法。 子类不重写 __init__,实例化子类时,会自动调用父类定义的 __init__。 父类名称.__init__(self,参数1,参数2,...)
实例
class Father(object):
def __init__(self, name):
self.name=name
print ( "name: %s" %( self.name))
def getName(self):
return 'Father ' + self.name class Son(Father):
def __init__(self, name):
super(Son, self).__init__(name)
print ("hi")
self.name = name
def getName(self):
return 'Son '+self.name if __name__=='__main__':
son=Son('runoob')
print ( son.getName() )
输出结果为: name: runoob
hi
Son runoob

Python 异常(菜鸟教程)
原文:https://www.runoob.com/python/python-exceptions.html BaseException 所有异常的基类
SystemExit 解释器请求退出
KeyboardInterrupt 用户中断执行(通常是输入^C)
Exception 常规错误的基类
StopIteration 迭代器没有更多的值
GeneratorExit 生成器(generator)发生异常来通知退出
StandardError 所有的内建标准异常的基类
ArithmeticError 所有数值计算错误的基类
FloatingPointError 浮点计算错误
OverflowError 数值运算超出最大限制
ZeroDivisionError 除(或取模)零 (所有数据类型)
AssertionError 断言语句失败
AttributeError 对象没有这个属性
EOFError 没有内建输入,到达EOF 标记
EnvironmentError 操作系统错误的基类
IOError 输入/输出操作失败
OSError 操作系统错误
WindowsError 系统调用失败
ImportError 导入模块/对象失败
LookupError 无效数据查询的基类
IndexError 序列中没有此索引(index)
KeyError 映射中没有这个键
MemoryError 内存溢出错误(对于Python 解释器不是致命的)
NameError 未声明/初始化对象 (没有属性)
UnboundLocalError 访问未初始化的本地变量
ReferenceError 弱引用(Weak reference)试图访问已经垃圾回收了的对象
RuntimeError 一般的运行时错误
NotImplementedError 尚未实现的方法
SyntaxError Python 语法错误
IndentationError 缩进错误
TabError Tab 和空格混用
SystemError 一般的解释器系统错误
TypeError 对类型无效的操作
ValueError 传入无效的参数
UnicodeError Unicode 相关的错误
UnicodeDecodeError Unicode 解码时的错误
UnicodeEncodeError Unicode 编码时错误
UnicodeTranslateError Unicode 转换时错误
Warning 警告的基类
DeprecationWarning 关于被弃用的特征的警告
FutureWarning 关于构造将来语义会有改变的警告
OverflowWarning 旧的关于自动提升为长整型(long)的警告
PendingDeprecationWarning 关于特性将会被废弃的警告
RuntimeWarning 可疑的运行时行为(runtime behavior)的警告
SyntaxWarning 可疑的语法的警告
UserWarning 用户代码生成的警告 什么是异常?
异常即是一个事件,该事件会在程序执行过程中发生,影响了程序的正常执行。
一般情况下,在Python无法正常处理程序时就会发生一个异常。
异常是Python对象,表示一个错误。 当Python脚本发生异常时我们需要捕获处理它,否则程序会终止执行。 异常处理
捕捉异常可以使用try/except语句。 try/except语句用来检测try语句块中的错误,从而让except语句捕获异常信息并处理。 如果你不想在异常发生时结束你的程序,只需在try里捕获它。 语法: 以下为简单的try....except...else的语法: try:
<语句> #运行别的代码
except <名字>:
<语句> #如果在try部份引发了'name'异常
except <名字>,<数据>:
<语句> #如果引发了'name'异常,获得附加的数据
else:
<语句> #如果没有异常发生 try的工作原理是,当开始一个try语句后,python就在当前程序的上下文中作标记,这样当异常出现时就可以回到这里,try子句先执行,接下来会发生什么依赖于执行时是否出现异常。 如果当try后的语句执行时发生异常,python就跳回到try并执行第一个匹配该异常的except子句,异常处理完毕,控制流就通过整个try语句(除非在处理异常时又引发新的异常)。
如果在try后的语句里发生了异常,却没有匹配的except子句,异常将被递交到上层的try,或者到程序的最上层(这样将结束程序,并打印默认的出错信息)。
如果在try子句执行时没有发生异常,python将执行else语句后的语句(如果有else的话),然后控制流通过整个try语句。 使用except而不带任何异常类型
你可以不带任何异常类型使用except,如下实例: try:
正常的操作
......................
except:
发生异常,执行这块代码
......................
else:
如果没有异常执行这块代码 以上方式try-except语句捕获所有发生的异常。但这不是一个很好的方式,我们不能通过该程序识别出具体的异常信息。因为它捕获所有的异常。
使用except而带多种异常类型
你也可以使用相同的except语句来处理多个异常信息,如下所示: try:
正常的操作
......................
except(Exception1[, Exception2[,...ExceptionN]]]):
发生以上多个异常中的一个,执行这块代码
......................
else:
如果没有异常执行这块代码
try-finally 语句
try-finally 语句无论是否发生异常都将执行最后的代码。 try:
<语句>
finally:
<语句> #退出try时总会执行 try:
fh = open("testfile", "w")
fh.write("这是一个测试文件,用于测试异常!!")
finally:
print "Error: 没有找到文件或读取文件失败"
如果打开的文件没有可写权限,输出如下所示: try:
fh = open("testfile", "w")
try:
fh.write("这是一个测试文件,用于测试异常!!")
finally:
print "关闭文件"
fh.close()
except IOError:
print "Error: 没有找到文件或读取文件失败"
当在try块中抛出一个异常,立即执行finally块代码。 finally块中的所有语句执行后,异常被再次触发,并执行except块代码。 变量接收的异常值通常包含在异常的语句中。在元组的表单中变量可以接收一个或者多个值。 元组通常包含错误字符串,错误数字,错误位置。 触发异常
我们可以使用raise语句自己触发异常 raise语法格式如下:
raise [Exception [, args [, traceback]]]
语句中 Exception 是异常的类型(例如,NameError)参数标准异常中任一种,args 是自已提供的异常参数。
最后一个参数是可选的(在实践中很少使用),如果存在,是跟踪异常对象。 实例
一个异常可以是一个字符串,类或对象。 Python的内核提供的异常,大多数都是实例化的类,这是一个类的实例的参数。 定义一个异常非常简单,如下所示: def functionName( level ):
if level < 1:
raise Exception("Invalid level!", level)
# 触发异常后,后面的代码就不会再执行
注意:为了能够捕获异常,"except"语句必须有用相同的异常来抛出类对象或者字符串。 例如我们捕获以上异常,"except"语句如下所示: try:
正常逻辑
except Exception,err:
触发自定义异常
else:
其余代码
实例
#!/usr/bin/python
# -*- coding: UTF-8 -*- # 定义函数
def mye( level ):
if level < 1:
raise Exception,"Invalid level!"
# 触发异常后,后面的代码就不会再执行
try:
mye(0) # 触发异常
except Exception,err:
print 1,err
else:
print 2
执行以上代码,输出结果为: $ python test.py
1 Invalid level!
用户自定义异常
通过创建一个新的异常类,程序可以命名它们自己的异常。异常应该是典型的继承自Exception类,通过直接或间接的方式。

Python3 错误和异常
原文链接:
https://www.runoob.com/python3/python3-errors-execptions.html

异常
即便 Python 程序的语法是正确的,在运行它的时候,也有可能发生错误。运行期检测到的错误被称为异常。 大多数的异常都不会被程序处理,都以错误信息的形式展现在这里: 实例
>>> 10 * (1/0) # 0 不能作为除数,触发异常
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ZeroDivisionError: division by zero
>>> 4 + spam*3 # spam 未定义,触发异常
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'spam' is not defined
>>> '2' + 2 # int 不能与 str 相加,触发异常
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
异常以不同的类型出现,这些类型都作为信息的一部分打印出来: 例子中的类型有 ZeroDivisionError,NameError 和 TypeError。 错误信息的前面部分显示了异常发生的上下文,并以调用栈的形式显示具体信息。 异常处理
try/except
异常捕捉可以使用 try/except 语句。

 

 

以下例子中,让用户输入一个合法的整数,但是允许用户中断这个程序(使用 Control-C 或者操作系统提供的方法)。用户中断的信息会引发一个 KeyboardInterrupt 异常。

while True:
try:
x = int(input("请输入一个数字: "))
break
except ValueError:
print("您输入的不是数字,请再次尝试输入!")
try 语句按照如下方式工作; 首先,执行 try 子句(在关键字 try 和关键字 except 之间的语句)。 如果没有异常发生,忽略 except 子句,try 子句执行后结束。 如果在执行 try 子句的过程中发生了异常,那么 try 子句余下的部分将被忽略。如果异常的类型和 except 之后的名称相符,那么对应的 except 子句将被执行。 如果一个异常没有与任何的 except 匹配,那么这个异常将会传递给上层的 try 中。 一个 try 语句可能包含多个except子句,分别来处理不同的特定的异常。最多只有一个分支会被执行。 处理程序将只针对对应的 try 子句中的异常进行处理,而不是其他的 try 的处理程序中的异常。 一个except子句可以同时处理多个异常,这些异常将被放在一个括号里成为一个元组,例如: except (RuntimeError, TypeError, NameError):
pass
try/except...else
try/except 语句还有一个可选的 else 子句,如果使用这个子句,那么必须放在所有的 except 子句之后。 else 子句将在 try 子句没有发生任何异常的时候执行。
以下实例在 try 语句中判断文件是否可以打开,如果打开文件时正常的没有发生异常则执行 else 部分的语句,读取文件内容:

for arg in sys.argv[1:]:
try:
f = open(arg, 'r')
except IOError:
print('cannot open', arg)
else:
print(arg, 'has', len(f.readlines()), 'lines')
f.close()
使用 else 子句比把所有的语句都放在 try 子句里面要好,这样可以避免一些意想不到,而 except 又无法捕获的异常。 异常处理并不仅仅处理那些直接发生在 try 子句中的异常,而且还能处理子句中调用的函数(甚至间接调用的函数)里抛出的异常。例如: >>> def this_fails():
x = 1/0 >>> try:
this_fails()
except ZeroDivisionError as err:
print('Handling run-time error:', err) Handling run-time error: int division or modulo by zero
try-finally 语句
try-finally 语句无论是否发生异常都将执行最后的代码。
以下实例中 finally 语句无论异常是否发生都会执行:

实例
try:
runoob()
except AssertionError as error:
print(error)
else:
try:
with open('file.log') as file:
read_data = file.read()
except FileNotFoundError as fnf_error:
print(fnf_error)
finally:
print('这句话,无论异常是否发生都会执行。')
抛出异常
Python 使用 raise 语句抛出一个指定的异常。 raise语法格式如下: raise [Exception [, args [, traceback]]]
以下实例如果 x 大于 5 就触发异常:

x = 10
if x > 5:
raise Exception('x 不能大于 5。x 的值为: {}'.format(x))
执行以上代码会触发异常: Traceback (most recent call last):
File "test.py", line 3, in <module>
raise Exception('x 不能大于 5。x 的值为: {}'.format(x))
Exception: x 不能大于 5。x 的值为: 10
raise 唯一的一个参数指定了要被抛出的异常。它必须是一个异常的实例或者是异常的类(也就是 Exception 的子类)。 如果你只想知道这是否抛出了一个异常,并不想去处理它,那么一个简单的 raise 语句就可以再次把它抛出。 >>> try:
raise NameError('HiThere')
except NameError:
print('An exception flew by!')
raise An exception flew by!
Traceback (most recent call last):
File "<stdin>", line 2, in ?
NameError: HiThere
用户自定义异常
你可以通过创建一个新的异常类来拥有自己的异常。异常类继承自 Exception 类,可以直接继承,或者间接继承,例如: >>> class MyError(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value) >>> try:
raise MyError(2*2)
except MyError as e:
print('My exception occurred, value:', e.value) My exception occurred, value: 4
>>> raise MyError('oops!')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
__main__.MyError: 'oops!'
在这个例子中,类 Exception 默认的 __init__() 被覆盖。 当创建一个模块有可能抛出多种不同的异常时,一种通常的做法是为这个包建立一个基础异常类,然后基于这个基础类为不同的错误情况创建不同的子类: class Error(Exception):
"""Base class for exceptions in this module."""
pass class InputError(Error):
"""Exception raised for errors in the input. Attributes:
expression -- input expression in which the error occurred
message -- explanation of the error
""" def __init__(self, expression, message):
self.expression = expression
self.message = message class TransitionError(Error):
"""Raised when an operation attempts a state transition that's not
allowed. Attributes:
previous -- state at beginning of transition
next -- attempted new state
message -- explanation of why the specific transition is not allowed
""" def __init__(self, previous, next, message):
self.previous = previous
self.next = next
self.message = message
大多数的异常的名字都以"Error"结尾,就跟标准的异常命名一样。 定义清理行为
try 语句还有另外一个可选的子句,它定义了无论在任何情况下都会执行的清理行为。 例如: >>> try:
... raise KeyboardInterrupt
... finally:
... print('Goodbye, world!')
...
Goodbye, world!
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
KeyboardInterrupt
以上例子不管 try 子句里面有没有发生异常,finally 子句都会执行。 如果一个异常在 try 子句里(或者在 except 和 else 子句里)被抛出,而又没有任何的 except 把它截住,那么这个异常会在 finally 子句执行后被抛出。 预定义的清理行为
一些对象定义了标准的清理行为,无论系统是否成功的使用了它,一旦不需要它了,那么这个标准的清理行为就会执行。 这面这个例子展示了尝试打开一个文件,然后把内容打印到屏幕上: for line in open("myfile.txt"):
print(line, end="")
以上这段代码的问题是,当执行完毕后,文件会保持打开状态,并没有被关闭。 关键词 with 语句就可以保证诸如文件之类的对象在使用完之后一定会正确的执行他的清理方法: with open("myfile.txt") as f:
for line in f:
print(line, end="")
以上这段代码执行完毕后,就算在处理过程中出问题了,文件 f 总是会关闭。
Python assert(断言)

Python assert(断言)用于判断一个表达式,在表达式条件为 false 的时候触发异常。

断言可以在条件不满足程序运行的情况下直接返回错误,而不必等待程序运行后出现崩溃的情况
语法格式如下:

assert expression
等价于: if not expression:
raise AssertionError
assert 后面也可以紧跟参数: assert expression [, arguments]
等价于: if not expression:
raise AssertionError(arguments)


非常感谢菜鸟教程.
以下为网址:
https://www.runoob.com/ 以前学习的时候会经常翻阅,学习一些编程知识
已附上原文链接,推荐到原链接进行访问,毕竟显示效果会差很多

类 巩固小结
'''
1.当方法具有结构时,使用 class 比函数要好 2.类中定义的 方法 或 属性 如果没有声明权限
在类外使用实例化对象可以直接访问 3.类中的方法,第一个参数一定要写上 self ,self 是约定好的 4.析构方法通常调用不到,垃圾回收机制中引用计数后会自动销毁 5.写程序之前:
伪代码 小的程序->直接写流程
大项目-> 先分析结构 6.父类通常具有一些公共的方法,使用子类进行扩展
子类可以使用父类中定义的初始化方法 __init__ , 但是参数可能不全 7.找到最小的节点,将节点与节点之间发生的关系使用不同的类进行标识 8._xxx, __xxx 外界都不应该直接访问,放到类中的方法中使用 9.方法一定要先实现再进行优化 10.使用装饰器 @property 后 下面的方法会变为属性
@property
def function(self,args):
pass
类实例化对象.function 调用 function 方法 11.@staticmethod 静态方法,下面的方法声明为"内置方法"
不需要再使用 self 进行调用
@staticmethod
def function(object):pass 12.最小惊讶原则
让很多人一看就知道在写什么 13.注意代码的复杂度 '''
# 示例:
class stu(object):
# 继承 object 基类 a = 'a'
# 定义一个属性 def __init__(self,name,age):
# 初始化赋值
self.name = name
self.age = age def showName(self):
print(self.name) def __del__(self):
# 析构方法,通常自动销毁
del self.name
del self.age student = stu('张三',20)
student.showName()
print(student.a)
# 输出类中的属性
# a class stu_extends(stu):
""" 继承 stu 类"""
pass class lst_extends(list):
def append(self):
pass
# 继承列表类

模块小结

1.import 模块名(文件)
导入模块 2.查看模块内的方法
dir(模块名) 3.if __name__ == "__main__":
需要测试的语句 4.from 模块名 import 方法/属性/*
再使用时 使用 方法即可 不再使用模块名.方法 5.使用 __all__ = ["方法1","方法2","属性名1","属性名2"]
在导入 * 时,只导入该 all 中的方法或属性
在 __init__.py 文件第一行 6.import 模块名(文件) as 模块别名 7.搜索模块:
import sys
sys.path.append(r'绝对路径')
绝对路径指 导入的模块的 文件夹的位置 a.py 程序: import linecache
print(linecache.__file__)
def show( ):
print("我是 0411 的模块") 程序:
import linecache
# 导入模块
print(dir(linecache))
# 查看都具有哪些方法
'''
['__all__', '__builtins__', '__cached__',
'__doc__', '__file__', '__loader__', '__name__',
'__package__', '__spec__', 'cache', 'checkcache',
'clearcache', 'functools', 'getline', 'getlines',
'lazycache', 'os', 'sys', 'tokenize', 'updatecache']
'''
linecache.__file__
# 查看模块地址
# F:\Python IDLE\lib\linecache.py
from math import pi import sys
sys.path.append(r'D:\见解\Python\Python代码\学习\0411')
if __name__ == "__main__":
# 执行文件时,进行测试
print(linecache.__file__)
# F:\Python IDLE\lib\linecache.py print(pi)
# 使用 math 中的 pi 属性
# 3.141592653589793
import a
# 导入 0411 的 a.py 文件
a.show()
# 我是 0411 的模块

以下部分内容为对以前内容的巩固,部分存在重复现象.

异常 巩固1

'''
1.索引异常
IndexError: list index out of range 2.语法异常
SyntaxError 3.缩进异常
IndentationError: unexpected indent 4.try 语句完整形态:try except else finally 5.try 内的语句 出错之后不会运行出现异常之后的 try 内语句 6.开发某些功能时 任何地方都可能会出错
通常参数传递过来时
读取某些未知文件时
打开某个网页时 7.except 捕获正确的异常,对异常进行处理 程序:''' # lst = [1,2,3,4,5]
# print(lst[5])
# 索引异常,不存在下标为 5 的元素
# IndexError: list index out of range # print 444
# 语法异常
# SyntaxError
# print(444)
# 缩进异常
# IndentationError: unexpected indent lst = [1,2,3,4,5]
try :
print(lst[5])
print("出错之后不会运行出现异常之后的语句")
except IndexError as e :
'''try 出现异常时执行'''
print("出现索引异常")
else:
'''try 正常运行时执行'''
print("程序运行 OK, 没有问题")
finally:
print("无论是否出错一定会运行到 finally") # 出现索引异常
# 无论是否出错一定会运行到 finally

异常 巩固2

'''
1.找到可能会抛出异常的地方,仅对这几行代码进行异常处理 2.明确会出现的异常类型
缩进,类型,语法,索引等等 3.捕获出现的异常
import sys
exc = sys.exc_info()
exc[1] 为问题出现的原因 4.日志 logging 模块
import logging
logger = logging.getLogger()
# 获取日志对象
logfile = 'test.log'
hdlr = logging.FileHandler('senging.txt')
# 存储文件日志
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
# 以什么格式进行存储,时间,等级,日志信息
hdlr.setFormatter(formatter)
# 导入日志格式
logger.addHandler(hdlr)
# 将日志绑定
logger.setLevel(logging.NOTSET)
# 设置日志级别 5.断言 assert
assert 表达式,出错以后抛出的提示信息
表达式 : 1 > 4 3 > 2 1 == 2
断言绝对不能发生的错误,然后再处理异常 程序:''' import logging
logger = logging.getLogger()
# 获取日志对象
logfile = 'test.log'
hdlr = logging.FileHandler('senging.txt')
# 存储文件日志
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
# 以什么格式进行存储,时间,等级,日志信息
hdlr.setFormatter(formatter)
# 导入日志格式
logger.addHandler(hdlr)
# 将日志绑定
logger.setLevel(logging.NOTSET)
# 设置日志级别 import sys
try:
print(a)
except:
exc = sys.exc_info()
print(exc[1])
# 查看异常的问题
# name 'a' is not defined
print(exc[0])
# <class 'NameError'>
print(exc)
# (<class 'NameError'>, NameError("name 'a' is not defined"),
# <traceback object at 0x000002A8BD9DA188>)
logging.debug(exc[1])

附:日志这个还是很不错的,以下为显示的内容,会将错误保存起来,便于以后的查看
2020-07-25 09:51:01,344 DEBUG name 'a' is not defined

logging 日志基础

import logging
logger = logging.getLogger()
# 获取日志对象 logfile = 'test.log'
hdlr = logging.FileHandler('senging.txt')
# 存储文件日志 formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
# 以什么格式进行存储,时间,等级,日志信息 hdlr.setFormatter(formatter)
# 导入日志格式 logger.addHandler(hdlr)
# 将日志绑定 logger.setLevel(logging.NOTSET)
# 设置日志级别 注:此处只是显示了拥有哪些方法,具体实例还需要进行查阅相关资料

异常 巩固3

'''
1.with open("文件路径","模式") as fp:
操作 进入时 调用 __enter__ 方法
def __enter__(self):
print("开始执行 with 方法")
退出时 调用 __exit__ 方法
def __exit__(self,type,value,traceback):
print("退出 with 方法") 2.文件操作方法:
打开、读取、关闭
d = open('a','r')
d.read()
d.close() 3.可以自己定义异常,继承 Exception 类 程序:''' # 查看 with 执行的方法
class sth(object): def __enter__(self):
print("开始执行 with 方法") def __exit__(self,type,value,traceback):
print("退出 with 方法") with sth( ) as fp:
# with 自动关闭文件
pass # 自定义异常
class myException(Exception):
# 继承 Exception
def __init__(self,error,msg):
self.args = (error,msg)
self.error = error
self.msg = msg
try:
raise myException(1,'my exception')
except Exception as e :
print(str(e))
# (1, 'my exception')

多线程复习1
多线程复习1
'''
1.进程在运行时会创建一个主线程,每一个进程只有一个主线程 2.子进程 pid 唯一标识符 3.任意时间里,只有一个线程在运行,且运行顺序不能确定(全局锁限制) 4.threading.Thread(target = test,args = [i])
target = 函数名 ,args = [ 参数 ] 5.可以继承 threading.Thread 然后重写 run 方法
class myThread(threading.Thread):
def run():
pass 程序:''' import threading
# 导入线程库
def test():
print(1) a = threading.Thread(target = test)
# 创建 a 线程
b = threading.Thread(target = test)
a.start()
# 启动线程
b.start() a.join()
b.join()
# 结束之前使用 join 等待其他线程

import threading
# 导入线程库
import time
def test(i):
time.sleep(0.1)
print(i) tests_thread = []
for i in range(0,10):
threads = threading.Thread(target = test,args = [i])
# 创建 a 线程
tests_thread.append(threads) for i in tests_thread:
i.start()
# 启动线程 for i in tests_thread:
i.join()
# 结束之前使用 join 等待其他线程 print("线程结束") '''
运行结果
2
0
1
8
7
9
6
534 线程结束
'''

多线程复习2

import time

def func_a():
print("a 函数开始")
time.sleep(2)
print("a 函数结束") def func_b():
print("b 函数开始")
time.sleep(2)
print("b 函数结束") b_time = time.time()
func_a()
func_b()
print(time.time() - b_time)
# 查看运行多少秒
'''
运行结果:
a 函数开始
a 函数结束
b 函数开始
b 函数结束
4.00050163269043
'''
import threading
import time def func_a():
print("a 函数开始")
time.sleep(2)
print("a 函数结束") def func_b():
print("b 函数开始")
time.sleep(2)
print("b 函数结束") b_time = time.time() _a = threading.Thread(target = func_a)
_b = threading.Thread(target = func_b)
_a.start()
_b.start()
# 开始
_a.join()
_b.join()
# 等待
print(time.time() - b_time)
# 查看时间
'''
运行结果:
a 函数开始
b 函数开始
b 函数结束a 函数结束 2.001542091369629
'''
通过使用了线程和不使用线程的对比,使用了线程的要快很多
线程里面的加锁和释放锁

# 加锁和释放
import threading
mlock = threading.Lock()
# 创建一把锁, mlock
# 当存在死锁时,防止死锁 可重用锁
num = 0
def a():
global num
mlock.acquire()
# 加锁
num += 1
mlock.release()
# 释放锁
print(num)
for i in range(10):
d = threading.Thread(target = a)
d.start()

'''
1.协程,微型进程:
yield 生成器
yield 会保存声明的变量,可以进行迭代
使用 接收函数返回的对象.__next__()
next(接收函数返回的对象)
.send() 方法
传递给函数中 yield 声明的对象
x = yield i
会发送给 x 变量
如果一直没有使用 send() ,x 值一直为 None
赋值之后如果没有修改则 x 一直为 send 后的值 2.此时 x 的值为 None ,并没有将 i 赋值给 x
x = yield i
程序:''' # 创建一个包含 yield 声明的函数
def test_yield():
i = 0
a = 4
while i < a:
x = yield i
# x 通过 gener 进行赋值
i += 1 # 使用 .__next__() 查看迭代对象
gener = test_yield() print(gener.__next__())
# 0
print(gener.__next__())
# 1
print(next(gener))
# 2
gener.send("x 通过 gener 进行赋值")
for i in test_yield():
# i 在 test_yield 中 yield 声明的迭代对象中
print(i,end = " ")
# 0 1 2 3

解决素数(质数)

def is_sushu(int_num):
# 判断输入的数是否为质数
if int_num == 1:
return False
if int_num == 2:
return True
else:
for i in range(2,int_num):
if int_num % i == 0:
return False
return True def _get_sushu(max_num):
return [i for i in range(1,max_num) if is_sushu(i)]
# 使用列表推导式 if __name__ == "__main__":
a = _get_sushu(101)
# 返回判断素数的列表
print(a)

爬虫流程复习

设置爬虫终端:
URL 管理器 -> 网页下载器 -> 网页解析器 -> 产生价值数据
URL 管理器判断爬取网页链接
流程:
调度器询问 URL 管理器,是否存在要爬取的 URL
URL 管理器返回 是或否
调度器 从 URL 管理器中 取出一个 URL
URL 管理器 将 URL 传递给调度器
调度器将 URL 发送到下载器
下载器将 URL 下载的内容传递给调度器
调度器将 URL 下载的内容传递给解析器
解析器解析后传递给调度器
此时可以收集价值数据 调度器再将需要爬取的 URL 传递给 URL管理器 一直到没有需要爬取的 URL URL 管理器:
管理待爬取的 URL 集合和已经爬取的 URL 集合
使用管理器是为了防止重复抓取和防止重复抓取一个 URL URL 功能:
添加新的 URL 到待爬取的集合中
确定待添加的 URL 是否在 URL 中
获取待爬取的 URL
将 URL 从待爬取的移动到已爬取的集合中
判断是否还有待爬取的数据 URL 管理器实现方式:
将 待爬取的 和 已爬取的 URL 存储在集合中
set()
将 URL 存储在 关系数据库中,区分 URL 是待爬取还是已经爬取
MySQL urls(url,is_crawled)
缓存数据库 redis 网页下载器: 将 URL 对应的网页转换为 HTML 数据
存储到本地文件或者内存字符串中
requests 、 urllib 库实现下载
特殊情景处理器:
需要使用 Cookie 访问时:HTTPCookieProcessor
需要使用 代理 访问时:ProxyHandler
需要使用 加密 访问时:HTTPHandler
网页存在跳转关系访问时:HTTPRedirectHandler 网页解析器:
从网页提取有价值的数据
HTML 网页文档字符串
提取出价值数据
提取出新的 URL 列表
正则表达式 -> 模糊匹配
文档作为字符串,直接匹配
html.parser
BeautifulSoup -> 可以使用 html.parser 和 lxml
从 HTML 和 XHTML 中提取数据
语法:
创建 BeautifulSoup 对象
搜索节点 findall find
访问节点(名称,属性,文字) lxml
->结构化解析
DOM 树
进行上下级的遍历
html
head
title
文本
body
a
href
文本
div
文本 爬虫:
确定目标
分析目标
URL 格式
数据的链接
数据的格式
网页编码
编写代码
执行爬虫

列表常用方法复习:

列表的常用操作:
1.使用 索引下标 或 切片
查找对应元素的值
修改特定位置上的值
2.删除列表元素
del 对象
对象.pop(index=-1)
对象.remove(元素)
对象.clear() 3.查看列表长度
len(对象)
4.重复 n 次
对象 * n
5.拼接两个列表对象
对象 + 对象
6.查看某一个元素是否在对象中
元素 in 对象
7.列表作为可迭代对象使用
for i in 对象
for i in range(len(对象))
8.列表可以嵌套
[[],[],[]]
9.列表内元素类型可以是任意类型
[任意类型,任意类型]
10.查看列表中最大值 最小值
max(对象) min(对象)
11.将其他类型数据转换为列表对象
list(对象)
list(可迭代对象)
12.在尾部增加元素
整体添加 对象.append(元素)
解包添加 对象.extend(元素)
13.在任意位置添加
对象.insert(index,元素)
14.排序
对象.sort()
倒序 对象.reverse()
15.查看元素索引位置
对象.index(元素)

字典常用操作复习

字典的常用操作:
1.创建字典对象
dict.fromkeys(seq[,value])
dic = {key1:value1,key2,value2}
dic = dict(zip([keys],[values]))
2.使用 对象['键值'] 访问字典元素
3.修改字典
对象['键值'] = 对象
4.删除字典元素或字典对象
del 对象['键值']
del 对象
对象.clear()
对象.pop(key)
对象.popitem()
5.获取字典长度
len(对象)
6.复制字典
对象.copy()
对象.update(对象2)
7.获取指定键值的元素
对象.get(key[,default=None])
8.查看 键 是否在字典中
key in 对象
9.获取字典中的元素
对象.keys()
对象.values()
对象.items()
10.对某一个元素设置默认值 如果该键已有值 则设置无效
对象.setdefault(key,default = None)

将"089,0760,009"变为 89,760,9

remove_zeros = lambda s: ','.join(map(lambda sub: str(int(sub)), s.split(',')))

remove_zeros("089,0760,009")

Linux最常用的基本操作复习

1.ctrl + shift + =  放大终端字体
2.ctrl + - 缩小终端字体
3.ls 查看当前文件夹下的内容
4.pwd 查看当前所在的文件夹
5.cd 目录名 切换文件夹
6.touch 如果文件不存在 则创建文件
创建一个文档
7.mkdir 创建目录
创建一个文件夹
8.rm 删除指定的文件名
9.clear 清屏

python连接数据库 MySQLdb 版本

import MySQLdb
# 导入 MySQL 库 class MysqlMethod(object):
def __init__(self):
# 前提:能够连接上数据库
self.get_connect() def get_connect(self):
# 获取连接
try:
self.conn = MySQLdb.connect(
host = '127.0.0.1',
# 主机
user = 'root',
# 用户名
passwd = 'root',
# 密码
db = 'python_prac',
# 数据库
port = 3306,
# 端口号
charset = 'utf8'
# 避免字符编码问题
)
except MySQLdb.Error as e:
print("连接数据库时,出现错误")
print("错误信息如下:\n %s"%e)
else:
print("连接 MySQL 成功!") def close_connect(self):
# 关闭连接
try:
# 关闭连接
self.conn.close()
# 关闭数据库连接
except MySQLdb.Error as e:
print("关闭数据库时出现错误")
print("错误信息如下:\n %s"%e)
else:
print("退出成功,欢迎下次使用!") def get_onedata(self):
# 获取一条数据
cursor = self.conn.cursor()
# 获取游标
sql = 'select * from students where age between %s and %s'
# 查询语句
cursor.execute(sql,(15,25))
# execute(语句,(参数))
result = dict(zip([k[0] for k in cursor.description],cursor.fetchone()))
'''
zip(列表推导式,获取到的值)
字典的键:描述数据的值
字典的值:获取到的值
例:
lst_keys = ['a','b','c']
lst_values = [1,2,3]
dict(zip(lst_keys,lst_values))
得到的结果:
{'a': 1, 'b': 2, 'c': 3}
'''
# 元组类型转换为字典,便于通过索引查找数据
print("获取到一条数据:")
return result def get_moredata(self,page,page_size):
# 添加多条数据
offset = (page - 1) * page_size
# 起始位置
cursor = self.conn.cursor()
sql = 'select * from students where age between %s and %s limit %s,%s;'
cursor.execute(sql,(15,45,offset,page_size))
result = list(dict(zip([k[0] for k in cursor.description],row)) for row in cursor.fetchall())
''' 使用 zip 将 列名 和 获取到的数据 压缩为一个个单独的二元组
但类型为 <class 'zip'> 需要进行转换才能看到具体的值
zip([k[0] for k in cursor.description],row)
('id', 1)···
使用 dict 将 zip 类型转换为字典类型
dict(zip([k[0] for k in cursor.description],row))
{'id': 1,···}
使用 列表推导式 将每一个 row 变为查找到的多个数据中的一个
原理:[元素操作 for 元素 in 序列对象]
list -> []
list[ row 的操作 for row in 数据集]
'''
print("获取到多条数据:")
# result 为[{},{}] 形式
return result def insert_onedata(self):
# 添加一条数据
try:
sql = "insert into stu_0415(name,school) values (%s,%s);"
# 查询语句
cursor = self.conn.cursor()
# 获取游标
need_info = ('王五','厦大')
# 需要插入的数据
cursor.execute(sql,need_info)
# 运行 sql 语句
self.conn.commit()
# 提交,如果没有提交,数据库数据不会发生变化
except :
print("插入数据失败")
self.conn.rollback()
# 如果个别数据插入成功了,则也不算入数据库
print("插入数据成功") def main():
sql_obj = MysqlMethod()
# 创建一个 sql 对象
data = sql_obj.get_onedata()
# 获取一条数据
print(data) moredata = obj.get_moredata(1,5)
# 查看 0~5 的数据
for item in moredata:
print(item)
# 循环遍历输出字典对象
print("-------------")
obj.insert_onedata()
# 插入一条数据 if __name__ == '__main__':
main()
# 运行主程序

显示列表重复值

lst = [1,2,3,2,1,5,5]

lst = list(filter(lambda x:lst.count(x) != 1,lst))
此处使用了 filter 和 lambda 进行混合使用,
x 为 lst 中的元素

应用场景

列表常用场景:
存储不同类型的数据
任意类型均可
列表存储相同类型的数据
类 node结点 next、data
通过迭代遍历,在循环体内部(多为 while 内),对列表的每一项都进行遍历
树的深度遍历等等
列表推导式的使用等等 元组常用场景:
作为函数的参数和返回值
传递任意多个参数 *args
函数内为元组形式
一次返回多个数据
return (a,b) 或 a,b
return a,b,c
接收函数返回值时
value1,value2 = 函数(参数)
函数(参数)即为 return (a,b)
格式化字符串
s1 = "%s %s"
s2 = ('hello','world')
s1%s2
'hello world'
数据库execute语句
cursor.execute(sql,(15,25))
让列表不可以被修改,保护数据
tuple(list对象) 字典常用场景:
for in 遍历字典
dic = {'a':1,'b':2,'c':3}
遍历键值:
for key in dic.keys():
print(key,end = " ")
遍历值:
for value in dic.values():
print(value,end = " ")
遍历键值对:
for key,value in dic.items():
print(key,":",value,end = " ")
使用字典(多个键值对)存储一个物体的信息
{"name":"张三","age":23}
将多个字典放到列表中,循环时对每一个字典进行相同操作
students_info = [{"name":"张三","age":23},{"name":"李四","age":22}]
访问张三数据:
students_info[0]['name']
操作 for i in range(len(students_info)):
students_info[i] 即可进行操作数据
for stu in students_info:
print(stu) 输出的为单个字典元素

类 扩展

关于类和对象的理解:
类 -> 设计图纸,设计应该具有哪些属性和行为
对象 -> 使用图纸制造出来的模型 类中定义普通方法,第一个参数为 self
self可以修改为别的,但最好还是不要改变,约定好的
self.属性 self.方法 调用 self 指向的对象的属性和行为 在类外可以为实例化对象直接创建属性,但是该属性只适用于该对象
不推荐使用,如果一定要使用,必须先创建属性,后使用方法 在 __init__(self,..) 初始化方法内,定义属性初始值有利于表达属性,定义方法 打印类的实例化对象时,实际调用的是 类中的 __str__方法
__str__必须返回字符串,可以自己定义 如果实例化对象 先使用 del 方法删除了,那么不会再执行类中的 __del__方法 保护私有公有对象,保护私有共有方法 在方法内都可以调用 先开发被使用的类,被包含操作的类

复习 装饰器

'''
装饰器的作用
引入日志
函数执行时间的统计
执行函数前预备处理
执行函数后清理功能
权限校验等场景
缓存
''' # 定义一个函数,遵循闭包原则(函数作为参数)
def decorator(func):
'''定义一个装饰器函数'''
print("func 函数开始")
def wrapper():
# 创建装饰器内容
print("进行装饰")
func()
print("装饰完毕")
print("func 函数结束")
return wrapper @decorator
# 加载 wrapper 函数,将 wrapper 函数传递给使用装饰器的函数
def house():
print("大房子") house()
'''
运行结果:
func 函数开始
func 函数结束
进行装饰
大房子
装饰完毕
'''

re 正则表达式练习
字符串重复出现 '''
有一段英文文本,其中有单词连续重复了 2 次,编写程序检查重复的单词并只保留一个
例: This is a a desk.
输出 This is a desk.
''' # 方法一
import re
x = 'This is a a desk.'
# 设置字符串
pattern = re.compile(r'\b(\w+)(\s+\1){1,}\b')
# \b 匹配单词和空格间的位置
# \w 匹配包括下划线的任何单词字符 [A-Za-z0-9_]
# \s 匹配任何空白字符
# {1,} 大于 1 个
matchResult = pattern.search(x)
# 查找这样的结果
x = pattern.sub(matchResult.group(1),x)
# sub 进行替换字符
# group(1) 为 a group(0) 为 a a
print(x)
# This is a desk. # 方法二
import re
x = 'This is a a desk.'
# 设置字符串
pattern = re.compile(r'(?P<f>\b\w+\b)\s(?P=f)')
# # \b 匹配单词和空格间的位置
# \w 匹配包括下划线的任何单词字符 [A-Za-z0-9_]
matchResult = pattern.search(x)
# 匹配到 a a
x = x.replace(matchResult.group(0),matchResult.group(1))
# 字符串对象.replace(旧字符串,新字符串)
# print(matchResult.group(0))
# a a
# print(matchResult.group(1))
# a
print(x)
# This is a desk.

最基本的Tkinter界面操作

'''
1.创建应用程序主窗口对象
root = Tk() 2.在主窗口中,添加各种可视化组件
btn1 = Button(root)
btn1["text"] = "点我" 3.通过几何布局管理器,管理组件得大小和位置
btn1.pack() 4.事件处理
通过绑定事件处理程序,响应用户操作所触发的事件
def songhua(e):
messagebox.showinfo("Message","送你一朵玫瑰花")
print("送花花")
btn1.bind("<Button-1>",songhua) 5.Tk() 的对象.mainloop() 方法会一直进行事件循环,监听用户操作 6.Button() 组件的参数为 Tk() 对象
Button() 的实例化对象 ["text"] 内容为显示在按钮上的内容 7.from tkinter import messagebox 显示点击之后提示的窗口
messagebox.showinfo("Message","送你一朵玫瑰花")
第一个参数为 标题
第二个参数为 显示信息 8.btn1.bind("<Button-1>",songhua)
使用创建好的按钮对象绑定鼠标事件和对应需要运行的函数 9.root.mainloop() 事件循环,一直监听用户操作 程序:''' from tkinter import *
from tkinter import messagebox
root = Tk()
# 创建一个窗口对象 btn1 = Button(root) btn1["text"] = "Submit" btn1.pack()
# 将组件对象合理的放在窗口中
def songhua(e):
# e 为事件 event
messagebox.showinfo("Message","送你一朵玫瑰花")
print("送花花")
btn1.bind("<Button-1>",songhua)
# <Button-1> 表示鼠标左键单击
root.mainloop()
# root.mainloop() 事件循环,一直监听用户操作

Tkinter经典写法

'''
1.继承 tkinter.Frame 类,实现类的基本写法 2.创建 主窗口 及 主窗口大小 位置 及 标题 3.将需要添加的组件放入到类中进行创建,
继承的 Frame 类需要使用 master 参数作为父类的初始化使用 4.初始化时,将属性和方法都进行初始化,此时可以将 GUI 程序所要实现的功能确定好 5.在类中定义事件发生时,需要实现的功能 6.self.btn1["command"] = self.kuaJiang
btn1["command"] 为事件发生时进行相应的函数 7.self.btnQuit = Button(self,text = "退出",command = root.destroy)
退出按钮的写法
''' from tkinter import *
from tkinter import messagebox class Application(Frame):
'''GUI程序经典写法'''
def __init__(self,master = None):
super().__init__(master)
# super() 表示父类的定义,父类使用 master 参数
self.master = master
# 子类定义一个属性接收传递过来的 master 参数
self.pack()
# .pack 设置布局管理器
self.createWidget()
# 在初始化时,将按钮也实现
# master传递给父类 Frame 使用后,子类中再定义一个 master 对象 def createWidget(self):
'''创建组件'''
self.btn1 = Button(self)
# self 为组件容器
self.btn1["text"] = "Hany love Python."
# 按钮的内容为 btn1["text"]定义的内容
self.btn1.pack()
# 最佳位置
self.btn1["command"] = self.kuaJiang
# 响应函数 self.btnQuit = Button(self,text = "退出",command = root.destroy)
# 设置退出操作
self.btnQuit.pack() def kuaJiang(self):
messagebox.showinfo("人艰不拆","继续努力,你是最棒的!") if __name__ == '__main__':
root = Tk()
# 定义主窗口对象
root.geometry("200x200+200+300")
# 创建大小
root.title("GUI 经典写法") app = Application(master = root)
# 传递 master 参数为 主窗口对象
root.mainloop()

Label 组件基本写法

'''
1.width,height 指定区域大小
文本 汉字 2 个字节 2.font 指定字体和字体大小
font(font_name,size) 3.image 显示在 Label 上的图像 支持 gif 格式 4.fg 前景色 5.bg 背景色 6.justify 针对多行文字的对齐
left center right 7.self.lab1 = Label(self,text = "Label实现",width = 10,height = 2,
bg = 'black',fg = 'white') 8. photo_gif = PhotoImage(file = "images/小熊.gif")
self.lab3 = Label(self,image = photo_gif)
将照片传递给 photo_gif 然后使用 Label 将图片变量作为参数进行传递 9.self.lab4 = Label(self,text = " Hany加油\n 人艰不拆!"
,borderwidth = 1,relief = "solid",justify = "right")
borderwidth 设置文本线的宽度 justify 表示左对齐 右对齐 ''' from tkinter import * class Application(Frame):
'''GUI程序经典写法'''
def __init__(self,master = None):
super().__init__(master)
# super() 表示父类的定义,父类使用 master 参数
self.master = master
# 子类定义一个属性接收传递过来的 master 参数
self.pack()
# .pack 设置布局管理器
self.createWidget()
# 在初始化时,将按钮也实现
# master传递给父类 Frame 使用后,子类中再定义一个 master 对象 def createWidget(self):
'''创建组件'''
self.lab1 = Label(self,text = "Label实现",width = 10,height = 2,
bg = 'black',fg = 'white')
self.lab1.pack()
self.lab2 = Label(self,text = "Labe2实现",width = 10,height = 2,
bg = 'black',fg = 'white',font = ("宋体",14))
self.lab2.pack()
# 显示图像
global photo_gif
# 将 photo_gif 设置为全局变量,防止方法调用后销毁
photo_gif = PhotoImage(file = "路径/图片名.gif")
self.lab3 = Label(self,image = photo_gif)
self.lab3.pack() # 显示多行文本
self.lab4 = Label(self,text = " Hany加油\n 人艰不拆!"
,borderwidth = 1,relief = "solid",justify = "right")
self.lab4.pack() if __name__ == '__main__':
root = Tk()
# 定义主窗口对象
root.geometry("300x300+400+300")
# 创建大小
root.title("Label 测试")
# 设置标题
app = Application(master = root)
# 传递 master 参数为 主窗口对象
root.mainloop()

注:图片路径和图片要修改为自己的

类实例化的对象调用的方法或属性来自于类的哪个方法中

__init__ 构造方法 对象创建 p = Person()
__del__ 析构方法 对象回收
__repr__ , __str__ 打印,转换 print(a)
__call__ 函数调用 a()
__getattr__ 点号运算 a.xxx
__setattr__ 属性赋值 a.xxx = value
__getitem__ 索引运算 a[key]
__setitem__ 索引赋值 a[key]=value
__len__ 长度 len(a)
每个运算符实际上都对应了相应的方法

运算符+ __add__ 加法
运算符- __sub__ 减法
<,<=,== __lt__,__le__,__eq__ 比较运算符
>,>=,!= __gt__,_ ge__,__ne__ 比较运算符
|,^,& __or__,__xor__,__and__ 或,异或,与
<<,>>__lshift__,__ rshift__ 左移,右移
*,/,%,// __mul__,__truediv__,__mod__,__floordiv__ 乘,浮点除,模运算(取余),整数除
** __pow__ 指数运算

tkinter Button基本用语

'''
1.self.btn2 = Button(root,image = photo,command = self.login)
使用 image 图片作为按钮,command 作为响应 2.self.btn2.config(state = "disabled")
对按钮进行禁用
3.Button 中 anchor 控制按钮上的图片位置
N NE E SE SW W NW CENTER
默认居中 '''
from tkinter import *
from tkinter import messagebox class Application(Frame):
'''GUI程序经典写法'''
def __init__(self,master = None):
super().__init__(master)
# super() 表示父类的定义,父类使用 master 参数
self.master = master
# 子类定义一个属性接收传递过来的 master 参数
self.pack()
# .pack 设置布局管理器
self.createWidget()
# 在初始化时,将按钮也实现
# master传递给父类 Frame 使用后,子类中再定义一个 master 对象 def createWidget(self):
'''创建组件'''
self.btn1 = Button(root,text = '登录',command = self.login,
width = 5,height = 2,anchor = E)
# command 进行操作的函数
self.btn1.pack() global photo
photo = PhotoImage(file = "图片路径/图片名.gif")
self.btn2 = Button(root,image = photo,command = self.login)
self.btn2.pack() # self.btn2.config(state = "disabled")
# # 设置按钮为禁用按钮 def login(self):
messagebox.showinfo("博客园","欢迎使用~") if __name__ == '__main__':
root = Tk()
# 定义主窗口对象
root.geometry("300x300+400+300")
# 创建大小
root.title("Button 测试")
# 设置标题
app = Application(master = root)
# 传递 master 参数为 主窗口对象
root.mainloop()

tkinter Entry基本用法

'''
1.BooleanVar() 布尔类型 2.IntVar() 整数类型 3.DoubleVar() 浮点数类型 4.StringVar() 字符串类型 5.self.entry1 = Entry(self,textviable = v1)
textviable 实现双向关联 6.v1.set("admin")
# 设置单行文本的值 7.v1.get() self.entry1.get() 获取的是单行文本框中的值 8.self.entry_passwd = Entry(self,textvariable = v2,show = "*")
textvariable 进行绑定 v2
v2 = StringVar()
用户输入后,show 显示为 * 9.Button(self,text = "登录",command = self.login).pack()
登录操作 10.点击登陆后执行的函数可以与数据库进行交互,达到验证的目的
self.组件实例化对象.get() 获取值 '''
from tkinter import *
from tkinter import messagebox class Application(Frame):
'''GUI程序经典写法'''
def __init__(self,master = None):
super().__init__(master)
# super() 表示父类的定义,父类使用 master 参数
self.master = master
# 子类定义一个属性接收传递过来的 master 参数
self.pack()
# .pack 设置布局管理器
self.createWidget()
# 在初始化时,将按钮也实现
# master传递给父类 Frame 使用后,子类中再定义一个 master 对象 def createWidget(self):
'''创建组件'''
self.lab1 = Label(self,text = "用户名")
self.lab1.pack()
# StringVar() 绑定到指定的组件,StringVar 和 v1 一起变化
v1 = StringVar()
self.entry_user = Entry(self,textvariable = v1)
self.entry_user.pack()
v1.set("admin")
# 设置单行文本的值
# v1.get() self.entry_user.get() 获取的是单行文本框中的值 # 创建密码框
self.lab2 = Label(self,text = "密码")
self.lab2.pack() v2 = StringVar()
self.entry_passwd = Entry(self,textvariable = v2,show = "*")
self.entry_passwd.pack() Button(self,text = "登录",command = self.login).pack() def login(self):
username = self.entry_user.get()
passwd = self.entry_passwd.get()
# 数据库进行操作,查看是否存在该用户
print("用户名:" + username)
print("密码:" + passwd)
if username == "Hany" and passwd == "123456":
messagebox.showinfo("博客园","欢迎使用~")
else:
messagebox.showinfo("Error","请重新输入~") if __name__ == '__main__':
root = Tk()
# 定义主窗口对象
root.geometry("300x300+400+300")
# 创建大小
root.title("Button 测试")
# 设置标题
app = Application(master = root)
# 传递 master 参数为 主窗口对象
root.mainloop()
此处的用户名是 Hany , 密码是 123456

Text多行文本框基本用法

#coding=gbk

'''
1.Text(root,width,height,bg)
主窗口,宽度,高度,背景色 2.使用 .insert() 方法添加内容
Text 对象.insert(几行.几列,"内容")
w1.insert(2.3,"···")
END 为最后位置
self.w1.insert(END,'[end]') 3.Button(窗口对象,text = "内容",command = "self.函数名").pack([side = "left"])
Button(self,text = "返回文本",command = self.returnText).pack(side = "left")
text 显示的内容 command 运行的函数 pack 位置,使用 side 后,按钮按照 pack 来 4.在类中定义的属性,不会因为运行函数方法后,就销毁
self.photo 不用再使用 global 进行声明 5.使用 PhotoImage 将图片存起来后,将图片显示在多行文本 Text 中
self.photo = PhotoImage(file = '图片路径/图片名.gif')
self.photo = PhotoImage(file = 'images/logo.gif')
使用 .image_create(位置,image = self.photo) 进行添加
self.w1.image_create(END,image = self.photo) 6.添加按钮组件到文本中
btn1 = Button(文本内容,text = "内容") 7.self.w1.tag_config (内容,background 背景颜色,foreground 文字颜色) 8.self.w1.tag_add("内容",起始位置,终止位置)
tag_add 加入内容 9.self.w1.tag_bind("内容","事件",self.函数名)
self.w1.tag_bind("baidu","<Button-1>",self.webshow) 10.webbrowser.open("网址")
打开一个网址 ''' from tkinter import *
from tkinter import messagebox
# 显示消息
import webbrowser
# 导入 webbrowser 到时候点击字体跳转使用 class Application(Frame):
'''GUI程序经典写法'''
def __init__(self,master = None): super().__init__(master)
# super() 表示父类的定义,父类使用 master 参数
self.master = master
# 子类定义一个属性接收传递过来的 master 参数
self.pack()
# .pack 设置布局管理器
self.createWidget()
# 在初始化时,将按钮也实现
# master传递给父类 Frame 使用后,子类中再定义一个 master 对象 def createWidget(self):
'''创建组件'''
# 创建文字 Text(root 主窗口对象,width 宽度,height 高度,bg 背景色)
# 只对于文本有效
self.w1 = Text(root,width = 100,height = 40,bg = "gray")
# 设置背景色 bg = "gray"
self.w1.pack() self.w1.insert(1.0,"0123456789\nabcdefg")
# 1.0 在 第一行 第零列 插入数据
self.w1.insert(2.3,"活在当下\n结发为夫妻,恩爱两不疑\n言行在于美,不在于多")
# 2.3 在 第二行 第三列 Button(self,text = "重复插入文本",command = self.insertText).pack(side = "left")
# 水平排列 side = "left"
Button(self,text = "返回文本",command = self.returnText).pack(side = "left")
Button(self,text = "添加图片",command = self.addImage).pack(side = "left")
Button(self,text = "添加组件",command = self.addWidget).pack(side = "left")
Button(self,text = "通过 tag 控制文本",command = self.testTag).pack(side = "left") def insertText(self):
'''INSERT 索引表示在光标处插入'''
self.w1.insert(INSERT,'Hany')
# END 索引号表示在最后插入
self.w1.insert(END,'[end]')
# 在文本区域最后
self.w1.insert(1.2,"(.-_-.)") def returnText(self):
'''返回文本内容'''
# Indexes(索引) 是用来指向 Text 组件中文本的位置
# Text 的组件索引 也是对应实际字符之间的位置
# 核心:行号从 1 开始,列号从 0 开始
print(self.w1.get(1.2,1.6)) print("文本内容:\n" + self.w1.get(1.0,END)) def addImage(self):
'''增加图片'''
self.photo = PhotoImage(file = 'images/logo.gif')
self.w1.image_create(END,image = self.photo) def addWidget(self):
'''添加组件'''
btn1 = Button(self.w1,text = "Submit")
self.w1.window_create(INSERT,window = btn1)
# 添加组件 def testTag(self):
'''将某一块作为特殊标记,并使用函数'''
self.w1.delete(1.0,END)
self.w1.insert(INSERT,"Come on, you're the best.\n博客园\nHany 加油!!!")
# self.w1.tag_add("good",1.0,1.9)
# 选中标记区域
# self.w1.tag_config("good",background = "yellow",foreground = "red")
# 单独标记某一句,背景色 字体色
self.w1.tag_add("baidu",3.0,3.4)
#
self.w1.tag_config("baidu",underline = True,background = "yellow",foreground = "red")
self.w1.tag_bind("baidu","<Button-1>",self.webshow) def webshow(self,event):
webbrowser.open("http://www.baidu.com") if __name__ == '__main__':
root = Tk()
# 定义主窗口对象
root.geometry("500x300+400+300")
# 创建大小
root.title("Button 测试")
# 设置标题
app = Application(master = root)
# 传递 master 参数为 主窗口对象
root.mainloop()

Radiobutton基础语法

'''
1.Radiobutton(root 主窗口,text 文本内容,value 值(可以通过set 和 get 获取到的值),variable 变量修改原来的StringVar)
self.radio_man = Radiobutton(root,text = "男性",value = "man",variable = self.v)
2.Button(root,text = "提交",command = self.confirm).pack(side = "left")
设置按钮进行提交,然后响应的函数 '''
from tkinter import *
from tkinter import messagebox class Application(Frame):
'''GUI程序经典写法'''
def __init__(self,master = None):
super().__init__(master)
# super() 表示父类的定义,父类使用 master 参数
self.master = master
# 子类定义一个属性接收传递过来的 master 参数
self.pack()
# .pack 设置布局管理器
self.createWidget()
# 在初始化时,将按钮也实现
# master传递给父类 Frame 使用后,子类中再定义一个 master 对象 def createWidget(self):
'''创建组件'''
self.v = StringVar()
#String类型
self.v.set("man")
# 默认为 man 选中
self.radio_man = Radiobutton(self,text = "男性",value = "man",variable = self.v)
# Radiobutton(root/self 主窗口,text 文本内容,value 值(可以通过set 和 get 获取到的值),variable 变量修改原来的StringVar()变量也修改)
self.radio_woman = Radiobutton(self,text = "女性",value = "woman",variable = self.v) self.radio_man.pack(side = "left")
self.radio_woman.pack(side = "left")
# 放到最佳位置
Button(self,text = "提交",command = self.confirm).pack(side = "left")
# 设置按钮进行提交,然后响应的函数 def confirm(self):
messagebox.showinfo("选择结果","选择的性别是 : "+self.v.get())
# 两个参数,一个是标题另一个是内容
# 显示内容 if __name__ == '__main__':
root = Tk()
# 定义主窗口对象
root.geometry("300x100+400+300")
# 创建大小
root.title("Button 测试")
# 设置标题
app = Application(master = root)
# 传递 master 参数为 主窗口对象
root.mainloop()

Checkbutton基本写法

'''
1.Checkbutton(self 窗口对象,text 按钮显示内容,variable 绑定变量->一起变化,
onvalue 用户点击时得到的值,offvalue 没有点击得到的值)
self.choose1 = Checkbutton(self,text = "玩游戏",variable = self.playHobby,
onvalue = 1,offvalue = 0)
2.self.playHobby.get() == 1 :
.get() 获取到值 判断是否时 onvalue 的值
''' from tkinter import *
from tkinter import messagebox class Application(Frame):
'''GUI程序经典写法'''
def __init__(self,master = None):
super().__init__(master)
# super() 表示父类的定义,父类使用 master 参数
self.master = master
# 子类定义一个属性接收传递过来的 master 参数
self.pack()
# .pack 设置布局管理器
self.createWidget()
# 在初始化时,将按钮也实现
# master传递给父类 Frame 使用后,子类中再定义一个 master 对象 def createWidget(self):
'''创建组件''' self.playHobby = IntVar()
# 默认为 0
# .get() 获取值 .set() 设置值
self.travelHobby = IntVar()
self.watchTvHobby = IntVar() # print(self.playHobby.get()) 0 self.choose1 = Checkbutton(self,text = "玩游戏",variable = self.playHobby,
onvalue = 1,offvalue = 0)
# Checkbutton(self 窗口对象,text 按钮显示内容,variable 绑定变量->一起变化,
# onvalue 用户点击时得到的值,offvalue 没有点击得到的值)
self.choose2 = Checkbutton(self,text = "去旅游",variable = self.travelHobby,
onvalue = 1,offvalue = 0)
self.choose3 = Checkbutton(self,text = "看电影",variable = self.watchTvHobby,
onvalue = 1,offvalue = 0) self.choose1.pack(side = "left")
self.choose2.pack(side = "left")
self.choose3.pack(side = "left") Button(self,text = "确定",command = self.confirm).pack(side = "left") def confirm(self): if self.playHobby.get() == 1 :
# 获取到的数据是 1 的话,进行接下来的操作
messagebox.showinfo("假期项目","玩游戏----")
if self.travelHobby.get() == 1 :
messagebox.showinfo("假期项目","去旅游----")
if self.watchTvHobby.get() == 1 :
messagebox.showinfo("假期项目","看电影----") if __name__ == '__main__':
root = Tk()
# 定义主窗口对象
root.geometry("300x200+400+300")
# 创建大小
root.title("Button 测试")
# 设置标题
app = Application(master = root)
# 传递 master 参数为 主窗口对象
root.mainloop()

# 一行代码合并字典
# {**{'键':'值','键':'值'},**{'键','值'}}
dic = {**{'a':1,'b':2},**{'c':3},**{'d':4}}
print(dic)
# {'a': 1, 'b': 2, 'c': 3, 'd': 4} # 一行代码查看多个列表最大值
print(max([[1,2,3],[4,5,7,8],[6]],key = lambda v:max(v)))
# [4, 5, 7, 8]
print(max(max([[1,2,3],[4,5,7,8],[6]],key = lambda v:max(v))))
# 8

整理上课内容

加载数据集
sklearn.datasets 集成了部分数据分析的经典数据集·
load_boston 回归
load_breast_cancer 分类 聚类
fetch_california_housing 回归
load_iris 分类 聚类
load_digits 分类
load_wine 分类 from sklearn.datasets import load_breast_cancer
cancer=load_ breast_cancer()
print('breast_cancer数据集的长度为:',len(cancer))
print('breast_cancer数据集的类型为:',type(cancer))
数据集可以看作字典
可以使用 data target feature_names DESCR
分别获取数据集的数据 标签 特征名称 描述信息
cancer['data'] cancer['target']
cancer['feature_names'] cancer['DESCR'] 将样本分为三部分
训练集(train set)用于估计模型
验证集(validation set) 用于确定 网络结构 或 控制模型复杂程度 的参数
测试集(test set) 检验最优的模型的性能
占比
50% 25% %25
通过一些数据建立一些模型 通过模型可以将新数据分组 K折交叉验证法
常用的方法是留少部分做测试集
对其余 N 个样本采用 K 折交叉验证法
将样本打乱 均匀分成K份。
轮流选择其中 K-1 份做训练 剩余的一份做验证。
计算预测误差平方和 把K次的预测误差平方和的均值作为选择最优模型结构的依据 对数据集进行拆分
sklearn.model_selection 的 train_test_split 函数
参数
*arrays 接收一个或多个需要划分的数据集
分类->数据和标签
聚类->数据
test_size 接收 float int None 数据
表示测试集的大小
float 类型 0-1 之间 表示测试集在总数中的占比
int 类型 表示测试集的绝对数目
test_size 默认为 25%
train_size 接收 float int None 类型的数据
表示训练集的大小 和 test_size 只能有一个
random_state 接收 int 类型 表示随机种子的编号
相同随机种子编号产生相同的随机结果
不同的随机种子编号产生不同的随机结果
shuffle 接收布尔类型 代表是否进行有放回抽样’
stratify 接收 array标签 或者 None
使用标签进行分层抽样 train_test_split 函数根据传入的数据
分别将传入的数据划分为训练集和测试集
如果传入的是1组数据,那么生成的就是这一组数据随机划分后训练集和测试集
如果传入的是2组数据,则生成的训练集和测试集分别2组 将breast_cancer数据划分为训练集和测试集
from sklearn.model_selection import train_test_split
cancer_data_train,cancer_data_test,cancer_target_train,cancer_target_test
= train_test_split(cancer_data,cancer_target,test_size=0.2,random_state=42)
.shape 查看形状 numpy.max() 查看最大值 使用 sklearn 转换器
fit 分析特征和目标值,提取有价值的信息 如 统计量 或 权值系数等。
transform 对特征进行转换
无信息转换 指数和对数函数转换等
有信息转换
无监督转换
只利用特征的统计信息 如 标准化 和 PCA 降维
有监督转换
利用 特征信息 和 目标值信息 如通过模型->选择特征 LDA降维等
fit_tranform 先调用 fit 方法 然后调用 transform 方法 使用 sklearn 转换器 能够实现对传入的 Numpy数组
进行标准化处理、归一化处理、二值化处理、PCA降维等操作
注 各类特征处理相关的操作都要将 训练集和测试集 分开
将训练集的操作规则、权重系数等应用到测试集中 .shape 查看形状 sklearn 提供的方法
MinMaxScaler 对特征进行离差标准化
StandardScaler 对特征进行标准差标准化
Normalizer 对特征进行归一化
Binarizer 对定量特征进行二值化处理
OneHotEncoder 对定性特征进行独热编码处理
Function Transformer 对特征进行自定义函数变换 from sklearn.decomposition import PCA
PCA 降维算法常用参数及作用
n_components 接收 None int float string 参数
未指定时,代表所有特征都会保留下来
int -> 降低到 n 个维度
float 同时 svd_solver 为full
string 如 n_components='mle'
自动选取特征个数为 n 满足所要求的方差百分比 默认为 None copy 接收 布尔类型数据
True 运行后 原始数据不会发生变化
False 运行 PCA 算法后,原始数据 会发生变化
默认为 True whiten 接收 布尔类型数据
表示白化 对降维后的数据的每个特征进行归一化
默认为 False svd_solver 接收 'auto' 'full' 'arpack' 'randomized'
默认为auto
auto 代表PCA类会自动在上述三种算法中去权衡 选择一个合适的SVD算法来降维
full 使用SciPy库实现的传统SVD算法
arpack 和randomized的适用场景类似
区别是 randomized 使用的是 sklearn 的SVD实现
而arpack直接使用了 SciPy 库的 sparse SVD实现
randomized 一般适用于数据量大 数据维度多 同时主成分数目比例又较低的PCA降维 使用一些加快SVD的随机算法 聚类分析 在没有给定 划分类别 的情况下,根据 数据相似度 进行样本分组的一种方法
聚类模型 可以将 无类标记的数据 聚集为多个簇 视为一类 是一种 非监督的学习算法
聚类的输入是 一组未被标记的样本
根据 自身的距离 或 相似度 将他们划分为若干组
原则 组内样本最小化 组间距离最大化 常用的聚类算法
划分方法
K-Means算法(K-平均)
K-MEDOIDS算法(K-中心点)
CLARANS算法(基于选择的算法) 层次分析方法
BIRCH算法(平衡送代规约和聚类)
CURE算法(代表点聚类)
CHAMELEON算法(动态模型) 基于密度的方法
DBSCAN算法(基于高密度连接区域)
DENCLUE算法(密度分布函数)
OPTICS算法(对象排序识别) 基于网格的方法
STING算法(统计信息网络)
CLIOUE算法(聚类高维空间)
WAVE-CLUSTER算法(小波变换) sklearn.cluster 提供的聚类算法
函数名称 K-Means
参数 簇数
适用范围 样本数目很大 聚类数目中等
距离度量 点之间的距离 函数名称 Spectral clustering
参数 簇数
适用范围 样本数目中等 聚类数目较小
距离度量 图距离 函数名称 Ward hierarchical clustering
参数 簇数
适用范围 样本数目较大 聚类数目较大
距离度量 点之间的距离 函数名称 Agglomerative clustering
参数 簇数 链接类型 距离
适用范围 样本数目较大 聚类数目较大
距离度量 任意成对点线图间的距离 函数名称 DBSCAN
参数 半径大小 最低成员数目
适用范围 样本数目很大 聚类数目中等
距离度量 最近的点之间的距离 函数名称 Birch
参数 分支因子 阈值 可选全局集群
适用范围 样本数目很大 聚类数目较大
距离度量 点之间的欧式距离 聚类算法实现需要sklearn估计器 fit 和 predict
fit 训练算法 接收训练集和标签
可用于有监督和无监督学习
predict 预测有监督学习的测试集标签
可用于划分传入数据的类别
将规则通过 fit 训练好后 将规则进行 预测 predict
如果存在数据 还可以检验规则训练的好坏 引入离差标准化
from sklearn.preprocessing import MinMaxScaler from sklearn.datasets import load_iris
from sklearn.cluster import K-Means
iris = load_iris() 数据集的特征
iris_data = iris['data'] 数据集的标签
iris_target = iris['target'] 数据集的特征名
iris_names = iris['feature_names'] 训练规则
scale = MinMaxScaler().fit(iris_data) 应用规则
iris_dataScale = scale.transform(iris_data) 构建并训练模型
kmeans = KMeans(n_components = 3,random_state = 123).fit(iris_dataScale)
n_components = 3 分为三类 预测模型
result = kmeans.predict([[1.5,1.5,1.5,1.5]]) 查看预测类别
result[0] 使用 sklearn.manifold 模块的 TSNE 函数
实现多维数据的可视化展现
原理 使用 TSNE 进行数据降维
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.manifold import TSNE
使用 TSNE 进行数据降维 降为两维
tsne = TSNE(n_components = 2,init = 'random',random_state = 177).fit(iris_data)
n_components = 2 降为两维 将原始数据转换为 DataFrame 对象
df = pd.DataFrame(tsne.embedding_)
转换为二维表格式 将聚类结果存到 df 数据表中
df['labels'] = kmeans.labels_ 提取不同标签的数据
df1 = df[df['labels'] == 0]
df2 = df[df['labels'] == 1]
df3 = df[df['labels'] == 2] 绘制图形
fig = plt.figure(figsize = (9,6)) 使用不同的颜色表示不同的数据
plt.plot(df1[0],df1[1],'bo',df2[0],df2[1],'r*') 储存为 .png 图片
plt.savefig('../tmp/名称.png')
plt.show() 聚类模型评价指标
标准
组内的对象相互之间是相似的(相关的)
不同组中的对象是不同的(不相关的)
sklearn.metrics 提供评价指标
ARI评价法(兰德系数) adjusted _rand_score
AMI评价法(互信息) adjusted_mutual_info_score
V-measure评分 completeness_score
FMI评价法 fowlkes_mallows_score
轮廓系数评价法 silhouette _score
Calinski-Harabasz指数评价法 calinski_harabaz_score
前四种更有说服力 评分越高越好
聚类方法的评价可以等同于分类算法的评价 FMI评价法 fowlkes_mallows_score
from sklearn.metrics import fowlkes_mallows_score
for i in range(2,7):
kmeans =KMeans(n_clusters =i,random_state=123).fit(iris_data)
score = fowlkes_mallows_score(iris_target,kmeans.labels_)
print('iris数据聚 %d 类FMI评价分值为:%f'%(i,score)) 轮廓系数评价法 silhouette_score
from sklearn.metrics import silhouette_score
import matplotlib.pyplot as plt silhouettteScore=[]
for i in range(2,15):
kmeans=KMeans(n_clusters =i,random state=123).fit(iris data)
score = silhouette_score(iris_data,kmeans.labels_)
silhouettteScore.append(score)
plt.figure(figsize=(10,6))
plt.plot(range(2,15),silhouettteScore,linewidth=1.5,linestyle="-")
plt.show() 使用 Calinski-Harabasz 指数评价 K-Means 聚类模型
分值越高聚类效果越好
from sklearn.metrics import calinski_harabaz_score
for i in range(2,7):
kmeans=KMeans(n_clusters =i,random_state=123).fit(iris_data)
进行评价
score=calinski_harabaz_score(iris_data,kmeans.labels_)
print('iris数据聚%d类calinski harabaz指数为:%f'%(i,score) 构建并评价分类模型(有监督学习)
输入样本的特征值 输出对应的类别
将每个样本映射到预先定义好的类别
分类模型建立在已有模型的数据集上 用于 图像检测 物品分类 分类算法
模块名 函数名称 算法名称
linear_model LogisticRegression 逻辑斯蒂回归
svm SVC 支持向量机
neighbors KNeighborsClassifier K最近邻分类
naive_bayes GaussianNB 高斯朴素贝叶斯
tree DecisionTreeClassifier 分类决策树
ensemble RandomForestClassifier 随机森林分类
ensemble GradientBoostingClassifier 梯度提升分类树 以 breast_cancer 数据为例 使用sklearn估计器构建支持向量机(SVM)模型 import numpy as np
from sklearn.datasets import load_breast.cancer
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler cancer = load_breast_cancer()
cancer_data = cancerf['data']
Cancer_target = cancert['target']
cancer_names = cancer['feature_names'] 建立 SVM 模型
svm = SVC().fit(cancer_trainStd,cancer_target_train) 预测训练集结果
cancer_target_pred = svm.predict(cancer_testStd) 将预测结果和真实结果比对 求出预测对的结果和预测错的结果
true = np.sum(cancer_target_pred == cancer_target_test)
预测对的结果的数目
true
预测错的结果的数目
cancer_target_test.shape[0] - true
准确率
true/cancer_target_test.shape[0] 评价分类模型
分类模型对测试集进行预测得到的准确率并不能很好的反映模型的性能
结合真实值->计算准确率、召回率 F1 值和 Cohen's Kappa 系数等指标 方法名称 最佳值 sklearn 函数
Precision(精确率) 1.0 metrics.precision_score
Recall(召回率) 1.0 metrics.recall_score
F1值 1.0 metrics.f1_score
Cohen's Kappa 系数 1.0 metrics.cohen_kappa_score
ROC曲线 最靠近y轴 metrics.roc_curve from sklearn.metrics import accuracy_score,precision_score,recall_score,f1_score,cohen_kappa_score 使用SVM预测breast_cancer数据的准确率为
accuracy_score(cancer_target_test,cancer_target_pred)
使用SVM预测breast_cancer数据的精确率为
precision_score(cancer_target_test,cancer_target_pred) 绘制ROC曲线
from sklearn.metrics import roc_curve
import matplotlib.pyplot as plt
求出ROC曲线的x轴和y轴
fpr,tpr,thresholds = roc_curve(cancer_target_test,cancer_target_pred)
plIt.figure(figsize=(10,6))
plt.xlim(O,1)##设定x轴的范围
plt.ylim(0.0,1.1)##设定y轴的范围
plt.xlabel('False Postive Rate')
plt.ylabel('True Postive Rate')
plt.plot(fpr,tpr,linewidth=2,linestyle=*-".color='red")
plt.show() ROC曲线 与 x 轴面积越大 模型性能越好 构建并评价回归模型
分类和回归的区别
分类算法的标签是离散的
回归算法的标签是连续的
作用 交通 物流 社交网络和金融领域等 回归模型
自变量已知
因变量未知 需要预测
回归算法实现步骤 分为 学习 和 预测 两个步骤
学习 通过训练样本数据来拟合回归方程
预测 利用学习过程中拟合出的回归方程 将测试数据放入方程中求出预测值 回归算法
模块名称 函数名称 算法名称
linear_model LinearRegression 线性回归
svm SVR 支持向量回归
neighbors KNeighborsRegressor 最近邻回归
tree DecisionTreeRegressor 回归决策树
ensemble RandomForestRegressor 随机森林回归
ensemble GradientBoostingRegressor 梯度提升回归树 以boston数据集为例 使用skllearn估计器构建线性回归模型
from sklearn.linear_model import LinearRegression
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
boston = load_boston()
X = boston['data']
y = boston['target']
names = boston['feature_names'] 划分训练集和测试集
X_train,X_test,y_train,y_test = train_test_split(X,Y.test_size=0.2,random_state=125)
建立线性回归模型
clf = LinearRegression().fit(X_train.y_train)
预测训练集结果
y_pred = clf.predict(X_test)
前二十个结果
y_pred[:20]
使用不同的颜色表示不同的数据
plt.plot(range(y_test.shape[0]),y_test,color='blue',linewidth=1.5,linestyle='-') 评价回归模型
方法名称 最优值 sklearn函数
平均绝对误差 0.0 metrics.mean_absolute_error
均方误差 0.0 metrics.mean_squared_error
中值绝对误差 0.0 metrics.median_absolute_error
可解释方差值 1.0 metrics.explained_variance_score
R方值 1.0 metrics.r2_score 平均绝对误差 均方误差和中值绝对误差的值越靠近 0
模型性能越好
可解释方差值 和 R方值 则越靠近1 模型性能越好 from sklearn.metrics import explained_variance_score,mean_absolute_error,mean_squared_error,median_absolute_error,r2_score
Boston数据线性回归模型的平均绝对误差为
mean_absolute_error(y_test,y_pred)
Boston数据线性回归模型的均方误差为
mean_squared_error(y_test,y _pred)
Boston数据线性回归模型的中值绝对误差为
median_absolute_error(y_test,y_pred)
Boston数据线性回归模型的可解释方差值为
explained_variance_score(y_test,y_pred)
Boston数据线性回归模型的R方值为
r2_score(y test,y_pred)

注:此篇随笔进行读取内容时,所读取的文件可以修改为自己的文件.
Seaborn基础1
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt # # 折线图
def sinplot(flip = 1):
x = np.linspace(0,14,100)
for i in range(1,7):
plt.plot(x,np.sin(x+i*0.5) * (7-i) * flip)
sns.set()
# # 默认组合
sinplot()
plt.show() # # 不带灰色格子
sns.set_style("white")
sinplot()
plt.show() # 坐标加上竖线
sns.set_style("ticks")
sinplot()
plt.show() # 将右上角的两条线去掉
sinplot()
sns.despine()
plt.show() # # 盒图
sns.set_style("whitegrid")
data = np.random.normal(size=(20,6)) + np.arange(6)/2
sns.boxplot(data = data)
plt.show()

Seaborn基础2

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np def sinplot(flip = 1):
x = np.linspace(0,14,100)
for i in range(1,7):
plt.plot(x,np.sin(x+i*0.5) * (7-i) * flip) data = np.random.normal(size=(20,6)) + np.arange(6)/2
# 使用 despine 进行操作
sns.violinplot(data)
sns.despine(offset = 10)
# offset 设置距离轴的距离
plt.show() # 底部变为白色
sns.set_style("whitegrid")
# 让左面的竖线消失
sns.boxplot(data = data,palette = "deep")
sns.despine(left = True)
plt.show() # 五种主题风格 darkgrid whitegrid dark white ticks
# 绘制子图
with sns.axes_style("darkgrid"):
# 第一种风格背景为黑色
plt.subplot(211)
# 分两个一列上面
sinplot() plt.subplot(212)
sinplot(-1)
plt.show() # 设置布局,画图的大小和风格
sns.set_context("paper")
# sns.set_context("talk")
# sns.set_context("poster")
# sns.set_context("notebook")
# 线条粗细依次变大 plt.figure(figsize=(8,6))
sinplot()
plt.show() # 设置坐标字体大小 参数 font_scale
sns.set_context("paper",font_scale=3)
plt.figure(figsize=(8,6))
sinplot()
plt.show() # 设置线的粗度 rc = {"lines.linewidth":4.5}
sns.set_context("paper",font_scale=1.5,rc={"lines.linewidth":3})
plt.figure(figsize=(8,6))
sinplot()
plt.show()

Seaborn基础3

import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt sns.set(rc = {"figure.figsize":(6,6)})
# 调色板
# color_palette() 默认颜色 , 可以传入所有支持颜色
# set_palette() 设置所有图的颜色 # 分类色板,显示十种颜色
current_palette = sns.color_palette()
sns.palplot(current_palette)
plt.show() current_palette = sns.color_palette("hls",8)
# 设置八种颜色
sns.palplot(current_palette)
plt.show() # 将八种颜色应用在盒图中
current_palette = sns.color_palette("hls",8)
data = np.random.normal(size = (20,8)) + np.arange(8)/2
sns.boxplot(data = data,palette = current_palette)
plt.show() # 指定亮度和饱和度
# hls_palette()
# l 亮度 s 饱和度
# 使用饱和度方法
sns.palplot(sns.hls_palette(8,l = 1,s = 5)) # 将两个相邻的颜色相近 使用 Paired 参数
sns.palplot(sns.color_palette("Paired",10))
plt.show() # 连续型渐变色画板 color_palette("颜色名")
sns.palplot(sns.color_palette("Blues"))
# 从浅到深
plt.show()
# 从深到浅 加上 _r 后缀名
sns.palplot(sns.color_palette("BuGn_r"))
plt.show() # cubehelix_palette() 调色板
# 八种颜色分别渐变
sns.palplot(sns.color_palette("cubehelix",8))
plt.show() # 指定 start 值,在区间中颜色的显示也不同
sns.palplot(sns.cubehelix_palette(8,start=5,rot=-0.75))
plt.show() # 颜色从浅到深 light_palette
sns.palplot(sns.light_palette("green"))
plt.show() # 颜色从深到浅 dark_palette
sns.palplot(sns.dark_palette("green"))
plt.show() # 实现反转颜色 在 light_palette 中添加参数 reverse
sns.palplot(sns.light_palette("green",reverse = True))
plt.show()

Seaborn实现单变量分析

import numpy as np
import pandas as pd
from scipy import stats,integrate
import matplotlib.pyplot as plt
import seaborn as sns # 绘制直方图
sns.set(color_codes=True)
np.random.seed(sum(map(ord,"distributions")))
# 生成高斯数据
x = np.random.normal(size = 100)
#
# sns.distplot(x,kde = False)
# x 数据 kde 是否做密度估计
# 将数据划分为 15 份 bins = 15
sns.distplot(x,kde = False,bins = 15)
plt.show() # 查看数据分布状况,根据某一个指标画一条线
x = np.random.gamma(6,size = 200)
sns.distplot(x,kde = False,fit = stats.gamma)
plt.show() mean,cov = [0,1],[(1,5),(0.5,1)]
data = np.random.multivariate_normal(mean,cov,200)
df = pd.DataFrame(data,columns=["x","y"]) # 单变量使用直方图,关系使用散点图
# 关系 joinplot (x,y,data)
sns.jointplot(x = "x",y = "y",data = df)
# 绘制散点图和直方图
plt.show() # hex 图,数据越多 色越深
mean,cov = [0,1],[(1,8),(0.5,1)]
x,y = np.random.multivariate_normal(mean,cov,500).T
# 注意 .T 进行倒置
with sns.axes_style("white"):
sns.jointplot(x = x,y = y,kind = "hex",color = "k")
plt.show()

Seaborn实现回归分析

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns iris = pd.read_csv("iris.csv")
# 对角线上是单个数据的情况,旁边的图都是关系分布的情况
sns.pairplot(iris)
plt.show() tips = pd.read_csv("tips.csv")
print(tips.head()) # 画图方式 regplot() 和 lmplot
sns.regplot(x = "total_bill",y = "tip",data = tips)
# x y 都是原数据的列名
plt.show() # lmplot 画图方式,支持更高级的功能,但是规范多
sns.lmplot(x = "total_bill",y = "tip",data = tips)
plt.show() sns.lmplot(x = "size",y = "tip",data = tips)
plt.show() # 加上抖动,使回归更准确
sns.regplot(x = "size",y = "tip",data = tips,x_jitter=0.08)
# x_jitter=0.05 在原始数据集中加上小范围浮动
plt.show()

Seaborn实现多变量分析

import seaborn as sns
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt sns.set(style = "whitegrid",color_codes = True)
np.random.seed(sum(map(ord,"categorical"))) titanic = pd.read_csv("titanic.csv")
tips = pd.read_csv("tips.csv")
iris = pd.read_csv("iris.csv") # 显示多个点
sns.stripplot(x = "day",y = "total_bill",data = tips)
plt.show() sns.swarmplot(x = "day",y = "total_bill",data = tips,hue = "sex")
# hue="sex" 生成两个颜色的小圆圈 混合进行查看,进行优化
plt.show() # 四分位距 IQR 四分之一到四分之三位 之间的距离
# N = 1.5 * IQR
# 离群点 > Q3 + N , < Q1 - N
sns.boxplot(x = "day",y = "total_bill",data = tips)
# hue = "time" 列名
plt.show() # 小提琴图
sns.violinplot(x = "total_bill",y = "day",hue = "time",data = tips)
plt.show() # 加入 split 竖着展示
sns.violinplot(x = "day",y = "total_bill",hue = "sex",data = tips,split = True)
plt.show()

由于图片太多,请复制代码后运行查看.文件名修改为自己的文件夹的名字.

将形如 5D, 30s 的字符串转为秒
import sys
def convert_to_seconds(time_str):
# write code here
if 's' in time_str:
return float(time_str[:-1])
elif 'm' in time_str:
return float(time_str[:-1]) * 60
elif 'h' in time_str:
return float(time_str[:-1]) * 3600
elif 'd' in time_str:
return float(time_str[:-1]) * 3600 *24
elif 'D' in time_str:
return float(time_str[:-1]) * 3600 *24 while True:
line = sys.stdin.readline()
line = line.strip()
if line == '':
break
print(convert_to_seconds(line))

获得昨天和明天的日期
import datetime
import sys def next_day(date_str):
date = datetime.datetime.strptime(date_str, '%Y-%m-%d')
return (date + datetime.timedelta(days=1)).date() def prev_day(date_str):
date = datetime.datetime.strptime(date_str,'%Y-%m-%d')
return (date - datetime.timedelta(days = 1)).date() while True:
line = sys.stdin.readline()
line = line.strip()
if line == '':
break
print('前一天:', prev_day(line))
print('后一天:', next_day(line))

计算两个日期相隔的秒数
import datetime

def date_delta(start, end):
# 转换为标准时间
start = datetime.datetime.strptime(start,"%Y-%m-%d %H:%M:%S")
end = datetime.datetime.strptime(end,"%Y-%m-%d %H:%M:%S")
# 获取时间戳
timeStamp_start = start.timestamp()
timeStamp_end = end.timestamp()
return timeStamp_end - timeStamp_start start = input() # sys.stdin.readline()
end = input() # sys.stdin.readline() print(date_delta(start, end))

遍历多个 txt 文件进行获取值
import random

def load_config(path):
with open(path,'r') as tou:
return [line for line in tou.readlines()] headers = {
'User-Agent':load_config('useragents.txt')[random.randint(0,len(load_config('useragents.txt'))-1)].strip("\n"),
'Referer':load_config('referers.txt')[random.randint(0,len(load_config('referers.txt'))-1)].strip("\n"),
'Accept':load_config('acceptall.txt')[random.randint(0,len(load_config('acceptall.txt'))-1)].strip("\n"),
}
print(headers)

安装第三方库

pip install 包名 -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

安装第三方库进阶

# 安装 pip 包

from tkinter import *

def getBao():
pip = 'pip install %s -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com'%entry_bao.get()
print(pip)
root = Tk()
root.title("pip包")
root.geometry("250x150+400+300")
url = StringVar()
url_lab1 = Label(text = "请输入包名:")
url_lab1.pack()
entry_bao = Entry(root,textvariable = url)
entry_bao.pack()
btn1 = Button(root,text = "提交",command = getBao,width = 8,height = 2)
btn1.pack()
root.mainloop()

Python第一次实验
'''
计算
1.输入半径,输出面积和周长
2.输入面积,输出半径及周长
3.输入周长,输出半径及面积
'''
# 1.输入半径,输出面积和周长
from math import pi # 定义半径
r = int(input("请输入半径的值(整数)"))
if r < 0 :
exit("请重新输入半径")
''' S 面积: pi * r * r '''
S = pi * pow(r,2)
print(" 半径为 %d 的圆,面积为 %.2f"%(r,S))
'''C 周长: C = 2 * pi * r '''
C = 2 * pi * r
print(" 半径为 %d 的圆,周长为 %.2f"%(r,C)) # 2.输入面积,输出半径及周长
from math import pi,sqrt S = float(input("请输入圆的面积(支持小数格式)"))
if S < 0 :
exit("请重新输入面积")
'''r 半径: r = sqrt(S/pi)'''
r = sqrt(S/pi) print("面积为 %.2f 的圆,半径为 %.2f"%(S,r))
'''C 周长: C = 2 * pi * r '''
C = 2 * pi * r
print("面积为 %.2f 的圆,周长为 %.2f"%(S,C)) # 3.输入周长,输出半径及面积
from math import pi C = float(input("请输入圆的周长(支持小数格式)"))
if C < 0 :
exit("请重新输入周长")
'''r 半径: r = C/(2*pi)'''
r = C/(2*pi) print("周长为 %.2f 的圆,半径为 %.2f"%(C,r))
''' S 面积: pi * r * r '''
S = pi * pow(r,2)
print("周长为 %.2f 的圆,面积为 %.2f"%(C,S))

'''
数据结构
列表练习
1.创建列表对象 [110,'dog','cat',120,'apple']
2.在字符串 'dog' 和 'cat' 之间插入空列表
3.删除 'apple' 这个字符串
4.查找出 110、120 两个数值,并以 10 为乘数做自乘运算
'''
# 1.创建列表对象 [110,'dog','cat',120,'apple']
'''创建一个名为 lst 的列表对象'''
lst = [110,'dog','cat',120,'apple']
print(lst) # 2.在字符串 'dog' 和 'cat' 之间插入空列表
lst = [110,'dog','cat',120,'apple']
'''添加元素到 'dog' 和 'cat' 之间'''
lst.insert(2,[])
print(lst) # 3.删除 'apple' 这个字符串
lst = [110,'dog','cat',120,'apple']
'''删除最后一个元素'''
lst.pop()
print(lst) # 4.查找出 110、120 两个数值,并以 10 为乘数做自乘运算
lst = [110,'dog','cat',120,'apple']
try:
# 如果找不到数据,进行异常处理
lst[lst.index(110)] *= 10
lst[lst.index(120)] *= 10
except Exception as e:
print(e)
print(lst)

'''
字典练习
1.创建字典 {'Math':96,'English':86,'Chinese':95.5,'Biology':86,'Physics':None}
2.在字典中添加键值对 {'Histore':88}
3.删除 {'Physisc':None} 键值对
4.将键 'Chinese' 所对应的值进行四舍五入后取整
5.查询键 'Math' 的对应值
'''
# 1.创建字典 {'Math':96,'English':86,'Chinese':95.5,'Biology':86,'Physics':None}
stu_score = {'Math':96,'English':86,'Chinese':95.5,'Biology':86,'Physics':None}
# 2.在字典中添加键值对 {'Histore':88}
stu_score['Histore'] = 88
# 3.删除 {'Physisc':None} 键值对
if 'Physisc' in stu_score.keys():
'''如果存在 "Physisc" '''
del stu_score['Physisc']
# 4.将键 'Chinese' 所对应的值进行四舍五入后取整
if 'Chinese' in stu_score.keys():
# 四舍五入 使用 round
stu_score['Chinese'] = round(stu_score['Chinese'])
# 5.查询键 'Math' 的对应值
print(stu_score.get('Math',"没有找到 Math 的值"))

'''
元组练习
1.创建列表 ['pen','paper',10,False,2.5] 赋给变量并查看变量的类型
2.将变量转换为 tuple 类型,查看变量的类型
3.查询元组中的元素 False 的位置
4.根据获得的位置提取元素
'''
# 1.创建列表 ['pen','paper',10,False,2.5] 赋给变量并查看变量的类型
lst = ['pen','paper',10,False,2.5]
'''查看变量类型'''
print("变量的类型",type(lst)) # 2.将变量转换为 tuple 类型,查看变量的类型
lst = tuple(lst)
print("变量的类型",type(lst)) # 3.查询元组中的元素 False 的位置 if False in lst:
print("False 的位置为(从0开始): ",lst.index(False))
# 4.根据获得的位置提取元素
print("根据获得的位置提取的元素为: ",lst[lst.index(False)])
else:
print("不在元组中")

'''
集合练习
1.创建列表 ['apple','pear','watermelon','peach'] 并赋给变量
2.用 list() 创建列表 ['pear','banana','orange','peach','grape'],并赋给变量
3.将创建的两个列表对象转换为集合类型
4.求两个集合的并集,交集和差集
'''
# 1.创建列表 ['apple','pear','watermelon','peach'] 并赋给变量
lst = ['apple','pear','watermelon','peach']
# 2.用 list() 创建列表 ['pear','banana','orange','peach','grape'],并赋给变量
lst_2 = list({'pear','banana','orange','peach','grape'})
print(lst_2)
# 3.将创建的两个列表对象转换为集合类型
lst_set = set(lst)
lst2_set = set(lst_2)
# 4.求两个集合的并集,交集和差集
''' 并集 | 交集 & 差集 - '''
print("两个集合的 并集为 :",lst_set | lst2_set)
print("两个集合的 交集为 :",lst_set & lst2_set)
print("lst_set 与 lst2_set 的差集为 :",lst_set - lst2_set)
print("lst2_set 与 lst_set 的差集为 :",lst2_set - lst_set)

pip 国内源
常用国内源
清华:https://pypi.tuna.tsinghua.edu.cn/simple
阿里云:https://mirrors.aliyun.com/pypi/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
华中理工大学:http://pypi.hustunique.com/
山东理工大学:http://pypi.sdutlinux.org/
豆瓣:http://pypi.douban.com/simple/

format 进阶

'''format(数字,str(算术式)+"d或者f")
d 表示 int
f 表示 float
'''
format(5,str(2*4)+"d")
' 5' format(5,str(2*4)+"f")
'5.000000'
'''使用 .2f 控制小数点个数'''
format(5,str(2*4)+".2f")
' 5.00'
format(5,str(2*15)+"f")
' 5.000000' '''format(字符串,str(算术式)+"s")'''
format('s',str(2*3)+"s")
's '

进阶删除重复元素
def dedupe(items,key=None):
seen = set()
for item in items:
val = item if key==None else key(item)
#item是否为字典,是则转化为字典key(item),匿名函数调用
if val not in seen:
yield item
seen.add(val)
#集合增加元素val
if __name__=="__main__":
a = [{'x':2,'y':4},{'x':3,'y':5},{'x':5,'y':8},{'x':2,'y':4},{'x':3,'y':5}]
b=[1,2,3,4,1,3,5]
print(b)
print(list(dedupe(b)))
print(a)
print(list(dedupe(a,key=lambda a:(a['x'],a['y']))))
#按照a['x'],a['y']方式

爬虫流程复习2
1.打开网页
urllib.request.urlopen('网址')
例:response = urllib.request.urlopen('http://www.baidu.com/')
返回值为 <http.client.HTTPResponse object at 0x00000224EC2C9490> 2.获取响应头信息
urlopen 对象.getheaders()
例:response.getheaders()
返回值为 [('Bdpagetype', '1'), ('Bdqid', '0x8fa65bba0000ba44'),···,('Transfer-Encoding', 'chunked')]
[('头','信息')] 3.获取响应头信息,带参数表示指定响应头
urlopen 对象.getheader('头信息')
例:response.getheader('Content-Type')
返回值为 'text/html;charset=utf-8' 4.查看状态码
urlopen 对象.status
例:response.status
返回值为 200 则表示成功 5.得到二进制数据,然后转换为 utf-8 格式
二进制数据
例:html = response.read()
HTML 数据格式
例:html = response.read().decode('utf-8')
打印输出时,使用 decode('字符集') 的数据 print(html.decode('utf-8')) 6.存储 HTML 数据
fp = open('文件名.html','模式 wb')
例:fp = open('baidu.html', 'wb')
fp.write(response.read() 对象)
例:fp.write(html) 7.关闭文件
open对象.close()
例:fp.close() 8.使用 ssl 进行抓取 https 的网页
例:
import ssl
content = ssl._create_unverified_context()
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'} request = urllib.request.Request('http://www.baidu.com/', headers = headers)
response = urllib.request.urlopen(request, context = context)
这里的 response 就和上面一样了 9.获取码
response.getcode()
返回值为 200 10.获取爬取的网页 url
response.geturl()
返回值为 https://www.baidu.com/ 11.获取响应的报头信息
response.info()
例:
import ssl
request = urllib.request.Request('http://www.baidu.com/', headers = headers)
context = ssl._create_unverified_context()
response = urllib.request.urlopen(request, context = context)
response.info()
获取的为 头信息
--
response = urllib.request.urlopen('http://www.baidu.com/')
response.info()
返回值为 <http.client.HTTPMessage object at 0x00000268D453DA60> 12.保存网页
urllib.request.urlretrieve(url, '文件名.html')
例:urllib.request.urlretrieve(url, 'baidu.html') 13.保存图片
urllib.request.urlretrieve(url, '图片名.jpg')
例:urllib.request.urlretrieve(url, 'Dog.jpg') 其他字符(如汉字)不符合标准时,要进行编码
14.除了-._/09AZaz 都会编码
urllib.parse.quote()
例:
Param = "全文检索:*"
urllib.parse.quote(Param)
返回值为 '%E5%85%A8%E6%96%87%E6%A3%80%E7%B4%A2%3A%2A'
参考链接:https://blog.csdn.net/ZTCooper/article/details/80165038 15.会编码 / 斜线(将斜线也转换为 %.. 这种格式)
urllib.parse.quote_plus(Param) 16.将字典拼接为 query 字符串 如果有中文,进行url编码
dic_object = {
'user_name':'张三',
'user_passwd':'123456'
}
urllib.parse.urlencode(dic_object)
返回值为 'user_name=%E5%BC%A0%E4%B8%89&user_passwd=123456' 17.获取 response 的行
url = 'http://www.baidu.com'
response = urllib.request.urlopen(url)
response.readline() 18.随机获取请求头(随机包含请求头信息的列表)
user_agent = [
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv2.0.1) Gecko/20100101 Firefox/4.0.1",
"Mozilla/5.0 (Windows NT 6.1; rv2.0.1) Gecko/20100101 Firefox/4.0.1",
"Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.8.131 Version/11.11",
"Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11"
] ua = random.choice(user_agent)
headers = {'User-Agent':ua} 19.对输入的汉字进行 urlencode 编码
urllib.parse.urlencode(字典对象)
例:
chinese = input('请输入要查询的中文词语:')
wd = {'wd':chinese}
wd = urllib.parse.urlencode(wd)
返回值为 'wd=%E4%BD%A0%E5%A5%BD' 20.常见分页操作
for page in range(start_page, end_page + 1):
pn = (page - 1) * 50 21.通常会进行拼接字符串形成网址
例:fullurl = url + '&pn=' + str(pn) 22.进行拼接形成要保存的文件名
例:filename = 'tieba/' + name + '贴吧_第' + str(page) + '页.html' 23.保存文件
with open(filename,'wb') as f:
f.write(reponse.read() 对象) 24.headers 头信息可以删除的有
cookie、accept-encoding、accept-languag、content-length\connection\origin\host 25.headers 头信息不可以删除的有
Accept、X-Requested-With、User-Agent、Content-Type、Referer 26.提交给网页的数据 formdata
formdata = {
'from':'en',
'to':'zh',
'query':word,
'transtype':'enter',
'simple_means_flag':'3'
} 27.将formdata进行urlencode编码,并且转化为bytes类型
formdata = urllib.parse.urlencode(formdata).encode('utf-8') 28.使用 formdata 在 urlopen() 中
response = urllib.request.urlopen(request, data=formdata) 29.转换为正确数据(导包 json) read -> decode -> loads -> json.dumps
通过read读取过来为字节码
data = response.read()
将字节码解码为utf8的字符串
data = data.decode('utf-8')
将json格式的字符串转化为json对象
obj = json.loads(data)
禁用ascii之后,将json对象转化为json格式字符串
html = json.dumps(obj, ensure_ascii=False)
json 对象通过 str转换后 使用 utf-8 字符集格式写入
保存和之前的方法相同
with open('json.txt', 'w', encoding='utf-8') as f:
f.write(html) 30.ajax请求自带的头部
'X-Requested-With':'XMLHttpRequest' 31.豆瓣默认都得使用https来进行抓取,所以需要使用ssl模块忽略证书
例:
url = 'http://movie.douban.com/j/chart/top_list?type=24&interval_id=100%3A90&action=' page = int(input('请输入要获取页码:'))
start = (page - 1) * 20
limit = 20 key = {
'start':start,
'limit':limit
} key = urllib.parse.urlencode(key)
url = url + '&' + key
headers = {
'X-Requested-With':'XMLHttpRequest',
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'
} request = urllib.request.Request(url, headers=headers) # context = ssl._create_unverified_context()
response = urllib.request.urlopen(request) jsonret = response.read() with open('douban.txt', 'w', encoding='utf-8') as f:
f.write(jsonret.decode('utf-8'))
print('over') 32.创建处理 http 请求的对象
http_handler = urllib.request.HTTPHandler() 33.处理 https 请求
https_handler = urllib.request.HTTPSHandler() 34.创建支持http请求的opener对象
opener = urllib.request.build_opener(http_handler) 35.创建 reponse 对象
例:opener.open(Request 对象)
request = urllib.request.Request('http://www.baidu.com/')
reponse = opener.open(request)
进行保存
with open('文件名.html', 'w', encoding='utf-8') as f:
f.write(reponse.read().decode('utf-8')) 36.代理服务器
http_proxy_handler = urllib.request.ProxyHandler({'https':'ip地址:端口号'})
例:http_proxy_handler = urllib.request.ProxyHandler({'https':'121.43.178.58:3128'}) 37.私密代理服务器(下面的只是一个例子,不一定正确)
authproxy_handler = urllib.request.ProxyHandler({"http" : "user:password@ip:port"}) 38.不使用任何代理
http_proxy_handler = urllib.request.ProxyHandler({}) 39.使用了代理之后的 opener 写法
opener = urllib.request.build_opener(http_proxy_handler) 40.response 写法
response = opener.open(request) 41.如果访问一个不存在的网址会报错
urllib.error.URLError 42.HTTPError(是URLError的子类)
例:
try:
urllib.request.urlopen(url)
except urllib.error.HTTPError as e:
print(e.code)
print(e.reason)
except urllib.error.URLError as e:
print(e) 43.使用 CookieJar 创建一个 cookie 对象,保存 cookie 值
import http.cookiejar
cookie = http.cookiejar.CookieJar() 44.通过HTTPCookieProcessor构建一个处理器对象,用来处理cookie
cookie_handler = urllib.request.HTTPCookieProcessor(cookie)
opener 的写法
opener = urllib.request.build_opener(cookie_handler) 45.使用 r'\x'
\d 表示转义字符 r'\d' 表示 \d 46.设置 正则模式
pattern = re.compile(r'规则', re.xxx )
pattern = re.compile(r'i\s(.*?),')
例:pattern = re.compile(r'LOVE', re.I) 47.match 只匹配开头字符
pattern.match('字符串'[,起始位置,结束位置])
例:m = pattern.match('i love you', 2, 6)
返回值为 <re.Match object; span=(2, 6), match='love'> 48. search 从开始匹配到结尾,返回第一个匹配到的
pattern.search('字符串')
例:m = pattern.search('i love you, do you love me, yes, i love')
返回值为 <re.Match object; span=(2, 6), match='love'> 49.findall 将匹配到的都放到列表中
pattern.findall('字符串')
例:m = pattern.findall('i love you, do you love me, yes, i love')
返回值为 ['love', 'love', 'love'] 50.split 使用匹配到的字符串对原来的数据进行切割
pattern.split('字符串',次数)
例:m = pattern.split('i love you, do you love me, yes, i love me', 1)
返回值为 ['i ', ' you, do you love me, yes, i love me']
例:m = pattern.split('i love you, do you love me, yes, i love me', 2)
返回值为 ['i ', ' you, do you ', ' me, yes, i love me']
例:m = pattern.split('i love you, do you love me, yes, i love me', 3)
返回值为 ['i ', ' you, do you ', ' me, yes, i ', ' me'] 51.sub 使用新字符串替换匹配到的字符串的值,默认全部替换
pattern.sub('新字符串','要匹配字符串'[,次数])
注:返回的是字符串
例:
string = 'i love you, do you love me, yes, i love me'
m = pattern.sub('hate', string, 1)
m 值为 'i hate you, do you love me, yes, i love me' 52.group 匹配组
m.group() 返回的是匹配都的所有字符
m.group(1) 返回的是第二个规则匹配到的字符
例:
string = 'i love you, do you love me, yes, i love me'
pattern = re.compile(r'i\s(.*?),')
m = pattern.match(string)
m.group()
返回值为 'i love you,'
m.group(1)
返回值为 'love you' 53.匹配标签
pattern = re.compile(r'<div class="thumb">(.*?)<img src=(.*?) alt=(.*?)>(.*?)</div>', re.S) 54.分离出文件名和扩展名,返回二元组
os.path.splitext(参数)
例:
获取路径
image_path = './qiushi'
获取后缀名
extension = os.path.splitext(image_url)[-1] 55.合并多个字符串
os.path.join()
图片路径
image_path = os.path.join(image_path, image_name + extension)
保存文件
urllib.request.urlretrieve(image_url, image_path) 56.获取 a 标签下的 href 的内容
pattern = re.compile(r'<a href="(.*?)" class="main_14" target="_blank">(.*?)</a>', re.M)
例:
import urllib.parse
import urllib.request
import re class SmileSpider(object):
"""
爬取笑话网站笑话的排行榜
"""
def __init__(self, url, page=1):
super(SmileSpider, self).__init__()
self.url = url
self.page = page def handle_url(self):
'''
处理url并且生成request请求对象
'''
self.url = self.url + '?mepage=' + str(self.page)
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36',
}
request = urllib.request.Request(self.url, headers=headers)
return request def xiazai(self, request):
'''
负责下载数据,并且将数据返回
'''
response = urllib.request.urlopen(request)
html = response.read().decode('gbk')
return html def handle_data(self, data):
'''
开始处理数据,将段子抓取出来并且写入文件
'''
# 这个必须使用多行模式进行抓取,因为是抓取多个a链接
pattern = re.compile(r'<a href="(.*?)" class="main_14" target="_blank">(.*?)</a>', re.M)
# 找到所有的笑话链接
alist = pattern.findall(data)
# print(alist)
# exit()
print('开始下载')
for smile in alist:
# 获取标题
# title = alist[14][1]
title = smile[1]
# 获取url
# smile_url = alist[14][0]
smile_url = smile[0]
# 获取内容
content = self.handle_content(smile_url)
# 将抓取的这一页的笑话写到文件中
with open('xiaohua.html', 'a', encoding='gbk') as f:
f.write('<h1>' + title + '</h1>' + content)
print('下载完毕') def handle_content(self, smile_url):
# 因为有的href中有中文,所以必须先转码再拼接,如果先拼接再转码,就会将:也给转码了,不符合要求
smile_url = urllib.parse.quote(smile_url)
smile_url = 'http://www.jokeji.cn' + smile_url
# print(smile_url)
# exit()
content = self.xiazai(smile_url) # 由于抓取的文本中,有的中间有空格,所以使用单行模式进行抓取
pattern = re.compile(r'<span id="text110">(.*?)</span>', re.S)
ret = pattern.search(content)
return ret.group(1) def start(self):
request = self.handle_url()
html = self.xiazai(request)
self.handle_data(html) if __name__ == '__main__':
url = 'http://www.jokeji.cn/hot.asp'
spider = SmileSpider(url)
spider.start() 57.href 中有中文的需要先进行转码,然后再拼接
smile_url = urllib.parse.quote(smile_url)
smile_url = 'http://www.jokeji.cn' + smile_url 58.导入 etree
from lxml import etree 59.实例化一个 html 对象,DOM模型
etree.HTML(通过requests库的get方法或post方法获取的信息 其实就是 HTML 代码)
例:html_tree = etree.HTML(text)
返回值为 <Element html at 0x26ee35b2400>
例:type(html_tree)
<class 'lxml.etree._Element'> 60.查找所有的 li 标签
html_tree.xpath('//li') 61.获取所有li下面a中属性href为link1.html的a
result = html_tree.xpath('//标签/标签[@属性="值"]')
例:result = html_tree.xpath('//li/a[@href="link.html"]') 62.获取最后一个 li 标签下 a 标签下面的 href 值
result = html_tree.xpath('//li[last()]/a/@href') 63.获取 class 为 temp 的结点
result = html_tree.xpath('//*[@class = "temp"]') 64.获取所有 li 标签下的 class 属性
result = html_tree.xpath('//li/@class') 65.取出内容
[0].text
例:result = html_tree.xpath('//li[@class="popo"]/a')[0].text
例:result = html_tree.xpath('//li[@class="popo"]/a/text()') 66.将 tree 对象转化为字符串
etree.tostring(etree.HTML对象).decode('utf-8')
例:
text = '''
<div>
<ul>
<li class="item-0"><a href="link1.html">first item</a></li>
<li class="item-1"><a href="link2.html">second item</a></li>
<li class="item-inactive"><a href="link3.html">third item</a></li>
<li class="item-1"><a href="link4.html">fourth item</a></li>
<li class="item-0"><a href="link5.html">fifth item</a>
</ul>
</div>
'''
html = etree.HTML(text)
tostring 转换的是bytes 类型数据
result = etree.tostring(html)
将 bytes 类型数据转换为 str 类型数据
print(result.decode('utf-8')) 67.动态保存图片,使用url后几位作为文件名
request = urllib.request.Request(url, headers=headers)
response = urllib.request.urlopen(request)
html_tree = etree.HTML(html)
img_list = html_tree.xpath('//div[@class="box picblock col3"]/div/a/img/@src2')
for img_url in img_list:
# 定制图片名字为url后10位
file_name = 'image/' + img_url[-10:]
load_image(img_url, file_name)
load_image内容:
def load_image(url, file_name):
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36',
}
request = urllib.request.Request(url, headers=headers)
response = urllib.request.urlopen(request)
image_bytes = response.read() with open(file_name, 'wb') as f:
f.write(image_bytes)
print(file_name + '图片已经成功下载完毕') 例:
def load_page(url):
headers = {
#'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36',
}
print(url)
# exit()
request = urllib.request.Request(url, headers=headers)
response = urllib.request.urlopen(request)
html = response.read() # 这是专业的图片网站,使用了懒加载,但是可以通过源码来进行查看,并且重新写xpath路径
with open('7image.html', 'w', encoding='utf-8') as f:
f.write(html.decode('utf-8'))
exit() # 将html文档解析问DOM模型
html_tree = etree.HTML(html)
# 通过xpath,找到需要的所有的图片的src属性,这里获取到的
img_list = html_tree.xpath('//div[@class="box picblock col3"]/div/a/img/@src2')
for img_url in img_list:
# 定制图片名字为url后10位
file_name = 'image/' + img_url[-10:]
load_image(img_url, file_name) def load_image(url, file_name):
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36',
}
request = urllib.request.Request(url, headers=headers)
response = urllib.request.urlopen(request)
image_bytes = response.read() with open(file_name, 'wb') as f:
f.write(image_bytes)
print(file_name + '图片已经成功下载完毕')
def main():
start = int(input('请输入开始页面:'))
end = int(input('请输入结束页面:'))
url = 'http://sc.chinaz.com/tag_tupian/'
for page in range(start, end + 1):
if page == 1:
real_url = url + 'KaTong.html'
else:
real_url = url + 'KaTong_' + str(page) + '.html'
load_page(real_url)
print('第' + str(page) + '页下载完毕') if __name__ == '__main__':
main() 68.懒图片加载案例
例:
import urllib.request
from lxml import etree
import json def handle_tree(html_tree):
node_list = html_tree.xpath('//div[@class="detail-wrapper"]')
duan_list = []
for node in node_list:
# 获取所有的用户名,因为该xpath获取的是一个span列表,然后获取第一个,并且通过text属性得到其内容
user_name = node.xpath('./div[contains(@class, "header")]/a/div/span[@class="name"]')[0].text
# 只要涉及到图片,很有可能都是懒加载,所以要右键查看网页源代码,才能得到真实的链接
# 由于这个获取的结果就是属性字符串,所以只需要加上下标0即可
face = node.xpath('./div[contains(@class, "header")]//img/@data-src')[0]
# .代表当前,一个/表示一级子目录,两个//代表当前节点里面任意的位置查找
content = node.xpath('./div[@class="content-wrapper"]//p')[0].text
zan = node.xpath('./div[@class="options"]//li[@class="digg-wrapper "]/span')[0].text
item = {
'username':user_name,
'face':face,
'content':content,
'zan':zan,
}
# 将其存放到列表中
duan_list.append(item) # 将列表写入到文件中
with open('8duanzi.txt', 'a', encoding='utf-8') as f:
f.write(json.dumps(duan_list, ensure_ascii=False) + '\n')
print('over') def main():
# 爬取百度贴吧,不能加上headers,加上headers爬取不下来
url = 'http://neihanshequ.com/'
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36',
}
request = urllib.request.Request(url, headers=headers)
response = urllib.request.urlopen(request)
html_bytes = response.read() # fp = open('8tieba.html', 'w', encoding='utf-8')
# fp.write(html_bytes.decode('utf-8'))
# fp.close()
# exit() # 将html字节串转化为html文档树
# 文档树有xpath方法,文档节点也有xpath方法
# 【注】不能使用字节串转化为文档树,这样会有乱码
html_tree = etree.HTML(html_bytes.decode('utf-8')) handle_tree(html_tree) if __name__ == '__main__':
main() 69. . / 和 // 在 xpath 中的使用
.代表当前目录
/ 表示一级子目录
// 代表当前节点里面任意的位置 70.获取内容的示范
获取内容时,如果为字符串,则不需要使用 text 只需要写[0]
face = node.xpath('./div[contains(@class, "header")]//img/@data-src')[0]
div 下 class 为 "content-wrapper" 的所有 p 标签内容
content = node.xpath('./div[@class="content-wrapper"]//p')[0].text
div 下 class 为 "options" 的所有 li 标签下 class为 "digg-wrapper" 的所有 span 标签内容
zan = node.xpath('./div[@class="options"]//li[@class="digg-wrapper"]/span')[0].text 71.将json对象转化为json格式字符串
f.write(json.dumps(duan_list, ensure_ascii=False) + '\n') 72.正则获取 div 下的内容
1.获取 div 到 img 之间的数据
2.img 下 src 的数据
3.img 下 alt 的数据
4.一直到 div 结束的数据
pattern = re.compile(r'<div class="thumb">(.*?)<img src=(.*?) alt=(.*?)>(.*?)</div>', re.S)
pattern.方法 ,参考上面的正则 73.带有参数的 get 方式
import requests
params = {
'wd':'中国'
}
r = requests.get('http://www.baidu.com/s?', headers=headers, params=params)
requests.get 还可以添加 cookie 参数 74.设置编码
r.encoding='utf-8 75.查看所有头信息
r.request.headers 76.在 requests.get 方法中 url,params,headers,proxies 为参数
url 网址 params 需要的数据 headers 头部 proxies 代理 77.通过 Session 对象,发送请求
s = requests.Session() 78.发送请求
s.post(url,data,headers) 79.接收请求
s.get(url[,proxies]) 80.当返回为 json 样式时
例:
city = input('请输入要查询的城市:')
params = {
'city':city
}
r = requests.get(url, params=params)
r.json() 会打印出响应的内容 81.BeautifulSoup 创建对象
from bs4 import BeautifulSoup
soup = BeautifulSoup(open(url,encoding='utf-8),'lxml') 82.查找第一个<title> 标签
soup.title
返回值为 <title>三国猛将</title> 83.查找第一个 a 标签
soup.a
返回值为 <a class="aa" href="http://www.baidu.com" title="baidu">百度</a> 84.查找第一个 ul 标签
soup.ul 85.查看标签名字
a_tag = soup.a
a_tag.name
返回值为 a 86.查看标签内容
a_tag.attrs
返回值为 {'href': 'http://www.baidu.com', 'title': 'baidu', 'class': ['aa']} 87.获取找到的 a 标签的 href 内容(第一个 a)
soup.a.get('href')
返回值为 http://www.baidu.com 88.获取 a 标签下的 title 属性(第一个 a)
soup.a.get('title')
返回值为 baidu 89.查看 a 标签下的内容
soup.标签.string 标签还可以是 head、title等
soup.a.string
返回值为 百度 90.获取 p 标签下的内容
soup.p.string 91.查看 div 的内容,包含 '\n'
soup.div.contents
返回值为
['\n', <div class="div">
<a class="la" href="www.nihao.com">你好</a>
</div>, '\n', <div>
<a href="www.hello.com">世界</a>
</div>, '\n'] 92.查看使用的字符集
soup.div.contents[1]
返回值为 <meta charset="utf-8"/> 93.查看body的子节点
soup.标签.children
例:soup.body.children
返回值是一个迭代对象,需要遍历输出
返回值为 <list_iterator object at 0x0000021863886C10>
for child in soup.body.children:
print(child)
返回值为 body 中的所有内容 94.查看所有的子孙节点
soup.标签.descendants
例:soup.div.descendants
返回值为
<div class="div">
<a class="la" href="www.nihao.com">你好</a>
</div>
<a class="la" href="www.nihao.com">你好</a>
你好 95.查看所有的 a 标签
soup.find_all('a')
返回值为 包含所有的 a 标签的列表 96.查看 a 标签中第二个链接的内容
soup.find_all('a')[1].string 97.查看 a 标签中第二个链接的href值
soup.find_all('a')[1].href 98.将 re 正则嵌入进来,找寻所有以 b 开头的标签
soup.findall(re.compile('^b'))
返回值为 <body>标签 <b> 99.找到所有的 a 标签和 b 标签
soup.findall(re.compile(['a','b']))
返回值为 <a> 和 <b> 标签 100.通过标签名获取所有的 a 标签
soup.select('a')
返回值为 所有的 <a> 标签 101.通过 类名 获取标签(在 class 等于的值前面加 .)
soup.select('.aa')
返回值为 class='aa' 的标签 102.通过 id 名获取标签(在 id 等于的值前面加 #)
soup.select('#wangyi')
返回值为 id='wangyi'的标签 103.查看 div 下 class='aa' 的标签
soup.select('标签 .class 等于的值')
soup.select('div .aa') 104.查看 div 下,第一层 class='aa' 的标签
soup.select('.标签名 > .class= 的值')
soup.select('.div > .la') 105.根据属性进行查找,input 标签下class为 haha 的标签
soup.select('input[class="haha"]') 例:
import requests
from bs4 import BeautifulSoup
import json
import lxml def load_url(jl, kw):
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36',
} url = 'http://sou.zhaopin.com/jobs/searchresult.ashx?'
params = {
'jl':jl,
'kw':kw,
}
# 自动完成转码,直接使用即可
r = requests.get(url, params=params, headers=headers) handle_data(r.text) def handle_data(html):
# 创建soup对象
soup = BeautifulSoup(html, 'lxml')
# 查找职位名称
job_list = soup.select('#newlist_list_content_table table')
# print(job_list)
jobs = []
i = 1
for job in job_list:
# 因为第一个table只是表格的标题,所以要过滤掉
if i == 1:
i = 0
continue
item = {}
# 公司名称
job_name = job.select('.zwmc div a')[0].get_text() # 职位月薪
company_name = job.select('.gsmc a')[0].get_text()
# 工作地点
area = job.select('.gzdd')[0].get_text() # 发布日期
time = job.select('.gxsj span')[0].get_text()
# 将所有信息添加到字典中
item['job_name'] = job_name
item['company_name'] = company_name
item['area'] = area
item['time'] = time
jobs.append(item) # 将列表转化为json格式字符串,然后写入到文件中
content = json.dumps(jobs, ensure_ascii=False)
with open('python.json', 'w', encoding='utf-8') as f:
f.write(content)
print('over') def main():
# jl = input('请输入工作地址:')
# kw = input('请输入工作职位:')
load_url(jl='北京', kw='python') if __name__ == '__main__':
main() 106.将字典进行 json 转换为
import json
str_dict = {"name":"张三", "age":55, "height":180}
print(json.dumps(str_dict, ensure_ascii=False))
使用 ensure_ascii 输出则为 utf-8 编码 107.读取转换的对象,(注意 loads 和 load 方法)
json.loads(json.dumps 对象)
string = json.dumps(str_dict, ensure_ascii=False)
json.loads(string)
{"name":"张三", "age":55, "height":180} 108.将对象序列化之后写入文件
json.dump(字典对象,open(文件名.json,'w',encoding='utf-8,ensure_ascii=False))
json.dump(str_dict, open('jsontest.json', 'w', encoding='utf-8'), ensure_ascii=False) 109.转换本地的 json 文件转换为 python 对象
json.load(open('文件名.json',encoding='utf-8)) 110.jsonpath 示例:
book.json文件
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
} import json
import jsonpath obj = json.load(open('book.json', encoding='utf-8')) 所有book
book = jsonpath.jsonpath(obj, '$..book')
print(book) 所有book中的所有作者
authors = jsonpath.jsonpath(obj, '$..book..author')
print(authors) book中的前两本书 '$..book[:2]'
book中的最后两本书 '$..book[-2:]'
book = jsonpath.jsonpath(obj, '$..book[0,1]')
print(book) 所有book中,有属性isbn的书籍
book = jsonpath.jsonpath(obj, '$..book[?(@.isbn)]')
print(book) 所有book中,价格小于10的书籍
book = jsonpath.jsonpath(obj, '$.store.book[?(@.price<10)]')
print(book)

numpy第三方库
# 导入numpy 并赋予别名 np
import numpy as np
# 创建数组的常用的几种方式(列表,元组,range,arange,linspace(创建的是等差数组),zeros(全为 0 的数组),ones(全为 1 的数组),logspace(创建的是对数数组))
# 列表方式
np.array([1,2,3,4])
# array([1, 2, 3, 4]) # 元组方式
np.array((1,2,3,4))
# array([1, 2, 3, 4]) # range 方式
np.array(range(4)) # 不包含终止数字
# array([0, 1, 2, 3]) # 使用 arange(初始位置=0,末尾,步长=1)
# 不包含末尾元素
np.arange(1,8,2)
# array([1, 3, 5, 7]) np.arange(8)
# array([0, 1, 2, 3, 4, 5, 6, 7])
# 使用 linspace(起始数字,终止数字,包含数字的个数[,endpoint = False]) 生成等差数组 # 生成等差数组,endpoint 为 True 则包含末尾数字
np.linspace(1,3,4,endpoint=False)
# array([1. , 1.5, 2. , 2.5])
np.linspace(1,3,4,endpoint=True)
# array([1. , 1.66666667, 2.33333333, 3. ]) # 创建全为零的一维数组
np.zeros(3)
# 创建全为一的一维数组
np.ones(4)
# array([1., 1., 1., 1.]) np.linspace(1,3,4)
# array([1. , 1.66666667, 2.33333333, 3. ]) # np.logspace(起始数字,终止数字,数字个数,base = 10) 对数数组
np.logspace(1,3,4)
# 相当于 10 的 linspace(1,3,4) 次方
# array([ 10. , 46.41588834, 215.443469 , 1000. ])
np.logspace(1,3,4,base = 2)
# array([2. , 3.1748021, 5.0396842, 8. ]) # 创建二维数组(列表嵌套列表)
np.array([[1,2,3],[4,5,6]])
'''
array([[1, 2, 3],
[4, 5, 6]])
''' # 创建全为零的二维数组
# 两行两列
np.zeros((2,2))
'''
array([[0., 0.],
[0., 0.]])
'''
# 三行三列
np.zeros((3,2))
'''
array([[0., 0.],
[0., 0.],
[0., 0.]])
''' # 创建一个单位数组
np.identity(3)
'''
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
'''
# 创建一个对角矩阵,(参数为对角线上的数字)
np.diag((1,2,3))
'''
array([[1, 0, 0],
[0, 2, 0],
[0, 0, 3]])
''' import numpy as np
x = np.arange(8)
# [0 1 2 3 4 5 6 7]
# 在数组尾部追加一个元素
np.append(x,10)
# array([ 0, 1, 2, 3, 4, 5, 6, 7, 10]) # 在数组尾部追加多个元素
np.append(x,[15,16,17])
# array([ 0, 1, 2, 3, 4, 5, 6, 7, 15, 16, 17]) # 使用 数组下标修改元素的值
x[0] = 99
# array([99, 1, 2, 3, 4, 5, 6, 7]) # 在指定位置插入数据
np.insert(x,0,54)
# array([54, 99, 1, 2, 3, 4, 5, 6, 7]) # 创建一个多维数组
x = np.array([[1,2,3],[11,22,33],[111,222,333]])
'''
array([[ 1, 2, 3],
[ 11, 22, 33],
[111, 222, 333]])
'''
# 修改第 0 行第 2 列的元素值
x[0,2] = 9
'''
array([[ 1, 2, 9],
[ 11, 22, 33],
[111, 222, 333]])
'''
# 行数大于等于 1 的,列数大于等于 1 的置为 1
x[1:,1:] = 1
'''
array([[ 1, 2, 9],
[ 11, 1, 1],
[111, 1, 1]])
'''
# 同时修改多个元素值
x[1:,1:] = [7,8]
'''
array([[ 1, 2, 9],
[ 11, 7, 8],
[111, 7, 8]])
'''
x[1:,1:] = [[7,8],[9,10]]
'''
array([[ 1, 2, 9],
[ 11, 7, 8],
[111, 9, 10]])
''' import numpy as np
n = np.arange(10)
# array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) # 查看数组的大小
n.size
# 10 # 将数组分为两行五列
n.shape = 2,5
'''
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
'''
# 显示数组的维度
n.shape
# (2, 5) # 设置数组的维度,-1 表示自动计算
n.shape = 5,-1
'''
array([[0, 1],
[2, 3],
[4, 5],
[6, 7],
[8, 9]])
'''
# 将新数组设置为调用数组的两行五列并返回
x = n.reshape(2,5)
'''
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
'''
x = np.arange(5)
# 将数组设置为两行,没有数的设置为 0
x.resize((2,10))
'''
array([[0, 1, 2, 3, 4, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
'''
# 将 x 数组的两行五列形式显示,不改变 x 的值
np.resize(x,(2,5))
'''
array([[0, 1, 2, 3, 4],
[0, 0, 0, 0, 0]])
''' import numpy as np
n = np.array(([1,2,3],[4,5,6],[7,8,9]))
'''
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
'''
# 第一行元素
n[0]
# array([1, 2, 3]) # 第一行第三列元素
n[0,2]
# 3 # 第一行和第二行的元素
n[[0,1]]
'''
array([[1, 2, 3],
[4, 5, 6]])
'''
# 第一行第三列,第三行第二列,第二行第一列
n[[0,2,1],[2,1,0]]
# array([3, 8, 4]) a = np.arange(8)
# array([0, 1, 2, 3, 4, 5, 6, 7]) # 将数组倒序
a[::-1]
# array([7, 6, 5, 4, 3, 2, 1, 0]) # 步长为 2
a[::2]
# array([0, 2, 4, 6]) # 从 0 到 4 的元素
a[:5]
# array([0, 1, 2, 3, 4]) c = np.arange(16)
# array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
c.shape = 4,4
'''
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
''' # 第一行,第三个元素到第五个元素(如果没有则输出到末尾截止)
c[0,2:5]
# array([2, 3]) # 第二行元素
c[1]
# array([4, 5, 6, 7]) # 第三行到第六行,第三列到第六列
c[2:5,2:5]
'''
array([[10, 11],
[14, 15]])
'''
# 第二行第三列元素和第三行第四列元素
c[[1,2],[2,3]]
# array([ 6, 11]) # 第一行和第三行的第二列到第三列的元素
c[[0,2],1:3]
'''
array([[ 1, 2],
[ 9, 10]])
'''
# 第一列和第三列的所有横行元素
c[:,[0,2]]
'''
array([[ 0, 2],
[ 4, 6],
[ 8, 10],
[12, 14]])
'''
# 第三列所有元素
c[:,2]
# array([ 2, 6, 10, 14]) # 第二行和第四行的所有元素
c[[1,3]]
'''
array([[ 4, 5, 6, 7],
[12, 13, 14, 15]])
'''
# 第一行的第二列,第四列元素,第四行的第二列,第四列元素
c[[0,3]][:,[1,3]]
'''
array([[ 1, 3],
[13, 15]])
''' import numpy as np
x = np.array((1,2,3,4,5)) # 使用 * 进行相乘
x*2
# array([ 2, 4, 6, 8, 10]) # 使用 / 进行相除
x / 2
# array([0.5, 1. , 1.5, 2. , 2.5])
2 / x
# array([2. , 1. , 0.66666667, 0.5 , 0.4 ]) # 使用 // 进行整除
x//2
# array([0, 1, 1, 2, 2], dtype=int32)
10//x
# array([10, 5, 3, 2, 2], dtype=int32) # 使用 ** 进行幂运算
x**3
# array([ 1, 8, 27, 64, 125], dtype=int32) 2 ** x
# array([ 2, 4, 8, 16, 32], dtype=int32) # 使用 + 进行相加
x + 2
# array([3, 4, 5, 6, 7]) # 使用 % 进行取模
x % 3
# array([1, 2, 0, 1, 2], dtype=int32) # 数组与数组之间的运算
# 使用 + 进行相加
np.array([1,2,3,4]) + np.array([11,22,33,44])
# array([12, 24, 36, 48]) np.array([1,2,3,4]) + np.array([3])
# array([4, 5, 6, 7]) n = np.array((1,2,3))
# +
n + n
# array([2, 4, 6])
n + np.array([4])
# array([5, 6, 7]) # *
n * n
# array([1, 4, 9])
n * np.array(([1,2,3],[4,5,6],[7,8,9]))
'''
array([[ 1, 4, 9],
[ 4, 10, 18],
[ 7, 16, 27]])
''' # -
n - n
# array([0, 0, 0]) # /
n/n
# array([1., 1., 1.]) # **
n**n
# array([ 1, 4, 27], dtype=int32) x = np.array((1,2,3))
y = np.array((4,5,6))
# 数组的内积运算(对应位置上元素相乘)
np.dot(x,y)
# 32
sum(x*y)
# 32 # 布尔运算
n = np.random.rand(4)
# array([0.53583849, 0.09401473, 0.07829069, 0.09363152]) # 判断数组中的元素是否大于 0.5
n > 0.5
# array([ True, False, False, False]) # 将数组中大于 0.5 的元素显示
n[n>0.5]
# array([0.53583849]) # 找到数组中 0.05 ~ 0.4 的元素总数
sum((n > 0.05)&(n < 0.4))
# 3 # 是否都大于 0.2
np.all(n > 0.2)
# False # 是否有元素小于 0.1
np.any(n < 0.1)
# True # 数组与数组之间的布尔运算
a = np.array([1,4,7])
# array([1, 4, 7])
b = np.array([4,3,7])
# array([4, 3, 7]) # 在 a 中是否有大于 b 的元素
a > b
# array([False, True, False]) # 在 a 中是否有等于 b 的元素
a == b
# array([False, False, True]) # 显示 a 中 a 的元素等于 b 的元素
a[a == b]
# array([7]) # 显示 a 中的偶数且小于 5 的元素
a[(a%2 == 0) & (a < 5)]
# array([4]) import numpy as np
# 将 0~100 10等分
x = np.arange(0,100,10)
# array([ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90]) # 每个数组元素对应的正弦值
np.sin(x)
'''
array([ 0. , -0.54402111, 0.91294525, -0.98803162, 0.74511316,
-0.26237485, -0.30481062, 0.77389068, -0.99388865, 0.89399666])
'''
# 每个数组元素对应的余弦值
np.cos(x)
'''
array([ 1. , -0.83907153, 0.40808206, 0.15425145, -0.66693806,
0.96496603, -0.95241298, 0.6333192 , -0.11038724, -0.44807362])
'''
# 对参数进行四舍五入
np.round(np.cos(x))
# array([ 1., -1., 0., 0., -1., 1., -1., 1., -0., -0.]) # 对参数进行上入整数 3.3->4
np.ceil(x/3)
# array([ 0., 4., 7., 10., 14., 17., 20., 24., 27., 30.]) # 分段函数
x = np.random.randint(0,10,size=(1,10))
# array([[0, 3, 6, 7, 9, 4, 9, 8, 1, 8]]) # 大于 4 的置为 0
np.where(x > 4,0,1)
# array([[1, 1, 0, 0, 0, 1, 0, 0, 1, 0]]) # 小于 4 的乘 2 ,大于 7 的乘3
np.piecewise(x,[x<4,x>7],[lambda x:x*2,lambda x:x*3])
# array([[ 0, 6, 0, 0, 27, 0, 27, 24, 2, 24]]) import numpy as np
x = np.array([1,4,5,2])
# array([1, 4, 5, 2]) # 返回排序后元素的原下标
np.argsort(x)
# array([0, 3, 1, 2], dtype=int64) # 输出最大值的下标
x.argmax( )
# 2 # 输出最小值的下标
x.argmin( )
# 0 # 对数组进行排序
x.sort( ) import numpy as np
# 生成一个随机数组
np.random.randint(0,6,3)
# array([1, 1, 3]) # 生成一个随机数组(二维数组)
np.random.randint(0,6,(3,3))
'''
array([[4, 4, 1],
[2, 1, 0],
[5, 0, 0]])
'''
# 生成十个随机数在[0,1)之间
np.random.rand(10)
'''
array([0.9283789 , 0.43515554, 0.27117021, 0.94829333, 0.31733981,
0.42314939, 0.81838647, 0.39091899, 0.33571004, 0.90240897])
'''
# 从标准正态分布中随机抽选出3个数
np.random.standard_normal(3)
# array([0.34660435, 0.63543859, 0.1307822 ])
# 返回三页四行两列的标准正态分布数
np.random.standard_normal((3,4,2))
'''
array([[[-0.24880261, -1.17453957],
[ 0.0295264 , 1.04038047],
[-1.45201783, 0.57672288],
[ 1.10282747, -2.08699482]], [[-0.3813943 , 0.47845782],
[ 0.97708005, 1.1760147 ],
[ 1.3414987 , -0.629902 ],
[-0.29780567, 0.60288726]], [[ 1.43991349, -1.6757028 ],
[-1.97956809, -1.18713495],
[-1.39662811, 0.34174275],
[ 0.56457553, -0.83224426]]])
'''
# 创建矩阵
import numpy as np
x = np.matrix([[1,2,3],[4,5,6]])
'''
matrix([[1, 2, 3],
[4, 5, 6]])
'''
y = np.matrix([1,2,3,4,5,6])
# matrix([[1, 2, 3, 4, 5, 6]]) # x 的第二行第二列元素
x[1,1]
# 5 # 矩阵的函数
import numpy as np
# 矩阵的转置
x = np.matrix([[1,2,3],[4,5,6]])
'''
matrix([[1, 2, 3],
[4, 5, 6]])
'''
y = np.matrix([1,2,3,4,5,6])
# matrix([[1, 2, 3, 4, 5, 6]]) # 实现矩阵的转置
x.T
'''
matrix([[1, 4],
[2, 5],
[3, 6]])
'''
y.T
'''
matrix([[1],
[2],
[3],
[4],
[5],
[6]])
'''
# 元素平均值
x.mean()
# 3.5 # 纵向平均值
x.mean(axis = 0)
# matrix([[2.5, 3.5, 4.5]]) # 横向平均值
x.mean(axis = 1)
'''
matrix([[2.],
[5.]])
''' # 所有元素之和
x.sum()
# 21 # 横向最大值
x.max(axis = 1)
'''
matrix([[3],
[6]])
'''
# 横向最大值的索引下标
x.argmax(axis = 1)
'''
matrix([[2],
[2]], dtype=int64)
''' # 对角线元素
x.diagonal()
# matrix([[1, 5]]) # 非零元素下标
x.nonzero()
# (array([0, 0, 0, 1, 1, 1], dtype=int64),
# array([0, 1, 2, 0, 1, 2], dtype=int64)) # 矩阵的运算
import numpy as np
x = np.matrix([[1,2,3],[4,5,6]])
'''
matrix([[1, 2, 3],
[4, 5, 6]])
'''
y = np.matrix([[1,2],[4,5],[7,8]])
'''
matrix([[1, 2],
[4, 5],
[7, 8]])
'''
# 矩阵的乘法
x*y
'''
matrix([[30, 36],
[66, 81]])
'''
# 相关系数矩阵,可使用在列表元素数组矩阵
# 负相关
np.corrcoef([1,2,3],[8,5,4])
'''
array([[ 1. , -0.96076892],
[-0.96076892, 1. ]])
'''
# 正相关
np.corrcoef([1,2,3],[4,5,7])
'''
array([[1. , 0.98198051],
[0.98198051, 1. ]])
'''
# 矩阵的方差
np.cov([1,1,1,1,1])
# array(0.) # 矩阵的标准差
np.std([1,1,1,1,1])
# 0.0 x = [-2.1,-1,4.3]
y = [3,1.1,0.12] # 垂直堆叠矩阵
z = np.vstack((x,y))
'''
array([[-2.1 , -1. , 4.3 ],
[ 3. , 1.1 , 0.12]])
'''
# 矩阵的协方差
np.cov(z)
'''
array([[11.71 , -4.286 ],
[-4.286 , 2.14413333]])
'''
np.cov(x,y)
'''
array([[11.71 , -4.286 ],
[-4.286 , 2.14413333]])
'''
# 标准差
np.std(z)
# 2.2071223094538484 # 列向标准差
np.std(z,axis = 1)
# array([2.79404128, 1.19558447]) # 方差
np.cov(x)
# array(11.71) # 特征值和特征向量
A = np.array([[1,-3,3],[3,-5,3],[6,-6,4]])
'''
array([[ 1, -3, 3],
[ 3, -5, 3],
[ 6, -6, 4]])
'''
e,v = np.linalg.eig(A)
# e 为特征值, v 为特征向量
'''
e
array([ 4.+0.00000000e+00j, -2.+1.10465796e-15j, -2.-1.10465796e-15j])
v
array([[-0.40824829+0.j , 0.24400118-0.40702229j,
0.24400118+0.40702229j],
[-0.40824829+0.j , -0.41621909-0.40702229j,
-0.41621909+0.40702229j],
[-0.81649658+0.j , -0.66022027+0.j ,
-0.66022027-0.j ]])
'''
# 矩阵与特征向量的乘积
np.dot(A,v)
'''
array([[-1.63299316+0.00000000e+00j, -0.48800237+8.14044580e-01j,
-0.48800237-8.14044580e-01j],
[-1.63299316+0.00000000e+00j, 0.83243817+8.14044580e-01j,
0.83243817-8.14044580e-01j],
[-3.26598632+0.00000000e+00j, 1.32044054-5.55111512e-16j,
1.32044054+5.55111512e-16j]])
'''
# 特征值与特征向量的乘积
e * v
'''
array([[-1.63299316+0.00000000e+00j, -0.48800237+8.14044580e-01j,
-0.48800237-8.14044580e-01j],
[-1.63299316+0.00000000e+00j, 0.83243817+8.14044580e-01j,
0.83243817-8.14044580e-01j],
[-3.26598632+0.00000000e+00j, 1.32044054-7.29317578e-16j,
1.32044054+7.29317578e-16j]])
'''
# 验证两个乘积是否相等
np.isclose(np.dot(A,v),(e * v))
'''
array([[ True, True, True],
[ True, True, True],
[ True, True, True]])
'''
# 行列式 |A - λE| 的值应为 0
np.linalg.det(A-np.eye(3,3)*e)
# 5.965152994198125e-14j x = np.matrix([[1,2,3],[4,5,6],[7,8,0]])
'''
matrix([[1, 2, 3],
[4, 5, 6],
[7, 8, 0]])
'''
# 逆矩阵
y = np.linalg.inv(x)
''' matrix([[-1.77777778, 0.88888889, -0.11111111],
[ 1.55555556, -0.77777778, 0.22222222],
[-0.11111111, 0.22222222, -0.11111111]])
注:numpy.linalg.LinAlgError: Singular matrix 矩阵不存在逆矩阵
'''
# 矩阵的乘法
x * y
'''
matrix([[ 1.00000000e+00, 5.55111512e-17, 1.38777878e-17],
[ 5.55111512e-17, 1.00000000e+00, 2.77555756e-17],
[ 1.77635684e-15, -8.88178420e-16, 1.00000000e+00]])
'''
y * x
'''
matrix([[ 1.00000000e+00, -1.11022302e-16, 0.00000000e+00],
[ 8.32667268e-17, 1.00000000e+00, 2.22044605e-16],
[ 6.93889390e-17, 0.00000000e+00, 1.00000000e+00]])
'''
# 求解线性方程组
a = np.array([[3,1],[1,2]])
'''
array([[3, 1],
[1, 2]])
'''
b = np.array([9,8])
# array([9, 8]) # 求解
x = np.linalg.solve(a,b)
# array([2., 3.]) # 验证
np.dot(a,x)
# array([9., 8.]) # 最小二乘解:返回解,余项,a 的秩,a 的奇异值
np.linalg.lstsq(a,b)
# (array([2., 3.]), array([], dtype=float64), 2, array([3.61803399, 1.38196601])) # 计算向量和矩阵的范数
x = np.matrix([[1,2],[3,-4]])
'''
matrix([[ 1, 2],
[ 3, -4]])
'''
np.linalg.norm(x)
# 5.477225575051661
np.linalg.norm(x,-2)
# 1.9543950758485487
np.linalg.norm(x,-1)
# 4.0
np.linalg.norm(x,1)
# 6.0
np.linalg.norm([1,2,0,3,4,0],0)
# 4.0
np.linalg.norm([1,2,0,3,4,0],2)
# 5.477225575051661 # 奇异值分解
a = np.matrix([[1,2,3],[4,5,6],[7,8,9]])
'''
matrix([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
'''
u,s,v = np.linalg.svd(a)
u
'''
matrix([[-0.21483724, 0.88723069, 0.40824829],
[-0.52058739, 0.24964395, -0.81649658],
[-0.82633754, -0.38794278, 0.40824829]])
'''
s
'''
array([1.68481034e+01, 1.06836951e+00, 4.41842475e-16])
'''
v
'''
matrix([[-0.47967118, -0.57236779, -0.66506441],
[-0.77669099, -0.07568647, 0.62531805],
[-0.40824829, 0.81649658, -0.40824829]])
'''
# 验证
u * np.diag(s) * v
'''
matrix([[1., 2., 3.],
[4., 5., 6.],
[7., 8., 9.]])
'''

pandas第三方库
# 一维数组与常用操作
import pandas as pd # 设置输出结果列对齐
pd.set_option('display.unicode.ambiguous_as_wide',True)
pd.set_option('display.unicode.east_asian_width',True) # 创建 从 0 开始的非负整数索引
s1 = pd.Series(range(1,20,5))
'''
0 1
1 6
2 11
3 16
dtype: int64
'''
# 使用字典创建 Series 字典的键作为索引
s2 = pd.Series({'语文':95,'数学':98,'Python':100,'物理':97,'化学':99})
'''
语文 95
数学 98
Python 100
物理 97
化学 99
dtype: int64
'''
# 修改 Series 对象的值
s1[3] = -17
'''
0 1
1 6
2 11
3 -17
dtype: int64
'''
s2['语文'] = 94
'''
语文 94
数学 98
Python 100
物理 97
化学 99
dtype: int64
'''
# 查看 s1 的绝对值
abs(s1)
'''
0 1
1 6
2 11
3 17
dtype: int64
'''
# 将 s1 所有的值都加 5
s1 + 5
'''
0 6
1 11
2 16
3 -12
dtype: int64
'''
# 在 s1 的索引下标前加入参数值
s1.add_prefix(2)
'''
20 1
21 6
22 11
23 -17
dtype: int64
'''
# s2 数据的直方图
s2.hist() # 每行索引后面加上 hany
s2.add_suffix('hany')
'''
语文hany 94
数学hany 98
Pythonhany 100
物理hany 97
化学hany 99
dtype: int64
'''
# 查看 s2 中最大值的索引
s2.argmax()
# 'Python' # 查看 s2 的值是否在指定区间内
s2.between(90,100,inclusive = True)
'''
语文 True
数学 True
Python True
物理 True
化学 True
dtype: bool
'''
# 查看 s2 中 97 分以上的数据
s2[s2 > 97]
'''
数学 98
Python 100
化学 99
dtype: int64
'''
# 查看 s2 中大于中值的数据
s2[s2 > s2.median()]
'''
Python 100
化学 99
dtype: int64
'''
# s2 与数字之间的运算,开平方 * 10 保留一位小数
round((s2**0.5)*10,1)
'''
语文 97.0
数学 99.0
Python 100.0
物理 98.5
化学 99.5
dtype: float64
'''
# s2 的中值
s2.median()
# 98.0 # s2 中最小的两个数
s2.nsmallest(2)
'''
语文 94
物理 97
dtype: int64
'''
# s2 中最大的两个数
s2.nlargest(2)
'''
Python 100
化学 99
dtype: int64
'''
# Series 对象之间的运算,对相同索引进行计算,不是相同索引的使用 NaN
pd.Series(range(5)) + pd.Series(range(5,10))
'''
0 5
1 7
2 9
3 11
4 13
dtype: int64
'''
# pipe 对 Series 对象使用匿名函数
pd.Series(range(5)).pipe(lambda x,y,z :(x**y)%z,2,5)
'''
0 0
1 1
2 4
3 4
4 1
dtype: int64
'''
pd.Series(range(5)).pipe(lambda x:x+3)
'''
0 3
1 4
2 5
3 6
4 7
dtype: int64
'''
pd.Series(range(5)).pipe(lambda x:x+3).pipe(lambda x:x*3)
'''
0 9
1 12
2 15
3 18
4 21
dtype: int64
'''
# 对 Series 对象使用匿名函数
pd.Series(range(5)).apply(lambda x:x+3)
'''
0 3
1 4
2 5
3 6
4 7
dtype: int64
'''
# 查看标准差
pd.Series(range(0,5)).std()
# 1.5811388300841898 # 查看无偏方差
pd.Series(range(0,5)).var()
# 2.5 # 查看无偏标准差
pd.Series(range(0,5)).sem()
# 0.7071067811865476 # 查看是否存在等价于 True 的值
any(pd.Series([3,0,True]))
# True # 查看是否所有的值都等价于 True
all(pd.Series([3,0,True]))
# False # 时间序列和常用操作
import pandas as pd # 每隔五天--5D
pd.date_range(start = '20200101',end = '20200131',freq = '5D')
'''
DatetimeIndex(['2020-01-01', '2020-01-06', '2020-01-11', '2020-01-16',
'2020-01-21', '2020-01-26', '2020-01-31'],
dtype='datetime64[ns]', freq='5D')
'''
# 每隔一周--W
pd.date_range(start = '20200301',end = '20200331',freq = 'W')
'''
DatetimeIndex(['2020-03-01', '2020-03-08', '2020-03-15', '2020-03-22',
'2020-03-29'],
dtype='datetime64[ns]', freq='W-SUN')
'''
# 间隔两天,五个数据
pd.date_range(start = '20200301',periods = 5,freq = '2D')
'''
DatetimeIndex(['2020-03-01', '2020-03-03', '2020-03-05', '2020-03-07',
'2020-03-09'],
dtype='datetime64[ns]', freq='2D')
'''
# 间隔三小时,八个数据
pd.date_range(start = '20200301',periods = 8,freq = '3H')
'''
DatetimeIndex(['2020-03-01 00:00:00', '2020-03-01 03:00:00',
'2020-03-01 06:00:00', '2020-03-01 09:00:00',
'2020-03-01 12:00:00', '2020-03-01 15:00:00',
'2020-03-01 18:00:00', '2020-03-01 21:00:00'],
dtype='datetime64[ns]', freq='3H')
'''
# 三点开始,十二个数据,间隔一分钟
pd.date_range(start = '202003010300',periods = 12,freq = 'T')
'''
DatetimeIndex(['2020-03-01 03:00:00', '2020-03-01 03:01:00',
'2020-03-01 03:02:00', '2020-03-01 03:03:00',
'2020-03-01 03:04:00', '2020-03-01 03:05:00',
'2020-03-01 03:06:00', '2020-03-01 03:07:00',
'2020-03-01 03:08:00', '2020-03-01 03:09:00',
'2020-03-01 03:10:00', '2020-03-01 03:11:00'],
dtype='datetime64[ns]', freq='T')
'''
# 每个月的最后一天
pd.date_range(start = '20190101',end = '20191231',freq = 'M')
'''
DatetimeIndex(['2019-01-31', '2019-02-28', '2019-03-31', '2019-04-30',
'2019-05-31', '2019-06-30', '2019-07-31', '2019-08-31',
'2019-09-30', '2019-10-31', '2019-11-30', '2019-12-31'],
dtype='datetime64[ns]', freq='M')
'''
# 间隔一年,六个数据,年末最后一天
pd.date_range(start = '20190101',periods = 6,freq = 'A')
'''
DatetimeIndex(['2019-12-31', '2020-12-31', '2021-12-31', '2022-12-31',
'2023-12-31', '2024-12-31'],
dtype='datetime64[ns]', freq='A-DEC')
'''
# 间隔一年,六个数据,年初最后一天
pd.date_range(start = '20200101',periods = 6,freq = 'AS')
'''
DatetimeIndex(['2020-01-01', '2021-01-01', '2022-01-01', '2023-01-01',
'2024-01-01', '2025-01-01'],
dtype='datetime64[ns]', freq='AS-JAN')
'''
# 使用 Series 对象包含时间序列对象,使用特定索引
data = pd.Series(index = pd.date_range(start = '20200321',periods = 24,freq = 'H'),data = range(24))
'''
2020-03-21 00:00:00 0
2020-03-21 01:00:00 1
2020-03-21 02:00:00 2
2020-03-21 03:00:00 3
2020-03-21 04:00:00 4
2020-03-21 05:00:00 5
2020-03-21 06:00:00 6
2020-03-21 07:00:00 7
2020-03-21 08:00:00 8
2020-03-21 09:00:00 9
2020-03-21 10:00:00 10
2020-03-21 11:00:00 11
2020-03-21 12:00:00 12
2020-03-21 13:00:00 13
2020-03-21 14:00:00 14
2020-03-21 15:00:00 15
2020-03-21 16:00:00 16
2020-03-21 17:00:00 17
2020-03-21 18:00:00 18
2020-03-21 19:00:00 19
2020-03-21 20:00:00 20
2020-03-21 21:00:00 21
2020-03-21 22:00:00 22
2020-03-21 23:00:00 23
Freq: H, dtype: int64
'''
# 查看前五个数据
data[:5]
'''
2020-03-21 00:00:00 0
2020-03-21 01:00:00 1
2020-03-21 02:00:00 2
2020-03-21 03:00:00 3
2020-03-21 04:00:00 4
Freq: H, dtype: int64
'''
# 三分钟重采样,计算均值
data.resample('3H').mean()
'''
2020-03-21 00:00:00 1
2020-03-21 03:00:00 4
2020-03-21 06:00:00 7
2020-03-21 09:00:00 10
2020-03-21 12:00:00 13
2020-03-21 15:00:00 16
2020-03-21 18:00:00 19
2020-03-21 21:00:00 22
Freq: 3H, dtype: int64
'''
# 五分钟重采样,求和
data.resample('5H').sum()
'''
2020-03-21 00:00:00 10
2020-03-21 05:00:00 35
2020-03-21 10:00:00 60
2020-03-21 15:00:00 85
2020-03-21 20:00:00 86
Freq: 5H, dtype: int64
'''
# 计算OHLC open,high,low,close
data.resample('5H').ohlc()
'''
open high low close
2020-03-21 00:00:00 0 4 0 4
2020-03-21 05:00:00 5 9 5 9
2020-03-21 10:00:00 10 14 10 14
2020-03-21 15:00:00 15 19 15 19
2020-03-21 20:00:00 20 23 20 23
'''
# 将日期替换为第二天
data.index = data.index + pd.Timedelta('1D')
# 查看前五条数据
data[:5]
'''
2020-03-22 00:00:00 0
2020-03-22 01:00:00 1
2020-03-22 02:00:00 2
2020-03-22 03:00:00 3
2020-03-22 04:00:00 4
Freq: H, dtype: int64
'''
# 查看指定日期是星期几
# pd.Timestamp('20200321').weekday_name
# 'Saturday' # 查看指定日期的年份是否是闰年
pd.Timestamp('20200301').is_leap_year
# True # 查看指定日期所在的季度和月份
day = pd.Timestamp('20200321')
# Timestamp('2020-03-21 00:00:00') # 查看日期的季度
day.quarter
# 1 # 查看日期所在的月份
day.month
# 3 # 转换为 python 的日期时间对象
day.to_pydatetime()
# datetime.datetime(2020, 3, 21, 0, 0) # DateFrame 的创建,包含部分:index , column , values
import numpy as np
import pandas as pd # 创建一个 DataFrame 对象
dataframe = pd.DataFrame(np.random.randint(1,20,(5,3)),
index = range(5),
columns = ['A','B','C'])
'''
A B C
0 17 9 19
1 14 5 8
2 7 18 13
3 13 16 2
4 18 6 5
'''
# 索引为时间序列
dataframe2 = pd.DataFrame(np.random.randint(5,15,(9,3)),
index = pd.date_range(start = '202003211126',
end = '202003212000',
freq = 'H'),
columns = ['Pandas','爬虫','比赛'])
'''
Pandas 爬虫 比赛
2020-03-21 11:26:00 8 10 8
2020-03-21 12:26:00 9 14 9
2020-03-21 13:26:00 9 5 13
2020-03-21 14:26:00 9 7 7
2020-03-21 15:26:00 11 10 14
2020-03-21 16:26:00 12 7 10
2020-03-21 17:26:00 11 11 13
2020-03-21 18:26:00 8 13 8
2020-03-21 19:26:00 7 7 13
'''
# 使用字典进行创建
dataframe3 = pd.DataFrame({'语文':[87,79,67,92],
'数学':[93,89,80,77],
'英语':[88,95,76,77]},
index = ['张三','李四','王五','赵六'])
'''
语文 数学 英语
张三 87 93 88
李四 79 89 95
王五 67 80 76
赵六 92 77 77
'''
# 创建时自动扩充
dataframe4 = pd.DataFrame({'A':range(5,10),'B':3})
'''
A B
0 5 3
1 6 3
2 7 3
3 8 3
4 9 3
''' # C:\Users\lenovo\Desktop\总结\Python
# 读取 Excel 文件并进行筛选 import pandas as pd # 设置列对齐
pd.set_option("display.unicode.ambiguous_as_wide",True)
pd.set_option("display.unicode.east_asian_width",True) # 读取工号姓名时段交易额,使用默认索引
dataframe = pd.read_excel(r'C:\Users\lenovo\Desktop\总结\Python\超市营业额.xlsx',
usecols = ['工号','姓名','时段','交易额']) # 打印前十行数据
dataframe[:10]
'''
工号 姓名 时段 交易额
0 1001 张三 9:00-14:00 2000
1 1002 李四 14:00-21:00 1800
2 1003 王五 9:00-14:00 800
3 1004 赵六 14:00-21:00 1100
4 1005 周七 9:00-14:00 600
5 1006 钱八 14:00-21:00 700
6 1006 钱八 9:00-14:00 850
7 1001 张三 14:00-21:00 600
8 1001 张三 9:00-14:00 1300
9 1002 李四 14:00-21:00 1500
'''
# 跳过 1 2 4 行,以第一列姓名为索引
dataframe2 = pd.read_excel(r'C:\Users\lenovo\Desktop\总结\Python\超市营业额.xlsx',
skiprows = [1,2,4],
index_col = 1)
'''注:张三李四赵六的第一条数据跳过
工号 日期 时段 交易额 柜台
姓名
王五 1003 20190301 9:00-14:00 800 食品
周七 1005 20190301 9:00-14:00 600 日用品
钱八 1006 20190301 14:00-21:00 700 日用品
钱八 1006 20190301 9:00-14:00 850 蔬菜水果
张三 1001 20190302 14:00-21:00 600 蔬菜水果
''' # 筛选符合特定条件的数据
# 读取超市营业额数据
dataframe = pd.read_excel(r'C:\Users\lenovo\Desktop\总结\Python\超市营业额.xlsx') # 查看 5 到 10 的数据
dataframe[5:11]
'''
工号 姓名 日期 时段 交易额 柜台
5 1006 钱八 20190301 14:00-21:00 700 日用品
6 1006 钱八 20190301 9:00-14:00 850 蔬菜水果
7 1001 张三 20190302 14:00-21:00 600 蔬菜水果
8 1001 张三 20190302 9:00-14:00 1300 化妆品
9 1002 李四 20190302 14:00-21:00 1500 化妆品
10 1003 王五 20190302 9:00-14:00 1000 食品
'''
# 查看第六行的数据,左闭右开
dataframe.iloc[5]
'''
工号 1006
姓名 钱八
时段 14:00-21:00
交易额 700
Name: 5, dtype: object
'''
dataframe[:5]
'''
工号 姓名 时段 交易额
0 1001 张三 9:00-14:00 2000
1 1002 李四 14:00-21:00 1800
2 1003 王五 9:00-14:00 800
3 1004 赵六 14:00-21:00 1100
4 1005 周七 9:00-14:00 600
'''
# 查看第 1 3 4 行的数据
dataframe.iloc[[0,2,3],:]
'''
工号 姓名 时段 交易额
0 1001 张三 9:00-14:00 2000
2 1003 王五 9:00-14:00 800
3 1004 赵六 14:00-21:00 1100
'''
# 查看第 1 3 4 行的第 1 2 列
dataframe.iloc[[0,2,3],[0,1]]
'''
工号 姓名
0 1001 张三
2 1003 王五
3 1004 赵六
'''
# 查看前五行指定,姓名、时段和交易额的数据
dataframe[['姓名','时段','交易额']][:5]
'''
姓名 时段 交易额
0 张三 9:00-14:00 2000
1 李四 14:00-21:00 1800
2 王五 9:00-14:00 800
3 赵六 14:00-21:00 1100
4 周七 9:00-14:00 600
'''
dataframe[:5][['姓名','时段','交易额']]
'''
姓名 时段 交易额
0 张三 9:00-14:00 2000
1 李四 14:00-21:00 1800
2 王五 9:00-14:00 800
3 赵六 14:00-21:00 1100
4 周七 9:00-14:00 600
'''
# 查看第 2 4 5 行 姓名,交易额 数据 loc 函数,包含结尾
dataframe.loc[[1,3,4],['姓名','交易额']]
'''
姓名 交易额
1 李四 1800
3 赵六 1100
4 周七 600
'''
# 查看第四行的姓名数据
dataframe.at[3,'姓名']
# '赵六' # 查看交易额大于 1700 的数据
dataframe[dataframe['交易额'] > 1700]
'''
工号 姓名 时段 交易额
0 1001 张三 9:00-14:00 2000
1 1002 李四 14:00-21:00 1800
'''
# 查看交易额总和
dataframe.sum()
'''
工号 17055
姓名 张三李四王五赵六周七钱八钱八张三张三李四王五赵六周七钱八李四王五张三...
时段 9:00-14:0014:00-21:009:00-14:0014:00-21:009:00...
交易额 17410
dtype: object
'''
# 某一时段的交易总和
dataframe[dataframe['时段'] == '14:00-21:00']['交易额'].sum()
# 8300 # 查看张三在下午14:00之后的交易情况
dataframe[(dataframe.姓名 == '张三') & (dataframe.时段 == '14:00-21:00')][:10]
'''
工号 姓名 时段 交易额
7 1001 张三 14:00-21:00 600
'''
# 查看日用品的销售总额
# dataframe[dataframe['柜台'] == '日用品']['交易额'].sum() # 查看张三总共的交易额
dataframe[dataframe['姓名'].isin(['张三'])]['交易额'].sum()
# 5200 # 查看交易额在 1500~3000 之间的记录
dataframe[dataframe['交易额'].between(1500,3000)]
'''
工号 姓名 时段 交易额
0 1001 张三 9:00-14:00 2000
1 1002 李四 14:00-21:00 1800
9 1002 李四 14:00-21:00 1500
''' # 查看数据特征和统计信息
import pandas as pd
# 读取文件
dataframe = pd.read_excel(r'C:\Users\lenovo\Desktop\总结\Python\超市营业额.xlsx') # 查看所有的交易额信息
dataframe['交易额'].describe()
'''
count 17.000000
mean 1024.117647
std 428.019550
min 580.000000
25% 700.000000
50% 850.000000
75% 1300.000000
max 2000.000000
Name: 交易额, dtype: float64
'''
# 查看四分位数
dataframe['交易额'].quantile([0,0.25,0.5,0.75,1.0])
'''
0.00 580.0
0.25 700.0
0.50 850.0
0.75 1300.0
1.00 2000.0
Name: 交易额, dtype: float64
'''
# 交易额中值
dataframe['交易额'].median()
# 850.0 # 交易额最小的三个数据
dataframe['交易额'].nsmallest(3)
'''
12 580
4 600
7 600
Name: 交易额, dtype: int64
'''
dataframe.nsmallest(3,'交易额')
'''
工号 姓名 日期 时段 交易额 柜台
12 1005 周七 20190302 9:00-14:00 580 日用品
4 1005 周七 20190301 9:00-14:00 600 日用品
7 1001 张三 20190302 14:00-21:00 600 蔬菜水果
'''
# 交易额最大的两个数据
dataframe['交易额'].nlargest(2)
'''
0 2000
1 1800
Name: 交易额, dtype: int64
'''
dataframe.nlargest(2,'交易额')
'''
工号 姓名 日期 时段 交易额 柜台
0 1001 张三 20190301 9:00-14:00 2000 化妆品
1 1002 李四 20190301 14:00-21:00 1800 化妆品
'''
# 查看最后一个日期
dataframe['日期'].max()
# 20190303 # 查看最小的工号
dataframe['工号'].min()
# 1001 # 第一个最小交易额的行下标
index = dataframe['交易额'].idxmin()
# 0 # 第一个最小交易额
dataframe.loc[index,'交易额']
# 580 # 最大交易额的行下标
index = dataframe['交易额'].idxmax()
dataframe.loc[index,'交易额']
# 2000 import pandas as pd # 设置列对齐
pd.set_option("display.unicode.ambiguous_as_wide",True)
pd.set_option("display.unicode.east_asian_width",True) # 读取工号姓名时段交易额,使用默认索引
dataframe = pd.read_excel(r'C:\Users\lenovo\Desktop\总结\Python\超市营业额.xlsx',
usecols = ['工号','姓名','时段','交易额','柜台'])
dataframe[:5]
'''
工号 姓名 时段 交易额 柜台
0 1001 张三 9:00-14:00 2000 化妆品
1 1002 李四 14:00-21:00 1800 化妆品
2 1003 王五 9:00-14:00 800 食品
3 1004 赵六 14:00-21:00 1100 食品
4 1005 周七 9:00-14:00 600 日用品
'''
# 按照交易额和工号降序排序,查看五条数据
dataframe.sort_values(by = ['交易额','工号'],ascending = False)[:5]
'''
工号 姓名 时段 交易额 柜台
0 1001 张三 9:00-14:00 2000 化妆品
1 1002 李四 14:00-21:00 1800 化妆品
9 1002 李四 14:00-21:00 1500 化妆品
8 1001 张三 9:00-14:00 1300 化妆品
16 1001 张三 9:00-14:00 1300 化妆品
'''
# 按照交易额和工号升序排序,查看五条数据
dataframe.sort_values(by = ['交易额','工号'])[:5]
'''
工号 姓名 时段 交易额 柜台
12 1005 周七 9:00-14:00 580 日用品
7 1001 张三 14:00-21:00 600 蔬菜水果
4 1005 周七 9:00-14:00 600 日用品
14 1002 李四 9:00-14:00 680 蔬菜水果
5 1006 钱八 14:00-21:00 700 日用品
'''
# 按照交易额降序和工号升序排序,查看五条数据
dataframe.sort_values(by = ['交易额','工号'],ascending = [False,True])[:5]
'''
工号 姓名 时段 交易额 柜台
0 1001 张三 9:00-14:00 2000 化妆品
1 1002 李四 14:00-21:00 1800 化妆品
9 1002 李四 14:00-21:00 1500 化妆品
8 1001 张三 9:00-14:00 1300 化妆品
16 1001 张三 9:00-14:00 1300 化妆品
'''
# 按工号升序排序
dataframe.sort_values(by = ['工号'])[:5]
'''
工号 姓名 时段 交易额 柜台
0 1001 张三 9:00-14:00 2000 化妆品
7 1001 张三 14:00-21:00 600 蔬菜水果
8 1001 张三 9:00-14:00 1300 化妆品
16 1001 张三 9:00-14:00 1300 化妆品
1 1002 李四 14:00-21:00 1800 化妆品
'''
dataframe.sort_values(by = ['工号'],na_position = 'last')[:5]
'''
工号 姓名 时段 交易额 柜台
0 1001 张三 9:00-14:00 2000 化妆品
7 1001 张三 14:00-21:00 600 蔬菜水果
8 1001 张三 9:00-14:00 1300 化妆品
16 1001 张三 9:00-14:00 1300 化妆品
1 1002 李四 14:00-21:00 1800 化妆品
'''
# 按列名升序排序
dataframe.sort_index(axis = 1)[:5]
'''
交易额 姓名 工号 时段 柜台
0 2000 张三 1001 9:00-14:00 化妆品
1 1800 李四 1002 14:00-21:00 化妆品
2 800 王五 1003 9:00-14:00 食品
3 1100 赵六 1004 14:00-21:00 食品
4 600 周七 1005 9:00-14:00 日用品
'''
dataframe.sort_index(axis = 1,ascending = True)[:5]
'''
交易额 姓名 工号 时段 柜台
0 2000 张三 1001 9:00-14:00 化妆品
1 1800 李四 1002 14:00-21:00 化妆品
2 800 王五 1003 9:00-14:00 食品
3 1100 赵六 1004 14:00-21:00 食品
4 600 周七 1005 9:00-14:00 日用品
''' # 分组与聚合
import pandas as pd
import numpy as np # 设置列对齐
pd.set_option("display.unicode.ambiguous_as_wide",True)
pd.set_option("display.unicode.east_asian_width",True) # 读取工号姓名时段交易额,使用默认索引
dataframe = pd.read_excel(r'C:\Users\lenovo\Desktop\总结\Python\超市营业额.xlsx',
usecols = ['工号','姓名','时段','交易额','柜台']) # 对 5 的余数进行分组
dataframe.groupby(by = lambda num:num % 5)['交易额'].sum()
'''
0 4530
1 5000
2 1980
3 3120
4 2780
Name: 交易额, dtype: int64
'''
# 查看索引为 7 15 的交易额
dataframe.groupby(by = {7:'索引为7的行',15:'索引为15的行'})['交易额'].sum()
'''
索引为15的行 830
索引为7的行 600
Name: 交易额, dtype: int64
'''
# 查看不同时段的交易总额
dataframe.groupby(by = '时段')['交易额'].sum()
'''
时段
14:00-21:00 8300
9:00-14:00 9110
Name: 交易额, dtype: int64
'''
# 各柜台的销售总额
dataframe.groupby(by = '柜台')['交易额'].sum()
'''
柜台
化妆品 7900
日用品 2600
蔬菜水果 2960
食品 3950
Name: 交易额, dtype: int64
'''
# 查看每个人在每个时段购买的次数
count = dataframe.groupby(by = '姓名')['时段'].count()
'''
姓名
周七 2
张三 4
李四 3
王五 3
赵六 2
钱八 3
Name: 时段, dtype: int64
'''
#
count.name = '交易人和次数'
''' '''
# 每个人的交易额平均值并排序
dataframe.groupby(by = '姓名')['交易额'].mean().round(2).sort_values()
'''
姓名
周七 590.00
钱八 756.67
王五 876.67
赵六 1075.00
张三 1300.00
李四 1326.67
Name: 交易额, dtype: float64
'''
# 每个人的交易额
dataframe.groupby(by = '姓名').sum()['交易额'].apply(int)
'''
姓名
周七 1180
张三 5200
李四 3980
王五 2630
赵六 2150
钱八 2270
Name: 交易额, dtype: int64
'''
# 每一个员工交易额的中值
data = dataframe.groupby(by = '姓名').median()
'''
工号 交易额
姓名
周七 1005 590
张三 1001 1300
李四 1002 1500
王五 1003 830
赵六 1004 1075
钱八 1006 720
'''
data['交易额']
'''
姓名
周七 590
张三 1300
李四 1500
王五 830
赵六 1075
钱八 720
Name: 交易额, dtype: int64
'''
# 查看交易额对应的排名
data['排名'] = data['交易额'].rank(ascending = False)
data[['交易额','排名']]
'''
交易额 排名
姓名
周七 590 6.0
张三 1300 2.0
李四 1500 1.0
王五 830 4.0
赵六 1075 3.0
钱八 720 5.0
'''
# 每个人不同时段的交易额
dataframe.groupby(by = ['姓名','时段'])['交易额'].sum()
'''
姓名 时段
周七 9:00-14:00 1180
张三 14:00-21:00 600
9:00-14:00 4600
李四 14:00-21:00 3300
9:00-14:00 680
王五 14:00-21:00 830
9:00-14:00 1800
赵六 14:00-21:00 2150
钱八 14:00-21:00 1420
9:00-14:00 850
Name: 交易额, dtype: int64
'''
# 设置各时段累计
dataframe.groupby(by = ['姓名'])['时段','交易额'].aggregate({'交易额':np.sum,'时段':lambda x:'各时段累计'})
'''
交易额 时段
姓名
周七 1180 各时段累计
张三 5200 各时段累计
李四 3980 各时段累计
王五 2630 各时段累计
赵六 2150 各时段累计
钱八 2270 各时段累计
'''
# 对指定列进行聚合,查看最大,最小,和,平均值,中值
dataframe.groupby(by = '姓名').agg(['max','min','sum','mean','median'])
'''
工号 交易额
max min sum mean median max min sum mean median
姓名
周七 1005 1005 2010 1005 1005 600 580 1180 590.000000 590
张三 1001 1001 4004 1001 1001 2000 600 5200 1300.000000 1300
李四 1002 1002 3006 1002 1002 1800 680 3980 1326.666667 1500
王五 1003 1003 3009 1003 1003 1000 800 2630 876.666667 830
赵六 1004 1004 2008 1004 1004 1100 1050 2150 1075.000000 1075
钱八 1006 1006 3018 1006 1006 850 700 2270 756.666667 720
'''
# 查看部分聚合后的结果
dataframe.groupby(by = '姓名').agg(['max','min','sum','mean','median'])['交易额']
'''
max min sum mean median
姓名
周七 600 580 1180 590.000000 590
张三 2000 600 5200 1300.000000 1300
李四 1800 680 3980 1326.666667 1500
王五 1000 800 2630 876.666667 830
赵六 1100 1050 2150 1075.000000 1075
钱八 850 700 2270 756.666667 720
''' # 处理异常值缺失值重复值数据差分
import pandas as pd
import numpy as np
import copy # 设置列对齐
pd.set_option("display.unicode.ambiguous_as_wide",True)
pd.set_option("display.unicode.east_asian_width",True) # 异常值 # 读取工号姓名时段交易额,使用默认索引
dataframe = pd.read_excel(r'C:\Users\lenovo\Desktop\总结\Python\超市营业额.xlsx') # 查看交易额低于 2000 的三条数据
# dataframe[dataframe.交易额 < 2000]
dataframe[dataframe.交易额 < 2000][:3]
'''
工号 姓名 日期 时段 交易额 柜台
1 1002 李四 20190301 14:00-21:00 1800 化妆品
2 1003 王五 20190301 9:00-14:00 800 食品
3 1004 赵六 20190301 14:00-21:00 1100 食品
'''
# 查看上浮了 50% 之后依旧低于 1500 的交易额,查看 4 条数据
dataframe.loc[dataframe.交易额 < 1500,'交易额'] = dataframe[dataframe.交易额 < 1500]['交易额'].map(lambda num:num*1.5)
dataframe[dataframe.交易额 < 1500][:4]
'''
工号 姓名 日期 时段 交易额 柜台
2 1003 王五 20190301 9:00-14:00 1200.0 食品
4 1005 周七 20190301 9:00-14:00 900.0 日用品
5 1006 钱八 20190301 14:00-21:00 1050.0 日用品
6 1006 钱八 20190301 9:00-14:00 1275.0 蔬菜水果
'''
# 查看交易额大于 2500 的数据
dataframe[dataframe.交易额 > 2500]
'''
Empty DataFrame
Columns: [工号, 姓名, 日期, 时段, 交易额, 柜台]
Index: []
'''
# 查看交易额低于 900 或 高于 1800 的数据
dataframe[(dataframe.交易额 < 900)|(dataframe.交易额 > 1800)]
'''
工号 姓名 日期 时段 交易额 柜台
0 1001 张三 20190301 9:00-14:00 2000.0 化妆品
8 1001 张三 20190302 9:00-14:00 1950.0 化妆品
12 1005 周七 20190302 9:00-14:00 870.0 日用品
16 1001 张三 20190303 9:00-14:00 1950.0 化妆品
'''
# 将所有低于 200 的交易额都替换成 200 处理异常值
dataframe.loc[dataframe.交易额 < 200,'交易额'] = 200 # 查看低于 1500 的交易额个数
dataframe.loc[dataframe.交易额 < 1500,'交易额'].count()
# 9 # 将大于 3000 元的都替换为 3000 元
dataframe.loc[dataframe.交易额 > 3000,'交易额'] = 3000 # 缺失值 # 查看有多少行数据
len(dataframe)
# 17 # 丢弃缺失值之后的行数
len(dataframe.dropna())
# 17 # 包含缺失值的行
dataframe[dataframe['交易额'].isnull()]
'''
Empty DataFrame
Columns: [工号, 姓名, 日期, 时段, 交易额, 柜台]
Index: []
'''
# 使用固定值替换缺失值
# dff = copy.deepcopy(dataframe)
# dff.loc[dff.交易额.isnull(),'交易额'] = 999
# 将缺失值设定为 999,包含结尾
# dff.iloc[[1,4,17],:] # 使用交易额的均值替换缺失值
# dff = copy.deepcopy(dataframe)
# for i in dff[dff.交易额.isnull()].index:
# dff.loc[i,'交易额'] = round(dff.loc[dff.姓名 == dff.loc[i,'姓名'],'交易额'].mean())
# dff.iloc[[1,4,17],:] # 使用整体均值的 80% 填充缺失值
# dataframe.fillna({'交易额':round(dataframe['交易额'].mean() * 0.8)},inplace = True)
# dataframe.iloc[[1,4,16],:] # 重复值
dataframe[dataframe.duplicated()]
'''
Empty DataFrame
Columns: [工号, 姓名, 日期, 时段, 交易额, 柜台]
Index: []
'''
# dff = dataframe[['工号','姓名','日期','交易额']]
# dff = dff[dff.duplicated()]
# for row in dff.values:
# df[(df.工号 == row[0]) & (df.日期 == row[2]) &(df.交易额 == row[3])] # 丢弃重复行
dataframe = dataframe.drop_duplicates() # 查看是否有录入错误的工号和姓名
dff = dataframe[['工号','姓名']]
dff.drop_duplicates()
'''
工号 姓名
0 1001 张三
1 1002 李四
2 1003 王五
3 1004 赵六
4 1005 周七
5 1006 钱八
''' # 数据差分
# 查看员工业绩波动情况(每一天和昨天的数据作比较)
dff = dataframe.groupby(by = '日期').sum()['交易额'].diff()
'''
日期
20190301 NaN
20190302 1765.0
20190303 -9690.0
Name: 交易额, dtype: float64
'''
# [:5] dataframe.head()
dff.map(lambda num:'%.2f'%(num))[:5]
'''
日期
20190301 nan
20190302 1765.00
20190303 -9690.00
Name: 交易额, dtype: object
'''
# 查看张三的波动情况
dataframe[dataframe.姓名 == '张三'].groupby(by = '日期').sum()['交易额'].diff()[:5]
'''
日期
20190301 NaN
20190302 850.0
20190303 -900.0
Name: 交易额, dtype: float64
''' # 使用透视表与交叉表查看业绩汇总数据
import pandas as pd
import numpy as np
import copy # 设置列对齐
pd.set_option("display.unicode.ambiguous_as_wide",True)
pd.set_option("display.unicode.east_asian_width",True) dataframe = pd.read_excel(r'C:\Users\lenovo\Desktop\总结\Python\超市营业额.xlsx')
# 对姓名和日期进行分组,并进行求和
dff = dataframe.groupby(by = ['姓名','日期'],as_index = False).sum()
'''
姓名 日期 工号 交易额
0 周七 20190301 1005 600
1 周七 20190302 1005 580
2 张三 20190301 1001 2000
3 张三 20190302 2002 1900
4 张三 20190303 1001 1300
5 李四 20190301 1002 1800
6 李四 20190302 2004 2180
7 王五 20190301 1003 800
8 王五 20190302 2006 1830
9 赵六 20190301 1004 1100
10 赵六 20190302 1004 1050
11 钱八 20190301 2012 1550
12 钱八 20190302 1006 720
'''
# 将 dff 的索引,列 设置成透视表形式
dff = dff.pivot(index = '姓名',columns = '日期',values = '交易额')
'''
日期 20190301 20190302 20190303
姓名
周七 600.0 580.0 NaN
张三 2000.0 1900.0 1300.0
李四 1800.0 2180.0 NaN
王五 800.0 1830.0 NaN
赵六 1100.0 1050.0 NaN
钱八 1550.0 720.0 NaN
'''
# 查看前一天的数据
dff.iloc[:,:1]
'''
日期 20190301
姓名
周七 600.0
张三 2000.0
李四 1800.0
王五 800.0
赵六 1100.0
钱八 1550.0
'''
# 交易总额小于 4000 的人的前三天业绩
dff[dff.sum(axis = 1) < 4000].iloc[:,:3]
'''
日期 20190301 20190302 20190303
姓名
周七 600.0 580.0 NaN
李四 1800.0 2180.0 NaN
王五 800.0 1830.0 NaN
赵六 1100.0 1050.0 NaN
钱八 1550.0 720.0 NaN
'''
# 工资总额大于 2900 元的员工的姓名
dff[dff.sum(axis = 1) > 2900].index.values
# array(['张三', '李四'], dtype=object) # 显示前两天每一天的交易总额以及每个人的交易金额
dataframe.pivot_table(values = '交易额',index = '姓名',columns = '日期',aggfunc = 'sum',margins = True).iloc[:,:2]
'''
日期 20190301 20190302
姓名
周七 600.0 580.0
张三 2000.0 1900.0
李四 1800.0 2180.0
王五 800.0 1830.0
赵六 1100.0 1050.0
钱八 1550.0 720.0
All 7850.0 8260.0
'''
# 显示每个人在每个柜台的交易总额
dff = dataframe.groupby(by = ['姓名','柜台'],as_index = False).sum()
dff.pivot(index = '姓名',columns = '柜台',values = '交易额')
'''
柜台 化妆品 日用品 蔬菜水果 食品
姓名
周七 NaN 1180.0 NaN NaN
张三 4600.0 NaN 600.0 NaN
李四 3300.0 NaN 680.0 NaN
王五 NaN NaN 830.0 1800.0
赵六 NaN NaN NaN 2150.0
钱八 NaN 1420.0 850.0 NaN
'''
# 查看每人每天的上班次数
dataframe.pivot_table(values = '交易额',index = '姓名',columns = '日期',aggfunc = 'count',margins = True).iloc[:,:1]
'''
日期 20190301
姓名
周七 1.0
张三 1.0
李四 1.0
王五 1.0
赵六 1.0
钱八 2.0
All 7.0
'''
# 查看每个人每天购买的次数
dataframe.pivot_table(values = '交易额',index = '姓名',columns = '日期',aggfunc = 'count',margins = True)
'''
日期 20190301 20190302 20190303 All
姓名
周七 1.0 1.0 NaN 2
张三 1.0 2.0 1.0 4
李四 1.0 2.0 NaN 3
王五 1.0 2.0 NaN 3
赵六 1.0 1.0 NaN 2
钱八 2.0 1.0 NaN 3
All 7.0 9.0 1.0 17
'''
# 交叉表
# 每个人每天上过几次班
pd.crosstab(dataframe.姓名,dataframe.日期,margins = True).iloc[:,:2]
'''
日期 20190301 20190302
姓名
周七 1 1
张三 1 2
李四 1 2
王五 1 2
赵六 1 1
钱八 2 1
All 7 9
'''
# 每个人每天去过几次柜台
pd.crosstab(dataframe.姓名,dataframe.柜台)
'''
柜台 化妆品 日用品 蔬菜水果 食品
姓名
周七 0 2 0 0
张三 3 0 1 0
李四 2 0 1 0
王五 0 0 1 2
赵六 0 0 0 2
钱八 0 2 1 0
'''
# 将每一个人在每一个柜台的交易总额显示出来
pd.crosstab(dataframe.姓名,dataframe.柜台,dataframe.交易额,aggfunc='sum')
'''
柜台 化妆品 日用品 蔬菜水果 食品
姓名
周七 NaN 1180.0 NaN NaN
张三 4600.0 NaN 600.0 NaN
李四 3300.0 NaN 680.0 NaN
王五 NaN NaN 830.0 1800.0
赵六 NaN NaN NaN 2150.0
钱八 NaN 1420.0 850.0 NaN
'''
# 每个人在每个柜台交易额的平均值,金额/天数
pd.crosstab(dataframe.姓名,dataframe.柜台,dataframe.交易额,aggfunc = 'mean').apply(lambda num:round(num,2) )
'''
柜台 化妆品 日用品 蔬菜水果 食品
姓名
周七 NaN 590.0 NaN NaN
张三 1533.33 NaN 600.0 NaN
李四 1650.00 NaN 680.0 NaN
王五 NaN NaN 830.0 900.0
赵六 NaN NaN NaN 1075.0
钱八 NaN 710.0 850.0 NaN
''' # 重采样 多索引 标准差 协方差
import pandas as pd
import numpy as np
import copy # 设置列对齐
pd.set_option("display.unicode.ambiguous_as_wide",True)
pd.set_option("display.unicode.east_asian_width",True) data = pd.read_excel(r'C:\Users\lenovo\Desktop\总结\Python\超市营业额.xlsx') # 将日期设置为 python 中的日期类型
data.日期 = pd.to_datetime(data.日期)
'''
工号 姓名 日期 时段 交易额 柜台
0 1001 张三 1970-01-01 00:00:00.020190301 9:00-14:00 2000 化妆品
1 1002 李四 1970-01-01 00:00:00.020190301 14:00-21:00 1800 化妆品
2 1003 王五 1970-01-01 00:00:00.020190301 9:00-14:00 800 食品
'''
# 每七天营业的总额
data.resample('7D',on = '日期').sum()['交易额']
'''
日期
1970-01-01 17410
Freq: 7D, Name: 交易额, dtype: int64
'''
# 每七天营业总额
data.resample('7D',on = '日期',label = 'right').sum()['交易额']
'''
日期
1970-01-08 17410
Freq: 7D, Name: 交易额, dtype: int64
'''
# 每七天营业额的平均值
func = lambda item:round(np.sum(item)/len(item),2)
data.resample('7D',on = '日期',label = 'right').apply(func)['交易额']
'''
日期
1970-01-08 1024.12
Freq: 7D, Name: 交易额, dtype: float64
'''
# 每七天营业额的平均值
func = lambda num:round(num,2)
data.resample('7D',on = '日期',label = 'right').mean().apply(func)['交易额']
# 1024.12 # 删除工号这一列
data.drop('工号',axis = 1,inplace = True)
data[:2]
'''
姓名 日期 时段 交易额 柜台
0 张三 1970-01-01 00:00:00.020190301 9:00-14:00 2000 化妆品
1 李四 1970-01-01 00:00:00.020190301 14:00-21:00 1800 化妆品
'''
# 按照姓名和柜台进行分组汇总
data = data.groupby(by = ['姓名','柜台']).sum()[:3]
'''
交易额
姓名 柜台
周七 日用品 1180
张三 化妆品 4600
蔬菜水果 600
'''
# 查看张三的汇总数据
data.loc['张三',:]
'''
交易额
柜台
化妆品 4600
蔬菜水果 600
'''
# 查看张三在蔬菜水果的交易数据
data.loc['张三','蔬菜水果']
'''
交易额 600
Name: (张三, 蔬菜水果), dtype: int64
'''
# 多索引
# 重新读取,使用第二列和第六列作为索引,排在前面
data = pd.read_excel(r'C:\Users\lenovo\Desktop\总结\Python\超市营业额.xlsx',index_col = [1,5])
data[:5]
'''
工号 日期 时段 交易额
姓名 柜台
张三 化妆品 1001 20190301 9:00-14:00 2000
李四 化妆品 1002 20190301 14:00-21:00 1800
王五 食品 1003 20190301 9:00-14:00 800
赵六 食品 1004 20190301 14:00-21:00 1100
周七 日用品 1005 20190301 9:00-14:00 600
'''
# 丢弃工号列
data.drop('工号',axis = 1,inplace = True)
data[:5]
'''
日期 时段 交易额
姓名 柜台
张三 化妆品 20190301 9:00-14:00 2000
李四 化妆品 20190301 14:00-21:00 1800
王五 食品 20190301 9:00-14:00 800
赵六 食品 20190301 14:00-21:00 1100
周七 日用品 20190301 9:00-14:00 600
'''
# 按照柜台进行排序
dff = data.sort_index(level = '柜台',axis = 0)
dff[:5]
'''
工号 日期 时段 交易额
姓名 柜台
张三 化妆品 1001 20190301 9:00-14:00 2000
化妆品 1001 20190302 9:00-14:00 1300
化妆品 1001 20190303 9:00-14:00 1300
李四 化妆品 1002 20190301 14:00-21:00 1800
化妆品 1002 20190302 14:00-21:00 1500
'''
# 按照姓名进行排序
dff = data.sort_index(level = '姓名',axis = 0)
dff[:5]
'''
工号 日期 时段 交易额
姓名 柜台
周七 日用品 1005 20190301 9:00-14:00 600
日用品 1005 20190302 9:00-14:00 580
张三 化妆品 1001 20190301 9:00-14:00 2000
化妆品 1001 20190302 9:00-14:00 1300
化妆品 1001 20190303 9:00-14:00 1300
'''
# 按照柜台进行分组求和
dff = data.groupby(level = '柜台').sum()['交易额']
'''
柜台
化妆品 7900
日用品 2600
蔬菜水果 2960
食品 3950
Name: 交易额, dtype: int64
'''
#标准差
data = pd.DataFrame({'A':[3,3,3,3,3],'B':[1,2,3,4,5],
'C':[-5,-4,1,4,5],'D':[-45,15,63,40,50]
})
'''
A B C D
0 3 1 -5 -45
1 3 2 -4 15
2 3 3 1 63
3 3 4 4 40
4 3 5 5 50
'''
# 平均值
data.mean()
'''
A 3.0
B 3.0
C 0.2
D 24.6
dtype: float64
'''
# 标准差
data.std()
'''
A 0.000000
B 1.581139
C 4.549725
D 42.700117
dtype: float64
'''
# 标准差的平方
data.std()**2
'''
A 0.0
B 2.5
C 20.7
D 1823.3
dtype: float64
'''
# 协方差
data.cov()
'''
A B C D
A 0.0 0.00 0.00 0.00
B 0.0 2.50 7.00 53.75
C 0.0 7.00 20.70 153.35
D 0.0 53.75 153.35 1823.30
'''
# 指定索引为 姓名,日期,时段,柜台,交易额
data = pd.read_excel(r'C:\Users\lenovo\Desktop\总结\Python\超市营业额.xlsx',
usecols = ['姓名','日期','时段','柜台','交易额'])
# 删除缺失值和重复值,inplace = True 直接丢弃
data.dropna(inplace = True)
data.drop_duplicates(inplace = True) # 处理异常值
data.loc[data.交易额 < 200,'交易额'] = 200
data.loc[data.交易额 > 3000,'交易额'] = 3000 # 使用交叉表得到不同员工在不同柜台的交易额平均值
dff = pd.crosstab(data.姓名,data.柜台,data.交易额,aggfunc = 'mean')
dff[:5]
'''
柜台 化妆品 日用品 蔬菜水果 食品
姓名
周七 NaN 590.0 NaN NaN
张三 1533.333333 NaN 600.0 NaN
李四 1650.000000 NaN 680.0 NaN
王五 NaN NaN 830.0 900.0
赵六 NaN NaN NaN 1075.0
'''
# 查看数据的标准差
dff.std()
'''
柜台
化妆品 82.495791
日用品 84.852814
蔬菜水果 120.277457
食品 123.743687
dtype: float64
'''
dff.cov()
'''
柜台 化妆品 日用品 蔬菜水果 食品
柜台
化妆品 6805.555556 NaN 4666.666667 NaN
日用品 NaN 7200.0 NaN NaN
蔬菜水果 4666.666667 NaN 14466.666667 NaN
食品 NaN NaN NaN 15312.5
''' import pandas as pd
import copy # 设置列对齐
pd.set_option("display.unicode.ambiguous_as_wide",True)
pd.set_option("display.unicode.east_asian_width",True) data = pd.read_excel(r'C:\Users\lenovo\Desktop\总结\Python\超市营业额.xlsx',usecols = ['日期','交易额']) dff = copy.deepcopy(data)
# 查看周几
dff['日期'] = pd.to_datetime(data['日期']).dt.weekday_name
'''
日期 交易额
0 Thursday 2000
1 Thursday 1800
2 Thursday 800
'''
# 按照周几进行分组,查看交易的平均值
dff = dff.groupby('日期').mean().apply(round)
dff.index.name = '周几'
dff[:3]
'''
交易额
周几
Thursday 1024.0
'''
# dff = copy.deepcopy(data)
# 使用正则规则查看月份日期
# dff['日期'] = dff.日期.str.extract(r'(\d{4}-\d{2})')
# dff[:5] # 按照日 进行分组查看交易的平均值 -1 表示倒数第一个
# data.groupby(data.日期.str.__getitem__(-1)).mean().apply(round) # 查看日期尾数为 1 的数据
# data[data.日期.str.endswith('1')][:12] # 查看日期尾数为 12 的交易数据,slice 为切片 (-2) 表示倒数两个
# data[data.日期.str.slice(-2) == '12'] # 查看日期中月份或天数包含 2 的交易数据
# data[data.日期.str.slice(-5).str.contains('2')][1:9] import pandas as pd
import numpy as np # 读取全部数据,使用默认索引
data = pd.read_excel(r'C:\Users\lenovo\Desktop\总结\Python\超市营业额.xlsx') # 修改异常值
data.loc[data.交易额 > 3000,'交易额'] = 3000
data.loc[data.交易额 < 200,'交易额'] = 200 # 删除重复值
data.drop_duplicates(inplace = True) # 填充缺失值
data['交易额'].fillna(data['交易额'].mean(),inplace = True) # 使用交叉表得到每人在各柜台交易额的平均值
data_group = pd.crosstab(data.姓名,data.柜台,data.交易额,aggfunc = 'mean').apply(round)
# 绘制柱状图
data_group.plot(kind = 'bar')
# <matplotlib.axes._subplots.AxesSubplot object at 0x000001D681607888> # 数据的合并
data1 = pd.read_excel(r'C:\Users\lenovo\Desktop\总结\Python\超市营业额.xlsx')
data2 = pd.read_excel(r'C:\Users\lenovo\Desktop\总结\Python\超市营业额.xlsx',sheet_name = 'Sheet2')
df1 = data1[:3]
'''
工号 姓名 日期 时段 交易额 柜台
0 1001 张三 20190301 9:00-14:00 2000 化妆品
1 1002 李四 20190301 14:00-21:00 1800 化妆品
2 1003 王五 20190301 9:00-14:00 800 食品
'''
df2 = data2[:4]
'''
工号 姓名 日期 时段 交易额 柜台
0 1006 钱八 20190301 9:00-14:00 850 蔬菜水果
1 1001 张三 20190302 14:00-21:00 600 蔬菜水果
2 1001 张三 20190302 9:00-14:00 1300 化妆品
3 1002 李四 20190302 14:00-21:00 1500 化妆品
'''
# 使用 concat 连接两个相同结构的 DataFrame 对象
df3 = pd.concat([df1,df2])
'''
工号 姓名 日期 时段 交易额 柜台
0 1001 张三 20190301 9:00-14:00 2000 化妆品
1 1002 李四 20190301 14:00-21:00 1800 化妆品
2 1003 王五 20190301 9:00-14:00 800 食品
0 1006 钱八 20190301 9:00-14:00 850 蔬菜水果
1 1001 张三 20190302 14:00-21:00 600 蔬菜水果
2 1001 张三 20190302 9:00-14:00 1300 化妆品
3 1002 李四 20190302 14:00-21:00 1500 化妆品
'''
# 合并,忽略原来的索引 ignore_index
df4 = df3.append([df1,df2],ignore_index = True)
'''
工号 姓名 日期 时段 交易额 柜台
0 1001 张三 20190301 9:00-14:00 2000 化妆品
1 1002 李四 20190301 14:00-21:00 1800 化妆品
2 1003 王五 20190301 9:00-14:00 800 食品
3 1006 钱八 20190301 9:00-14:00 850 蔬菜水果
4 1001 张三 20190302 14:00-21:00 600 蔬菜水果
5 1001 张三 20190302 9:00-14:00 1300 化妆品
6 1002 李四 20190302 14:00-21:00 1500 化妆品
7 1001 张三 20190301 9:00-14:00 2000 化妆品
8 1002 李四 20190301 14:00-21:00 1800 化妆品
9 1003 王五 20190301 9:00-14:00 800 食品
10 1006 钱八 20190301 9:00-14:00 850 蔬菜水果
11 1001 张三 20190302 14:00-21:00 600 蔬菜水果
12 1001 张三 20190302 9:00-14:00 1300 化妆品
13 1002 李四 20190302 14:00-21:00 1500 化妆品
'''
# 按照列进行拆分
df5 = df4.loc[:,['姓名','柜台','交易额']]
# 查看前五条数据
df5[:5]
'''
姓名 柜台 交易额
0 张三 化妆品 2000
1 李四 化妆品 1800
2 王五 食品 800
3 钱八 蔬菜水果 850
4 张三 蔬菜水果 600
''' # 合并 merge 、 join
# 按照工号进行合并,随机查看 3 条数据
rows = np.random.randint(0,len(df5),3)
pd.merge(df4,df5).iloc[rows,:]
'''
工号 姓名 日期 时段 交易额 柜台
7 1002 李四 20190301 14:00-21:00 1800 化妆品
4 1002 李四 20190301 14:00-21:00 1800 化妆品
10 1003 王五 20190301 9:00-14:00 800 食品
'''
# 按照工号进行合并,指定其他同名列的后缀
pd.merge(df1,df2,on = '工号',suffixes = ['_x','_y']).iloc[:,:]
'''
工号 姓名_x 日期_x 时段_x ... 日期_y 时段_y 交易额_y 柜台_y
0 1001 张三 20190301 9:00-14:00 ... 20190302 14:00-21:00 600 蔬菜水果
1 1001 张三 20190301 9:00-14:00 ... 20190302 9:00-14:00 1300 化妆品
2 1002 李四 20190301 14:00-21:00 ... 20190302 14:00-21:00 1500 化妆品
'''
# 两个表都设置工号为索引 set_index
df2.set_index('工号').join(df3.set_index('工号'),lsuffix = '_x',rsuffix = '_y').iloc[:]
'''
姓名_x 日期_x 时段_x 交易额_x ... 日期_y 时段_y 交易额_y 柜台_y
工号 ...
1001 张三 20190302 14:00-21:00 600 ... 20190301 9:00-14:00 2000 化妆品
1001 张三 20190302 14:00-21:00 600 ... 20190302 14:00-21:00 600 蔬菜水果
1001 张三 20190302 14:00-21:00 600 ... 20190302 9:00-14:00 1300 化妆品
1001 张三 20190302 9:00-14:00 1300 ... 20190301 9:00-14:00 2000 化妆品
1001 张三 20190302 9:00-14:00 1300 ... 20190302 14:00-21:00 600 蔬菜水果
1001 张三 20190302 9:00-14:00 1300 ... 20190302 9:00-14:00 1300 化妆品
1002 李四 20190302 14:00-21:00 1500 ... 20190301 14:00-21:00 1800 化妆品
1002 李四 20190302 14:00-21:00 1500 ... 20190302 14:00-21:00 1500 化妆品
1006 钱八 20190301 9:00-14:00 850 ... 20190301 9:00-14:00 850 蔬菜水果 '''

函数实现 多个数据求平均值
def average(*args):
print(args)
# (1, 2, 3)
# (1, 2, 3)
print(len(args))
# 3
# 3
print(sum(args, 0.0) / len(args)) average(*[1, 2, 3])
# 2.0
average(1, 2, 3)
# 2.0

使用 * 对传入的列表进行解包

对传入的数据进行分类
def bifurcate(lst, filter):
print(lst)
# ['beep', 'boop', 'foo', 'bar'] print(filter)
# [True, True, False, True]
# 列表名,不是 filter 函数 print(enumerate(lst))
# <enumerate object at 0x0000017EB10B9D00> print(list(enumerate(lst)))
# [(0, 'beep'), (1, 'boop'), (2, 'foo'), (3, 'bar')] print([
[x for i, x in enumerate(lst) if filter[i] == True],
[x for i, x in enumerate(lst) if filter[i] == False]
])
'''
filter[i] 主要是对枚举类型前面的索引和传入的 filter 列表进行判断是否重复
'''
bifurcate(['beep', 'boop', 'foo', 'bar'], [True, True, False, True])

进阶 对传入的数据进行分类
def bifurcate_by(lst, fn):
print(lst)
# ['beep', 'boop', 'foo', 'bar'] print(fn('baby'))
# True print(fn('abc'))
# False print([
[x for x in lst if fn(x)],
[x for x in lst if not fn(x)]
]) bifurcate_by(
['beep', 'boop', 'foo', 'bar'], lambda x: x[0] == 'b'
)
# [['beep', 'boop', 'bar'], ['foo']]

二进制字符长度
def byte_size(s):
print(s)
#

python基础全部知识点整理,超级全(20万字+)的相关教程结束。

《python基础全部知识点整理,超级全(20万字+).doc》

下载本文的Word格式文档,以方便收藏与打印。