
python
文章平均质量分 65
D_ry
Don't repeat yourself.
展开
-
几个有用的python装饰器,先收藏再学习
通用装饰器模板import functoolsdef decorator(func): @functools.wraps(func) def wrapper_decorator(*args, **kwargs): # Do something before value = func(*args, **kwargs) # Do something after return value return wrapper_d原创 2021-07-02 11:51:08 · 1894 阅读 · 43 评论 -
学了这么久连python内置函数有哪些都不知道?
内置函数大全集(python 3.9)快速跳转内置函数大全集(python 3.9)`abs(x)``all(iterable)``bin(x)``class bool([x])``class bytearray([source[, encoding[, errors]]])``class bytes([source[, encoding[, errors]]])``callable(object)``chr(i)``@classmethod``compile(source, filename, mode原创 2021-07-01 20:40:24 · 3410 阅读 · 90 评论 -
用python命令行来做一个计算器:Argparse教程
前言通常,使用python命令行的情况是运行某个文件,比如python hello.py,之后会在终端上看到hello.py文件的输出结果。但如果每次运行py文件时,需要对其中的某些变量赋值,比如我想运行一个py文件,做一个简单的加法运算:python sum.py 1 2,希望得到结果为3。sys.argv应该如何来编写可以接受从命令行进行赋值的加法脚本?一种可行的代码如下:import sysargs = sys.argvprint(sum(map(int,args[1:])))s原创 2021-06-29 21:16:44 · 2689 阅读 · 70 评论 -
你真的懂怎么用Python排序数据吗?
Python 列表有一个内置的 list.sort() 方法可以直接修改列表。还有一个 sorted() 内置函数,它会从一个可迭代对象构建一个新的排序列表。来看看这两个函数到底怎么用吧!基本用法对一个列表进行简单升序排序,使用sorted():>>> sorted([5, 2, 3, 1, 4])[1, 2, 3, 4, 5]另一种方法是使用 list.sort() 方法,它会直接修改原列表(并返回 None 以避免混淆),通常来说它不如 sorted() 方便 ——— 但原创 2021-06-28 19:36:28 · 1958 阅读 · 38 评论 -
python正则表达式re快速入门
简介正则表达式(称为RE,或正则,或正则表达式模式)本质上是嵌入在Python中的一种微小的、高度专业化的编程语言,可通过 re 模块获得。 使用正则,可以为要匹配的可能字符串集指定规则,然后在任何字符串进行匹配。还可以使用正则修改字符串,或以各种方式将字符串拆分。正则表达式模式被编译成一系列字节码,然后由用 C 编写的匹配引擎执行。正则表达式语言相对较小且受限制,因此并非所有可能的字符串处理任务都可以使用正则表达式完成。简单模式先来看看最简单的正则表达式,正则表达式最常用的任务就是匹配字符。匹配原创 2021-06-28 11:56:45 · 2743 阅读 · 42 评论 -
发生异常: RuntimeError (note: full exception trace is shown but execution is paused at: <module>)
使用multiprocessing模块时报错:from multiprocessing import Processdef hello(): print('hello')p = Process(target=hello)p.start()将代码更改为:from multiprocessing import Process, freeze_support, set_start_methoddef hello(): print('hello')if __name__ =原创 2021-06-27 22:13:03 · 17560 阅读 · 0 评论 -
为什么python中浮点计算结果不正确?
什么是浮点数?浮点数在计算机硬件中表示为以2为基数(二进制)的小数。举例而言,十进制的小数0.125等于 1/10 + 2/100 + 5/1000 ,同理,二进制的小数0.001等于0/2 + 0/4 + 1/8。这两个小数具有相同的值,唯一真正的区别是第一个是以10为基数的小数表示法,第二个则是2为基数。十进制小数与二进制小数的转换大多数的十进制小数都不能精确地表示为二进制小数。所以在大多数情况下,十进制浮点数都只能近似地以二进制浮点数形式储存在计算机中。对于一个分数1/3 。它在十进制下的一原创 2021-06-25 21:39:05 · 5405 阅读 · 31 评论 -
一行python代码打印前n个Fibonacci数
打印前10个Fibonacci数:print(list(map(lambda x,f=lambda x,f:(f(x-1,f)+f(x-2,f)) if x>1 else 1: f(x,f), range(10))))结果:[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]代码解析:将这一行代码拆开来看。map()方法的接受两个参数,第一个参数是一个方法,第二个参数是一个可迭代对象,功能是将第一个方法应用在第二个参数中的每一项上,得到一个map对象。比如:map(st原创 2021-06-22 21:31:17 · 2051 阅读 · 9 评论 -
python 算法与数据结构 二叉树
二叉树类class BiTNode: def __init__(self): self.data = None self.lchild = None self.rchild = None将有序数组存为二叉树def array2tree(arr,start,end): # 有序列表转二叉树 root = None if ...原创 2020-04-17 23:44:02 · 526 阅读 · 0 评论 -
PAT 1012 The Best Rank python解法
1012 The Best Rank (25分)To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programming Language, M - Mathematics (Calculus or Lin...原创 2020-04-06 20:58:16 · 597 阅读 · 0 评论 -
PAT 1009 Product of Polynomials python解法
This time, you are supposed to find A×B where A and B are two polynomials.Input Specification:Each input file contains one test case. Each case occupies 2 lines, and each line contains the informati...原创 2020-04-05 19:30:58 · 597 阅读 · 0 评论 -
数据分析神库pandas_profiling
以下代码在jupyter notebook中测试通过import pandas_profilingimport seaborn as snsimport pandas as pdimport pandas_profiling as ppimport matplotlib.pyplot as plt# 加载泰坦尼克数据集data = sns.load_dataset('titanic'...原创 2020-04-04 21:32:14 · 622 阅读 · 0 评论 -
PAT 1081 Rational Sum python解法
1081 Rational Sum (20 分)Given N rational numbers in the form numerator/denominator, you are supposed to calculate their sum.Input Specification:Each input file contains one test case. Each case sta...原创 2019-01-20 17:18:24 · 270 阅读 · 0 评论 -
PAT 1112 Stucked Keyboard python解法
1112 Stucked Keyboard (20 分)On a broken keyboard, some of the keys are always stucked. So when you type some sentences, the characters corresponding to those keys will appear repeatedly on screen for...原创 2019-01-28 17:11:58 · 283 阅读 · 0 评论 -
PAT 1136 A Delayed Palindrome python解法
1136 A Delayed Palindrome (20 分)Consider a positive integer N written in standard notation with k+1 digits ai as aki ⋯a1 a0 with 0 ≤ ai < 10 for all i and ak > 0. Then N is pa...原创 2019-01-16 11:50:54 · 244 阅读 · 0 评论 -
PAT 1019 General Palindromic Number python解法
1019 General Palindromic Number (20 分)A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All singl...原创 2019-01-16 11:15:31 · 256 阅读 · 0 评论 -
PAT 1152 Google Recruitment python解法
1152 Google Recruitment (20 分)In July 2004, Google posted on a giant billboard along Highway 101 in Silicon Valley (shown in the picture below) for recruitment. The content is super-simple, a URL con...原创 2019-01-21 16:11:21 · 273 阅读 · 2 评论 -
python中4位16进制数与float浮点数互相转换
import structs = 'F4CEF042'#&amp;lt;是小端,&amp;gt;是大端,f代表16进制print(struct.unpack('&amp;lt;f', bytes.fromhex(s))[0])#输出:120.40420532226562原创 2019-01-10 11:04:46 · 17043 阅读 · 3 评论 -
PAT 1132 Cut Integer python解法
1132 Cut Integer (20 分)Cutting an integer means to cut a K digits lone integer Z into two integers of (K/2) digits long integers A and B. For example, after cutting Z = 167334, we have A = 167 and B ...原创 2019-01-21 10:44:00 · 320 阅读 · 2 评论 -
python刷PAT超时怎么办
1.优化代码,降低复杂度2.有一些python内置函数比较耗时,尝试换一种方式, 比如求a集合是不是b集合的父集可以转换为求b集合是不是a集合的子集,(将a.issuperset(b)转换为b.issubset(a))3.删掉中文...原创 2019-01-09 22:23:22 · 870 阅读 · 0 评论 -
PAT 1149 Dangerous Goods Packaging python解法
1149 Dangerous Goods Packaging (25 分)When shipping goods with containers, we have to be careful not to pack some incompatible goods into the same container, or we might get ourselves in serious troub...原创 2019-01-09 22:17:05 · 295 阅读 · 0 评论 -
PAT 1104 Sum of Number Segments python解法
1104 Sum of Number Segments (20 分)Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For example, given the sequence { 0.1, 0.2, 0.3, 0.4 }, we have 10 segmen...原创 2019-01-15 13:51:48 · 278 阅读 · 0 评论 -
PAT 1096 Consecutive Factors python解法
1096 Consecutive Factors (20 分)Among all the factors of a positive integer N, there may exist several consecutive numbers. For example, 630 can be factored as 3×5×6×7, where 5, 6, and 7 are the three...原创 2019-01-23 16:06:53 · 496 阅读 · 4 评论 -
PAT 1144 The Missing Number python解法
1144 The Missing Number (20 分)Given N integers, you are supposed to find the smallest positive integer that is NOT in the given list.Input Specification:Each input file contains one test case. For ...原创 2019-01-29 15:31:17 · 228 阅读 · 0 评论 -
PAT 1140 Look-and-say Sequence python解法
1140 Look-and-say Sequence (20 分)Look-and-say sequence is a sequence of integers as the following:D, D1, D111, D113, D11231, D112213111, …where D is in [0, 9] except 1. The (n+1)st number is a kind...原创 2019-01-06 21:49:57 · 304 阅读 · 0 评论 -
使用2句python代码获取微博热搜榜表格版
1.首先导入requests和pandas两个库。2.定义ua,找到微博热搜榜的网址https://s.weibo.com/top/summary?cate=realtimehot3.将获取到的html通过read_html方法得到其中的表格数据,read_html方法返回一个表格类型的列表,因为只有一个表格,我们取第一个就好。import requestsimport pandas as...原创 2019-04-06 23:13:21 · 482 阅读 · 0 评论 -
爬虫实战:爬取豆瓣TOP250电影信息
直接上代码,主要2个函数,一个是获取每个电影的详情页URL的函数,一个是处理电影详情页数据的函数。import requestsfrom bs4 import BeautifulSoupimport timestart_url = 'https://movie.douban.com/top250'movie_url = []#连接太多会被拒绝,限制在5个requests.adap...原创 2019-03-13 22:56:32 · 1066 阅读 · 0 评论 -
PAT 1024 Palindromic Number python解法
1024 Palindromic Number (25 分)A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit ...原创 2019-03-02 17:30:32 · 240 阅读 · 0 评论 -
python:打印一个动态进度条
以下代码完成了一个动态进度条的打印。import timefor i in range(11): time.sleep(0.5) print('\r当前进度:{0}{1}%'.format('▉'*i,(i*10)), end='')print('加载完成!')\r是将光标移到一行的开始,所以\r之后的内容会覆盖掉上次打印的内容,形成动态打印。效果图:...原创 2019-02-20 16:14:53 · 7689 阅读 · 1 评论 -
PAT 1071 Speech Patterns python解法
1071 Speech Patterns (25 分)People often have a preference among synonyms of the same word. For example, some may prefer “the police”, while others may prefer “the cops”. Analyzing such patterns can h...原创 2019-02-19 17:22:40 · 362 阅读 · 0 评论 -
PAT 1077 Kuchiguse python解法
1077 Kuchiguse (20 分)The Japanese language is notorious for its sentence ending particles. Personal preference of such particles can be considered as a reflection of the speaker’s personality. Such a...原创 2019-01-30 15:46:43 · 274 阅读 · 0 评论 -
PAT 1061 Dating python解法
1061 Dating (20 分)Sherlock Holmes received a note with some strange strings: Let’s date! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm. It took him only a minute to figure out that th...原创 2019-01-30 14:34:17 · 331 阅读 · 0 评论 -
爬虫实战:使用requests库爬取12306余票信息
最近看了一些爬虫的资料,试着自己写了一个小爬虫,爬取12306的余票信息。代码很少,也没做什么优化,仅记录一下第一个爬虫。思路分析:查询余票的正常步骤肯定是打开12306,输入出发地,目的地,出发时间,点击查询。根据这个步骤,一步一步开始:1.首先来到https://kyfw.12306.cn/otn/leftTicket,输入出发地等信息,点击查询,通过浏览器F12抓包分析可以发现,车站...原创 2019-02-21 15:07:48 · 949 阅读 · 1 评论 -
PAT 1108 Finding Average python解法
1108 Finding Average (20 分)The basic task is simple: given N real numbers, you are supposed to calculate their average. But what makes it complicated is that some of the input numbers might not be le...原创 2019-01-29 17:36:07 · 223 阅读 · 0 评论 -
PAT 1046 Shortest Distance python解法
1046 Shortest Distance (20 分)The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed to tell the shortest distance between any pair of exits.Input Specific...原创 2019-01-20 00:06:59 · 358 阅读 · 0 评论 -
PAT 1050 String Subtraction python解法
1050 String Subtraction (20 分)Given two strings S1 and S2 , S=S1−S2 is defined to be the remaining string after taking all the characters in S2 from S1 . Your task is si...原创 2019-01-09 09:59:15 · 311 阅读 · 0 评论 -
PAT (Advanced Level) Practice python解法合集
1001 A+B Format1002 A+B for Polynomials1003 Emergency1004 Counting Leaves1005 Spell It Right1006 Sign In and Sign Out1007 Maximum Subsequence Sum1008 Elevator1009 Product of Polynomials1010 R...原创 2019-01-07 13:46:36 · 1029 阅读 · 0 评论 -
PAT 1120 Friend Numbers python解法
1120 Friend Numbers (20 分)Two integers are called “friend numbers” if they share the same sum of their digits, and the sum is their “friend ID”. For example, 123 and 51 are friend numbers since 1+2+3...原创 2019-01-07 11:50:43 · 217 阅读 · 0 评论 -
PAT 1092 To Buy or Not to Buy python解法
1092 To Buy or Not to Buy (20 分)Eva would like to make a string of beads with her favorite colors so she went to a small shop to buy some beads. There were many colorful strings of beads. However the...原创 2019-01-07 11:36:16 · 177 阅读 · 0 评论 -
PAT 1027 Colors in Mars python解法
1027 Colors in Mars (20 分)People in Mars represent the colors in their computers in a similar way as the Earth people. That is, a color is represented by a 6-digit number, where the first 2 digits ar...原创 2019-01-07 11:21:36 · 220 阅读 · 0 评论