python
Monkey_24
沸腾吧,皮皮猴!
展开
-
实操:Python爬虫读取豆瓣TOP250生成数据表存储后借助Flask可视化展示
from bs4 import BeautifulSoupfrom collections import OrderedDictimport urllib.requestimport urllib.errorimport reimport xlwtimport sqlite3class doubanCatch: def __init__(self,baseurl,head,savepath,complileDict,dbpath): self.baseurl = .原创 2020-09-23 22:48:33 · 2614 阅读 · 1 评论 -
[实操]Python财务数据分析可视化
from matplotlib import pyplot as pltplt.rcParams['font.family'] = 'Noto Sans CJK JP'plt.rcParams['axes.unicode_minus'] = Falseplt.figure()plt.subplot(1,1,1)x=[0,1,2,3,4]y=[3,7,5,9,4]plt.ylabel('Y_LABEL')plt.xlabel('X_LABEL')plt.title('Matlib_案例').原创 2020-09-21 08:32:01 · 7395 阅读 · 7 评论 -
图解:pycharm+github实现python代码的版本控制(ubuntu环境)
温馨提示:如果查看不到图片请访问本站https://blog.csdn.net/u012335228/article/details/95388993创建git仓库(请先注册github账号)填写仓库信息创建成功在linux安装git工具(本例使用deepin-linux深度系统)sudo apt install git-all(快速安装)或者sudo apt in...原创 2019-03-01 13:51:12 · 917 阅读 · 0 评论 -
实用:在Python3中使用正则表达式对httpd日志进行分析
日志分析介绍如何进行httpd日志分析本文项目仅做学术交流,转载请注明出处!import datetimeimport relogline='''192.168.56.1 - - [18/Mar/2019:10:55:04 +0800] \"POST /zabbix/jsrpc.php?output=json-rpc HTTP/1.1" 200 64 \"http://192.168...原创 2019-03-25 00:48:02 · 420 阅读 · 0 评论 -
图解:Deepin手动安装PyCharm(解决了没有启动图标的问题)
1、 官网下载最新版本https://www.jetbrains.com/pycharm/2、找到下载目录3、进行安装:打开终端,输入tar -xvzf pycharm-community-2017.3…tar.gz -C ~注意:-C 后面的~ 为文件解压路径,这里解压到home下4、进入pycharm目录5、运行pycharm:sh pycharm.sh &6、...转载 2019-05-01 12:51:14 · 3956 阅读 · 0 评论 -
实用:Python3实现打印加法口诀表
print('加法口诀表')for i in range(1,10): for j in range(1,i+1): #print("%d + %d = %2d " %(i , j ,i + j) ,end = ' ') print('{} + {} = {:<2d} '.format(i , j , i + j) ,end = ' ') ...转载 2019-05-18 22:01:14 · 3160 阅读 · 0 评论 -
排序:python依次接收用户输入3个数,排序后打印(八种方案)
方案一:num = []for i in range(1,4): num.append(int(input('>>>')))print(num)if num[0] > num[1]: tmp = num[0] num[0] = num[1] num[1] = tmp if num[1] > num[2]: ...原创 2019-05-19 21:54:26 · 2436 阅读 · 0 评论 -
实用:python中求杨辉三角的n行r列的数(利用公式求解)
#杨辉三角定理:n行r列的数字可表示为C(n,r),有公式C(n,r) = n!/(r!(n-r)!)n,r = 8,5 #求n行r列所在的数,编号从0开始,0代表第一行或第一列targets = [] #依次放入!r,!n-r,!nfactorial = 1for i in range(1,n+1): factorial *= i if i == r: ...原创 2019-05-24 12:55:50 · 1647 阅读 · 0 评论 -
算法优化:python中打印任意矩阵
n = 10lines = 2width = n//lineslst1 = []lst2 = list(range(1,n+1))for i in range(0,n,width): lst1.append(lst2[i:i+width])else: for i in range(lines): print(lst1[i])print('-----...原创 2019-05-24 19:39:11 · 1297 阅读 · 0 评论 -
算法优化:python随机产生10个数字,统计重复以及不重复的次数并分类打印出来(注意书写规范)
import randomtotal = 20rand = 10lst = [random.randint(1,total) for _ in range(rand)]print('source:',lst)record = [0 for _ in range(total+1)]single = []multi = []for i in lst: record[i] ...原创 2019-05-25 00:39:35 · 4678 阅读 · 0 评论 -
算法优化:python用户输入N个正整数,打印整数里的每一位数及其重复次数,从低位到高位依次打印
lst = []n = 2for i in range(n): lst.append(input('>>>').strip().lstrip('0'))total = len(lst)for i in range(total): num = lst[i] lst2 = [0] * 10 print(num) lenth = len...原创 2019-05-21 18:57:23 · 3811 阅读 · 0 评论 -
实用:python随机产生2组各10个数字的列表,统计重复和不重复数字(集合练习)
随机产生2组各10个数字的列表,如下要求:每个数字取值范围:[10,20]统计20个数字中,一共有多少个不同的数字?对比2组中,不重复的数字有几个,分别是什么?(不考虑本组中的重复数字)对比2组中,重复的数字有几个,分别是什么?(不考虑本组中的重复数字)import randomlst = []for i in range(2): lst.append([random.ra...原创 2019-05-25 17:56:57 · 5842 阅读 · 0 评论 -
实用:python字符编码之间的转化(来猜猜博主的真实姓名)
bytes(range(255))运行结果:b’\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&’()*+,-./0123456789:;<=>?@ABCDEFGH...原创 2019-05-22 00:00:17 · 4023 阅读 · 0 评论 -
算法优化:Python3求5000000以内的素数(二代i3处理器60秒算出)
import mathlst=[]for i in range(3,100,2): j = math.sqrt(i) j = math.ceil(j) for x in lst: if i % x == 0: break else: lst.append(i) else: lst =...原创 2019-05-17 00:03:59 · 1006 阅读 · 0 评论 -
算法优化:python中选择排序,随机产生1-9的数字组成的9个元素的列表按降序排列
按降序排列:m_lst = [ [1,2,3,4,5,6,7,8,9], [2,3,4,5,7,8,9,6,1], [9,8,7,6,5,4,3,2,1]]nums = m_lst[0]count_swap = 0count_inter = 0for i in range(len(nums)): maxindex = i for j in ...原创 2019-05-26 00:17:27 · 2047 阅读 · 0 评论 -
实用:collections中namedtuple,defaultdict,OrderedDict的使用
一. namedtuplefrom collections import namedtupleStudent=namedtuple('student','name age')Tom=Student('tom',20)Jerry=Student('jerry',18)print(1,Tom.name)print(2,Jerry.name)print(3,type(Jerry))运...原创 2019-05-26 12:51:15 · 277 阅读 · 0 评论 -
实用:python字典中默认值的取法
from collections import defaultdictnum = input('>>>')d1 = dict.fromkeys(num,0)print(d1)d2 = defaultdict(int)for k in num: print('k:{} v:{}'.format(k,d2[k]))d3 = {'a':1,'b':2}print(...原创 2019-05-26 14:40:12 · 2154 阅读 · 0 评论 -
实用:python用户输入一个数字,打印每一位数字及其重复的次数(字典练习)
from collections import defaultdict,OrderedDictnum = input('>>>')od = OrderedDict()for k in num: od.setdefault(k,0) od[k] += 1else: for k,v in od.items(): print('nums...原创 2019-05-26 14:51:55 · 7972 阅读 · 1 评论 -
实用:python数字重复的统计,随机产生100个整数,升序输出所有不同数字及其重复的次数(字典练习)
范围[-1000,1000]from collections import OrderedDictimport randomlst = sorted([(random.randint(-1000,1000))for i in range(100)])print(lst,end='\n\n')od = OrderedDict()for k in lst: od.setdefa...原创 2019-05-26 16:30:40 · 14490 阅读 · 0 评论 -
实用:python中字符串重复统计
要求:1.字符表为a~z中的26个字母2.随机挑选2个字母组成字符串,共挑选100个3.降序输出这100个字符串及重复的次数from collections import OrderedDictimport randomlst1 = [chr(i) for i in range(ord(‘a’),ord(‘z’)+1)]length = len(lst1)n = 2lst2 = ...原创 2019-05-26 16:58:19 · 8170 阅读 · 0 评论 -
实用:python中日期时间格式化的打印
import datetimedt = datetime.datetime.now()print(1,dt)print(2,dt.date(),dt.time())print(3,dt.year,dt.month,dt.day,dt.isoweekday())print('-----------------------------')ts = dt.timestamp()prin...原创 2019-05-26 18:11:24 · 561 阅读 · 0 评论 -
算法优化:python转置方阵的打印
转置方阵:lst = [1,2,3,4,5,6,7,8,9]width = 3for i in range(0,len(lst),width): print(lst[i:i+3])print('----------------')width = 3lines = lines = len(lst)//widthfor i in range(lines): pri...原创 2019-05-22 16:15:57 · 283 阅读 · 0 评论 -
实用:python打印随机的ID码(产品序列号),如0001,abadicddws的格式
"0001,abadicddws"是ID格式,要求ID格式是以点号分割,左边是4位从1开始的整数,右边是10位随机小写英文字母,情一次生成前100个ID的列表import random['{:04}.{}'.format(i,''.join([chr(random.randint(ord('a'),ord('z'))) for _ in range(10)])) for i in range(...原创 2019-05-27 02:43:48 · 589 阅读 · 0 评论 -
实用:python通过封装解构利用丢弃变量取列表元素
给定一个列表lst = list(range(10)),取列表,第二个,第四个,倒数第二个元素lst = list(range(10))print(lst)_,a,_,b,*_,c,_ = lstprint(a,b,c)运行结果:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]1 3 8...原创 2019-05-23 00:05:10 · 161 阅读 · 0 评论 -
算法优化:Pthon3实现杨辉三角前15行的计算
lst1 = []for i in range(1,16+1): raw = [1] * i for x in range(0,i-2): mid = i // 2 if x < mid: rel = pre[x] + pre[x+1] raw[x+1] = rel ra...原创 2019-05-18 14:34:25 · 1045 阅读 · 0 评论 -
实用:python编写一个函数,能够接收至少2个参数,返回最小值和最大值
def fn(a,b,*args): maxnum = max(a,b,*args) minnum = min(a,b,*args) return maxnum,minnum运行结果:#fn(1,2,3,4)(4, 1)原创 2019-05-27 21:07:09 · 10203 阅读 · 1 评论 -
实用:python中字典的扁平化(flat)
把一个字典扁平化,源字典为{‘a’:{‘b’:1,‘c’:2},‘d’:{‘e’:3,‘f’:{‘g’:4}}}dct1 = {'a':{'b':1,'c':2},'d':{'e':3,'f':{'g':4}}}# dct1 = {'a':{'b':1},'c':1}def fn(key='',dct={},**src): #src = {'a':{'b':1,'c':2},'d':{...原创 2019-05-30 16:33:39 · 7798 阅读 · 0 评论 -
实用:Python3计算斐波那契数列前100项分别是多少?
old1=1old2=1n = 100for i in range(n-1): old1,old2=old2,old1+old2print("%d "%old1,end='')原创 2019-05-18 20:39:32 · 14104 阅读 · 0 评论 -
实用:Python3解决猴子偷桃子的问题(1-10天里面分别有多少颗桃子可以吃)
猴子第一天偷摘天宫若干个桃子,当即吃了一半,还不过瘾,又多吃了一个,第二天早上又将剩下的桃子吃掉一半,又不过瘾多吃了一个,以后每天早上都吃了前一天剩下的一半零一个.到了第10天早上想吃的时候,才发现只剩下一个桃子了,求第一天共摘了多少个桃子?days = 10last = 1loops = days -1for day in range(loops,0,-1): last = 2*...原创 2019-05-18 21:05:26 · 4434 阅读 · 0 评论 -
实用:Python3实现打印任意宽度的菱形
width = 11 #注意边长必须位奇数for i in range(-width//2,width//2+1): prespace=i if i>0 else -i print(' '*prespace+'*'*(width-prespace*2))原创 2019-05-18 21:31:37 · 997 阅读 · 1 评论 -
实用:Python3实现打印九九乘法表(格式对齐版)
for i in range(1,10):for j in range(1,10):if i <= j:if j != 9:if j <= 3:print(’{0}{1}={2:<1} '.format(i,j,ij),end=’’)else:print(’{0}{1}={2:<2} '.format(i,j,ij),end=’’)else:print(’...原创 2019-05-18 21:37:42 · 7289 阅读 · 2 评论 -
实用:python编写一个函数,能够接受一个参数n,n为正整数,上三角和下三角打印.要求数字必须对齐
def fn(n): n = 111 lst = [ i for i in range(n,0,-1)] print(lst) c = len(str(n))-1 w1 = c+3 w2 = c+2 for i in range(n+1,1,-1): space = [' ' for i in range(i-2)] ...原创 2019-05-27 23:10:48 · 5094 阅读 · 0 评论 -
实用:python直接插入排序(稳定性排序算法)
m_list = [[1,2,3,4,5,6,7,8,9], [9,8,7,6,5,4,3,2,1], [3,5,4,1,2,6,7,8,9], [1,1,1,1,1,1,1,1,1]]lst = [0] + m_list[2]print(lst)n = len(lst)for i in range(2,n): lst[0] = lst[...原创 2019-05-28 02:37:10 · 600 阅读 · 0 评论 -
二叉树:二叉树和完全二叉树的性质
二叉树性质1:(高度/最大深度)含有n个结点的二叉数,其最大高度为n,最小高度为math.ceil(log2(n+1))性质2:(度数为2和叶子结点关系)度数为2的结点=叶子结点数-1性质3:(单层最多结点)二叉树的第i层最多有2^(i-1)个结点(i>=1)性质4:(所有层最多结点)深度为k的二叉树,最多有2^k-1个结点完全二叉树性质1:(深度)具有n个节点的完全二...原创 2019-05-28 22:25:59 · 1183 阅读 · 0 评论 -
函数:理解变量名解析原则LEGB
LEGB分别如下:.local 本地作用域.Enclosing 闭包.Global 全局作用域 .Build-in 内置函数变量所以一个名词的查找顺序就是LEGB原创 2019-05-28 22:45:55 · 292 阅读 · 0 评论 -
函数:python中函数执行的流程
def foo1(b,b1=3): print("foo1 called",b,b1)def foo2(c): foo3(c) print("foo2 called",c)def foo3(d): print("foo3 called",d)def main(): print("main called") foo1(100,101) ...原创 2019-05-28 23:42:04 · 2145 阅读 · 0 评论 -
实用:python的堆排序
import randomimport mathimport copylst = [[1,2,3,4,5,6,7,8,9],[0, 7, 3, 5, 2, 1, 4, 8, 9, 6],[6,2,7,4,5,3,1]]# random.shuffle(lst)def print_tree(array:list): ''' i prespace offset ...原创 2019-06-06 01:31:26 · 140 阅读 · 0 评论 -
实用:python中函数的注解/签名(稽核函数inspect使用)
def add(x:int=0,y:int=0) ->int: ''' this is a function for add x: int y: int return: int ''' return x + yprint(add(4,5))print(add('a','b'))print(add())运行结果:9ab0...原创 2019-06-06 10:30:17 · 555 阅读 · 0 评论 -
递归:python中求n的阶乘
def fn(num,result=1): result *= num# if num == 1:# return result# else:# return fn(num-1,result) return result if num == 1 else fn(num-1,result)运行结果:fn(4)24案例...原创 2019-05-29 12:40:47 · 6995 阅读 · 0 评论 -
递归:python中将一个数逆序放入列表中,例如1234 ==> [4,3,2,1]
n=input('>>>')src = list(n)lst = []def fn(num): if len(num) == 0: return lst lst.append(num.pop()) return fn(num)fn(src)运行结果:>>>1547['7', '4', '5', '1']...原创 2019-05-29 13:06:15 · 3226 阅读 · 0 评论