python 计算倒数代码class Expr: def __add__(self, other): return Plus(self, other) def __mul__(self, other): return Times(self, other) class Int(Expr): def……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2085浏览 2675个赞
python实现二分查找算法# low and high have to be integersdef binarySearch(array, key, low, high): if low > high: # termination case return -1 middle = (low + high) / ……继续阅读 » 水墨上仙 4年前 (2021-03-05) 1459浏览 168个赞
转载自:……继续阅读 » 水墨上仙 4年前 (2021-03-05) 1234浏览 2897个赞
python版的快速排序算法代码def qsort(L): return ((qsort([x for x in L[1:] if x < L[0]]) + L[0:1] + qsort([x for x in L[1:] if x >= L[0]]) ) if L ……继续阅读 » 水墨上仙 4年前 (2021-03-05) 1617浏览 2959个赞
python里的generator函数 定义一个generator函数:#basic syntax(a function that yield)def genMul2(N): for i in range(N): yield i * 2for i in genMul2……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2346浏览 726个赞
break语句是用来 终止 循环语句的,即哪怕循环条件没有称为False或序列还没有被完全递归,也停止执行循环语句。一个重要的注释是,如果你从for或while循环中 终止 ,任何对应的循环else块将不执行。#!/usr/bin/python# Filename: break.pywhile True: s = raw_input(……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2490浏览 737个赞
continue语句被用来告诉Python跳过当前循环块中的剩余语句,然后 继续 进行下一轮循环。#!/usr/bin/python# Filename: continue.pywhile True: s = raw_input('Enter something : ') if s == 'quit……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2582浏览 1631个赞
python 下载指定网址的所有图片#coding=utf-8#download pictures of the url#useage: python downpicture.py www.baidu.comimport osimport sysfrom html.parser import HTMLParserfrom urllib.re……继续阅读 » 水墨上仙 4年前 (2021-03-05) 1631浏览 387个赞
if语句用来检验一个条件, 如果 条件为真,我们运行一块语句(称为 if-块 ), 否则 我们处理另外一块语句(称为 else-块 )。 else 从句是可选的。#!/usr/bin/python# Filename: if.py number = 23guess = int(raw_input('Enter an integer : ……继续阅读 » 水墨上仙 4年前 (2021-03-05) 1407浏览 2226个赞
python使用for循环语句范例代码,for..in是另外一个循环语句,它在一序列的对象上 递归 即逐一使用队列中的每个项目。我们会在后面的章节中更加详细地学习序列#!/usr/bin/python# Filename: for.pyfor i in range(1, 5): print ielse: print 'T……继续阅读 » 水墨上仙 4年前 (2021-03-05) 1692浏览 502个赞
python中while语句使用范例代码#!/usr/bin/python# Filename: while.pynumber = 23running = Truewhile running: guess = int(raw_input('Enter an integer : ')) if guess == ……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2526浏览 871个赞
最简单的Python读取文本文件的方法 f = open("c:\\1.txt","r") lines = f.readlines()#读取全部内容 for line in lines print line ……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2843浏览 2391个赞
python中遍历字典的方法出自:“Larry学习之路” 博客来源:http://5iqiong.blog.51cto.com/2999926/806230#!/usr/bin/python dict={"a":"apple","b":"banana","o&q……继续阅读 » 水墨上仙 4年前 (2021-03-05) 1361浏览 629个赞
map函数有的时候可以用来替代循环def add1(a): return a + 1def add2(a, b): return a + bdef add3(a, b, c): return a + b + ca1 = [1,2,3,4,5]a2 = [1,2,3,4,5]a3 = [1,2,3,4,5]b = ma……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2781浏览 659个赞
有时候我们可以需要处理一个列表里的所有元素,可以使用一个for循环来完成这个工作。但是Python内置的map函数可以帮我们的忙,它接受函数和列表作为参数,然后返回函数处理之后的列表:#map function#basic syntaxdef mul2(x): return x*2testList = [1,2,3,4]print map(……继续阅读 » 水墨上仙 4年前 (2021-03-05) 1411浏览 2588个赞
python中如何定义函数,函数是重用的程序段。它们允许你给一块语句一个名称,然后你可以在你的程序的任何地方使用这个名称任意多次地运行这个语句块。这被称为 调用 函数。我们已经使用了许多内建的函数,比如len和range。函数通过def关键字定义。def关键字后跟一个函数的 标识符 名称,然后跟一对圆括号。圆括号之中可以包括一些变量名,该行以冒号结尾。接下来……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2493浏览 2823个赞
Certificate validating HTTPSHandler class, VerifiedHTTPSHandler, for Python 2.7from httplib import HTTPSConnectionimport urllib2import socketimport sslclass VerifiedHTTPSCon……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2608浏览 2023个赞
lambda可以理解为一种小函数,但是它是一个表达式,而不是一个语句,所以在def不允许出现的地方仍然可以使用lambda函数,例如list里。但是lambda内只可以执行一个表达式。#lambda#普通的函数def add(x,y): return x+yprint add(1,2)#lambda函数func = lambda x,y:……继续阅读 » 水墨上仙 4年前 (2021-03-05) 1191浏览 2201个赞
函数取得的参数是你提供给函数的值,这样函数就可以利用这些值 做 一些事情。这些参数就像变量一样,只不过它们的值是在我们调用函数的时候定义的,而非在函数本身内赋值。参数在函数定义的圆括号对内指定,用逗号分割。当我们调用函数的时候,我们以同样的方式提供值。注意我们使用过的术语——函数中的参数名称为 形参 而你提供给函数调用的值称为 实参 。#!/usr/b……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2464浏览 2761个赞
python里的列表list使用范例代码#list#新建列表testList=[10086,'中国移动',[1,2,4,5]]#访问列表长度print len(testList)#到列表结尾print testList[1:]#向列表添加元素testList.append('i\'m new he……继续阅读 » 水墨上仙 4年前 (2021-03-05) 1611浏览 1912个赞
Python读取tomcat的日志并打印日期 import re regx = "\d\d\d\d-\d\d-\d+" f = open("c:\stdout.log","r") i = 0 for str in f.readlines(): ……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2835浏览 1786个赞
Python是一种面向对象的开发语言,在函数中使用全局变量,一般应作全局变量说明,只有在函数内经过说明的全局变量才能使用,下面介绍下Python全局变量有关问题。应该尽量避免使用Python全局变量。不同的模块都可以自由的访问全局变量,可能会导致全局变量的不可预知性。对全局变量,如果程序员甲修改了_a的值,这时可能导致程序中的错误。这种错误是很难发现和更……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2744浏览 512个赞
这段代码通过访问whatismyip.org网站获得本机ip地址import urllib2def get_external_ip(): ip = urllib2.urlopen("http://whatismyip.org").read() return ip……继续阅读 » 水墨上仙 4年前 (2021-03-05) 1262浏览 702个赞
The Python map() function returns a list of the results of applying the function to the items of the argument sequence(s).The fmap() function does the inverse, in a sense. It retur……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2098浏览 204个赞
每次开始写Python打开文件第一件事就是写上#!/usr/bin/env python和编码之类的东西,太多了,写烦就,写了一个打开Python自动填充的函数,将下面内容添加到~/.vimrc下即可每次打开如果没有上述行则会自动填充:转自:http://www.linuxzen.com/function InsertPythonHeader() ……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2993浏览 1396个赞
python中得thread的一些机制和C/C++不同:在C/C++中,主线程结束后,其子线程会默认被主线程kill掉。而在python中,主线程结束后,会默认等待子线程结束后,主线程才退出。 python对于thread的管理中有两个函数:join和setDaemonjoin:如在一个线程B中调用threada.join(),则thre……继续阅读 » 水墨上仙 4年前 (2021-03-05) 1174浏览 2163个赞
python一个简单的lisp实#coding=utf-8import types,sysimport operator as opfrom functools import reducefrom inspect import getargspec##########################def quote(code): ……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2474浏览 2146个赞
python 在控制条输出进度条 A ”progress bar” that looks like:|#############################----……继续阅读 » 水墨上仙 4年前 (2021-03-05) 1198浏览 1603个赞
用这段代码可以用来打包自己的文件夹为zip,我就用这段代码来备份import zipfilez = zipfile.ZipFile('my-archive.zip', 'w', zipfile.ZIP_DEFLATED)startdir = "/home/johnf"for dirpat……继续阅读 » 水墨上仙 4年前 (2021-03-05) 3090浏览 830个赞
这段代码调用ie浏览器下载网页上的所有图片,并保存到本地import win32com.client,time,win32inet,win32file,osclass ImgDownloader: def __init__(self,url,dir): self.__dir=dir self.__ie=win3……继续阅读 » 水墨上仙 4年前 (2021-03-05) 1989浏览 2007个赞
python获取本机网卡的mac地址import uuidprint ':'.join(['{:02x}'.format((uuid.getnode() >> i) & 0xff) for i in range(0,8*6,8)][::-1])……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2732浏览 2747个赞
Python超简单截取中文字符串代码 web应用难免会截取字符串的需求,Python中截取英文很容易:>>> s = 'abce'>>> s[0:3]'abc'但是截取utf-8的中文机会截取一半导……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2961浏览 1733个赞
python对MySqldb的简单封装#!/usr/local/python/bin# coding=utf-8'''Implements a simple database interfaceExample 0: Create connection: # Set auto commit to false ……继续阅读 » 水墨上仙 4年前 (2021-03-05) 1828浏览 1423个赞
python得到N以内的所有的质数import stringdef isPrime(num): i=2 while(i<num): if (0== num%i): return False break else: i=i+1……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2357浏览 1811个赞
通过python获取当前mac地址的方法如下 (1)通用方法,借助uuid模块def get_mac_address(): import uuid node = uuid.getnode() mac = uuid.UUID(int = node).hex……继续阅读 » 水墨上仙 4年前 (2021-03-05) 1468浏览 2214个赞
python一个简单的计算过期时间的算法def time_passed(value): now = datetime.now() past = now - value if past.days: return u'%s天前' % past.days mins = past.second……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2372浏览 2447个赞
python 中yield的用法详解 包含了关键字”yield”的函数就不是普通的函数。当含有这个关键字的函数被调用的时候,这个函数在遇到yield的时候会停止运行,并且返回一个迭代器(iterator)。每次请求一个值,就会执行生成的代码。直到遇到一个yiel……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2605浏览 175个赞
python提取网址URL的域名部分#!/usr/bin/python# Desc : fetch the domain name of website import reimport optparse def get_filename(): p = optparse.OptionParser() p.add_optio……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2850浏览 160个赞
python批量下载新浪博客的代码来源:https://github.com/dangoakachan/Dango-Scripts/blob/master/Others/sinablog_download.py# coding=utf-8 import urllib2import sys, osimport reimport stringfr……继续阅读 » 水墨上仙 4年前 (2021-03-05) 1267浏览 2580个赞
python数组复制拷贝方法 python中直接通过等号赋值实际上只是引用地址的传递如:a = [1,2,3,4,5]b=a 当a的值改变时,b的值也会随之改变如果希望b和a没有关系,可以通过下面的方法……继续阅读 » 水墨上仙 4年前 (2021-03-05) 1596浏览 2308个赞
python编写的FTP弱口令扫描器 python FTP暴力破解部分代码#!/usr/local/bin/python#-*- coding: UTF-8 -*-#####################################################################qq:316118740#BLOG:http……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2928浏览 1147个赞
Python 扫描IP段 指定端口是否开放 TCP21.py #!/usr/local/bin/python#-*- coding: UTF-8 -*-######################################################……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2512浏览 1715个赞
python 实现 数独算法# -*- coding: utf-8 -*-'''Created on 2012-10-5@author: Administrator'''from collections import defaultdictimport itertoolsa = ……继续阅读 » 水墨上仙 4年前 (2021-03-05) 1442浏览 1624个赞
Python 判断文件和文件夹是否存在import osos.path.isfile('test.txt') #如果不存在就返回Falseos.path.exists(directory) #如果目录不存在就返回False……继续阅读 » 水墨上仙 4年前 (2021-03-05) 2192浏览 1967个赞
我需要实现一个Windows下远程连接到SSH服务器执行命令的功能,所以就在网上找资料。我的环境是:Windows7 64位,Python 2.7 32位。按照网上的说法,需要下载pycrypto和paramiko两个模块进行安装。最后下载的版本是pycrypto2.3和paramiko1.7.6。 安装过程也比较简单,先安装pycryp……继续阅读 » 水墨上仙 4年前 (2021-03-05) 1413浏览 2556个赞