![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
python
BobChill
这个作者很懒,什么都没留下…
展开
-
mergesort与逆序对个数
mergesort代码如下:def merge_lists(left, right): res = [] lc = rc = 0 while lc < len(left) and rc < len(right): if left[lc] <= right[rc]: res.append(left[lc]) lc += 1 else: res.append(r原创 2020-11-23 21:21:25 · 343 阅读 · 0 评论 -
快速排序-python极简版
def qsort(seq): if seq == [] or seq is None: return else: pivot = seq[0] lesser = qsort([x for x in seq[1:] if x<pivot]) greater = qsort([x for x in seq[1:] if x>=pivot]) return lesser + [pivot] + grea.原创 2020-11-22 01:12:35 · 126 阅读 · 0 评论 -
python排序sort函数实践
https://blog.csdn.net/bestsort/article/details/79339682原创 2019-01-22 10:16:23 · 373 阅读 · 0 评论 -
python的for循环中remove或del的坑
a = [1,2,3,4,5]for i in a: a.remove(i)print a#[2,4]1、当第一次删除后,后面的元素会前移,此时a=[2,3,4,5]#第一个元素可以正常删除2、然后指针下一次会指向新列表的第二个元素(即初始状态的第三个位置),从而初始状态的第二个位置被跳过了,初始第三个位置被删除,接着后面的元素又再次前移(变成[2,4,5])#指针对应...原创 2019-01-29 22:08:54 · 1136 阅读 · 0 评论 -
python 如何捕获异常详细信息
当我们需要对异常进行捕获来保证程序的健壮。但是debug的时候,我们可能需要异常的详细信息,这时可以使用sys.exc_info来处理:import tracebackimport systry: raise ValueError('this is a exp')except Exception as ex: ex_type, ex_val, ex_stack = ...原创 2019-04-05 16:24:41 · 4227 阅读 · 0 评论 -
python如何表示无穷大的数
float also accepts the strings “nan” and “inf” with an optional prefix “+” or “-” for Not a Number (NaN) and positive or negative infinity.float("-inf") == -float("inf")result is True,负无穷的两种表达法...原创 2019-04-08 21:37:05 · 6811 阅读 · 0 评论 -
echo -e对交互式脚步传入参数
ehco命令的-e选项:激活转义字符。使用-e选项时,若字符串中出现以下字符,则特别加以处理,而不会将它当成一般文字输出:•\a 发出警告声;•\b 删除前一个字符;•\c 最后不加上换行符号;•\f 换行但光标仍旧停留在原来的位置;•\n 换行且光标移至行首;•\r 光标移至行首,但不换行;•\t 插入tab;•\v 与\f相同;•\\ 插入\字符;•\nnn 插入n...原创 2019-04-28 16:37:24 · 2399 阅读 · 0 评论 -
os.exec**与os.system的区别
对于os.exec**(path, args) 在执行它的时候,使用的是当前进程,也就是系统不会为括号中执行的程序重新分配进程, 不返回执行结果 对于os.system(cmd) 相当于在命令行直接输入该语句,会返回执行结果,但是系统会自动为它所执行的程序分配进程代码demoimport osfrom multiprocessing import Pr...原创 2019-05-08 22:09:19 · 1981 阅读 · 0 评论 -
python -m 导入模块启动
两种方式启动:python xxx.py -----直接运行的方式启动(此时脚本__name__为"__main__") python -m xxx.py ------以模块的方式启动(此时脚本的__name__属性值依然不再是"__main__"而是"xxx")不同的加载py文件的方式,主要是影响——sys.path 这个属性。sys.path 就相当于liunx中的PATH...原创 2019-07-02 20:46:42 · 704 阅读 · 0 评论 -
python中__get__(),__getattr__()和__getattribute__()的区别
https://www.jianshu.com/p/885d59db57fc原创 2019-01-16 11:05:04 · 584 阅读 · 0 评论 -
python中对list的操作append和“+”有什么不同
Using list.append modifies the list in place - its result is None. Using + creates a new list. 看看下面两段代码:demo1class Solution(object): def permute(self, nums): """ :type num...原创 2018-12-20 09:56:22 · 764 阅读 · 0 评论 -
sliding window 编程范式python
滑动窗口(Sliding Window)问题经常使用快慢指针(slow, fast pointer)[0, slow) 的区域为滑动窗口已经探索过的区域 [slow, fast]的区域为滑动窗口正在探索的区域 (fast, end of array)为待探索的区域 Sliding Window的问题主要分为:fixed size sliding window dynamic siz...原创 2018-12-14 14:01:02 · 1651 阅读 · 0 评论 -
subprocess模块的使用——python
subprocess.call()用来调用系统命令(替代os.system()) subprocess.Popen():(替代os.popen())在一些复杂场景中,我们需要将一个进程的执行输出作为另一个进程的输入。在另一些场景中,我们需要先进入到某个输入环境,然后再执行一系列的指令等。这个时候我们就需要使用到suprocess的Popen()方法。该方法有以下参数:args:sh...转载 2018-07-20 16:13:05 · 932 阅读 · 0 评论 -
contextlib里的closing上下文管理
推荐使用 "with"语句 以管理文件:with open("hello.txt") as hello_file: for line in hello_file: print line对于不支持使用"with"语句的类似文件的对象,使用 contextlib.closing():import contextlibwith contextlib.closing(url...原创 2018-08-06 11:42:54 · 1221 阅读 · 0 评论 -
python默认参数的坑以及最佳实践
// An exampledef addEnd(L=[]): L.append("end") return Lprint addEnd()#["end"]print addEnd()#["end", "end"]坑!!!对一切的列表有记忆了,不是我们想要的print的结果和想象中不同, 从上述调用结果发现,函数每次都记住了上次原创 2018-10-19 15:54:49 · 329 阅读 · 0 评论 -
验证ip是否合法的工程实践-python
验证合法ip用正则太麻烦,socket模块拿来用就好,import socketdef validate_ip_address(ipstr, proto=4): """ Validate specified IP address is legal IPv4 or IPv6 address """ family = socket.AF_INET6原创 2018-11-09 11:25:30 · 1694 阅读 · 0 评论 -
信号量的理解Semaphore
信号量的概念和锁很像,不过它是一次给你好几把锁,这样就可以实现同时让 限定个数的进程来获取某个资源,看下面的代码from multiprocessing import Process, Semaphoreimport randomimport timedef ktv(i,sem): sem.acquire() print("%s walked in ktv"%i) ...原创 2018-11-10 15:23:50 · 256 阅读 · 0 评论 -
python中__name__变量的值
在python代码里我们经常可以看到if __name__ =="__main__":竟然这里要对__name__的值进行判断,故我们可以本能的猜到该变量还有别的值,那么__name__变量到底等于什么值呢?在python直接运行某个文件时:> python somefile.pypython会自动将__name__设为__main__,但是当某个文件被import时,那个文件...原创 2018-11-11 18:28:08 · 418 阅读 · 0 评论 -
将字典写入文件的例子-python
字典内容写入json时,需要用json.dumps将字典转换为字符串,然后再写入。json也支持格式,通过参数indent可以设置缩进,如果不设置的话,则保存下来会是一行。from collections import defaultdictimport jsonvideo = defaultdict(list)video["label"].append("haha")video["...原创 2018-11-08 17:02:15 · 3321 阅读 · 0 评论 -
带参数的装饰器-python
先来看看一般的情况def use_logging(func): def wrapper(*args, **kwargs): if level == 'warn': logging.warn('%s is running' % func.__name__) return func(*args) return wrapper...原创 2018-11-30 09:40:53 · 210 阅读 · 0 评论 -
Backtracking 编程范式python
choose-> explore-> unchoose paradigm is the key! below is the general pseudo code 来看看例子一:Given a collection of distinct integers, return all possible permutations.Input: [1,2,3]Output...原创 2018-12-19 20:05:58 · 612 阅读 · 0 评论 -
Python参数传入,自己实现mutable string
参数传入有两种:1.passing by value2.passing by referenceC++ can pass-by-value and pass-by-referenceStraightforward. You can simulate pass-by-reference with pointers. eg:void grow(int& age){}it's easier t...转载 2018-03-22 15:57:34 · 483 阅读 · 0 评论