Time of Update: 2016-06-06
python 2.6 引入了itertools模組,使得排列組合的實現非常簡單: 代碼如下:import itertools 有序排列:e.g., 4個數內選2個排列: 代碼如下:>>> print list(itertools.permutations([1,2,3,4],2))[(1, 2), (1, 3), (1, 4), (2, 1), (2, 3), (2, 4), (3, 1), (3, 2), (3, 4), (4, 1), (4, 2), (4,
Time of Update: 2016-06-06
樣本程式如下: >>> a = [-1, 3, 'aa', 85] # 定義一個list>>> a[-1, 3, 'aa', 85]>>> del a[0] # 刪除第0個元素>>> a[3, 'aa', 85]>>> del a[2:4] # 刪除從第2個元素開始,到第4個為止的元素。包括頭不包括尾>>> a[3, 'aa']>>> del a # 刪除整個list>>> aTraceback (most recent call last): File "", line 1, in
Time of Update: 2016-06-06
代碼如下:# -*- coding:utf-8 -*-import httplibimport urllibimport stringimport redef getContent(): #從網站中擷取所有內容 conn = httplib.HTTPConnection("tu.duowan.com") conn.request("GET", "/m/meinv/index.html") r = conn.getresponse() print
Time of Update: 2016-06-06
核心代碼:#!/usr/bin/env python # -*- coding: utf-8 -*- from bisect import bisect _LIST1, _LIST2 = [], [] _INIT = False ip2int = lambda ip_str: reduce(lambda a, b: (a 0) if len(_LIST1) 代碼打包下載 http://xiazai.bitsCN.com/201105/yuanma/ipaddress.7z
Time of Update: 2016-06-06
這兩天閑來無事在百度上淘了點圖片,不多,也就幾萬張吧,其中有不少美女圖片奧!哈哈!這裡暫且不說圖片是怎麼獲得的,咱聊聊得到圖片以後發生的事。 遇到的第一個問題就是有些圖片沒有尾碼名。在windows下,沒有尾碼名的檔案是不能正確被識別的,沒有預覽,開啟時還要選擇開啟檔案,費勁!這個問題比較容易解決,給每個圖片加上尾碼名就是了。沒有尾碼名的圖片也不多,不到1000張吧,一張一張地改很麻煩,還好我是學電腦的,上午寫了個程式批量修改http://www.bitsCN.com/article/30400
Time of Update: 2016-06-06
所以就想把這些照片翻著看一遍,可是拷出來的照片手機 裡是按時間自動分檔案夾的,一個一個檔案夾拷很是麻煩,於是打算寫個python小指令碼來完成這個工作(扯這麼多,終於 到主題了,囧) 這是待拷貝的檔案夾根目錄,每個子目錄下都有若干照片。 廢話少說,上代碼: 代碼如下: # -*- coding: utf-8 -*- #!/usr/bin/python #Filename:copyfile.py import os,shutil def mycopy(srcpath,dstpath): if
Time of Update: 2016-06-06
繼續List: 刪除元素: 代碼如下: a =[1, 2, 3, 4] a[2:3] = [] #[1, 2, 4] del a[2] #[1, 2] 清空list 代碼如下: a[ : ] = [] del a[:] list作為棧使用(後入先出): 代碼如下: stack = [3, 4, 5] stack.append(6) stack.append(7) stack.pop() # 7 stack.pop() # 6 stack.pop() # 5 用負數索引: 代碼如下:
Time of Update: 2016-06-06
我採用的是MySQLdb操作的MYSQL資料庫。先來一個簡單的例子吧: 代碼如下: import MySQLdb try: conn=MySQLdb.connect(host='localhost',user='root',passwd='root',db='test',port=3306) cur=conn.cursor() cur.execute('select * from user') cur.close() conn.close() except MySQLdb.Error,e:
Time of Update: 2016-06-06
理論不多說: 代碼如下: #!/usr/bin/python import sys def merge(array, q, p, r): left_array = array[q:p+1] right_array = array[p+1:r+1] left_array_num = len(left_array) right_array_num = len(right_array) i, j , k= [0, 0, q] while i if (left_array[i] array[k] =
Time of Update: 2016-06-06
內送郵件 代碼如下:import poplib,pdb,email,re,timefrom email import headerPOP_ADDR = r'pop.126.com'USER = ''PASS = ''CONFIG = ''def getYear(date): rslt = re.search(r'\b2\d{3}\b', date) return int(rslt.group())def getMonth(date): monthMap = {'Jan':1,'
Time of Update: 2016-06-06
代碼如下:import osimport sysimport MySQLdbdef getStatus(conn): query = " SHOW SLAVE STATUS " # print query cursor = conn.cursor() cursor.execute(query) result = cursor.fetchall() return result[0]def resolve(conn): cursor =
Time of Update: 2016-06-06
最近在搞基於python的webservice項目,今天為把環境給配好,折騰了不少時間,還是把配的過程記錄下來,以後備用:首先你系統上要有python,這個不必說啦,我系統上用的是2.7+其次,要用python進行webservice開發,還需要一些庫:lxml :命令列下 sudo easy_install lxml 就能安裝pytz : 命令列下 sudo easy_install pytz
Time of Update: 2016-06-06
一、服務端(Server.py) 服務端要做的事情是: 1. 建立一個Socket對象Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->import sockets = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 2. 綁定一個連接埠Code highlighting
Time of Update: 2016-06-06
什麼是迭代器?迭代器是帶有next方法的簡單對象,當然也要實現__iter__函數。迭代器能在一序列的值上進行迭代,當沒有可供迭代時,next方法就會引發StopIteration 的異常。python中有很多的對象都是迭代器,例如:列表,元素,字串,檔案,映射,集合如何使用迭代器?1. for 變數 in 可迭代對象 代碼如下: list1 = [1,2,3,4,5]for ele in list1: print ele,結果為:1 2 3 4 52. if 變數 in
Time of Update: 2016-06-06
>>> a = 2.5>>> b = 2.5>>> c = b>>> a is cFalse>>> a = 2>>> b = 2>>> c = b>>> a is cTrue在使用is函數的時候去列印a,b分別被賦值為2.5 和2的情況,發現:>>> a = 2>>> b = 2>>> id(a)21132060>>> id(b)21132060>>> a = 2.5>>> b = 2.5>>> id(a)19622112>>>
Time of Update: 2016-06-06
假設我們要添加一個我們自己的Middleware,用來記錄每次請求的日誌下面就是一個符合規範的Middleware, 建構函式中接受一個WSGI APP, __call__返回一個WSGI APP. 代碼如下:class LoggerMiddleware(object): '''WSGI middleware''' def __init__(self, application): self.app = application def __call__(self,
Time of Update: 2016-06-06
核心代碼: 代碼如下:#!/usr/bin/python#Filename:friendbook.pyimport cPickle as pimport sysimport timeimport osab={'Xdex':'cneds@fnedf.com', 'Laexly':'fev@fe.com', 'Fukc':'fexok@ver.com', 'Stifu':'stif@qq.com'}def Dumpfile(list):
Time of Update: 2016-06-06
python sys模組包含了與python解譯器和它的環境有關的函數,這個你可以通過dir(sys)來查看他裡面的方法和成員屬性 代碼如下:import sysprint dir(sys)result: 代碼如下:['__displayhook__', '__doc__', '__excepthook__', '__name__', '__package__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache',
Time of Update: 2016-06-06
代碼如下:#-*- coding:utf-8 -*- import threading import time def fun(name, ls_name, front_thread = None): ''''' 線程啟動函數 通過front_thread來使用線程有序的運行 ''' time.clock() time.sleep(2) # 如果front_thread存在,則在front_thread運行完成後,才運行當前線程 if front_thread != None:
Time of Update: 2016-06-06
發現一個簡單而又強大的讀寫設定檔的lib,http://www.voidspace.org.uk/python/configobj.html。個人覺得最大的亮點在於內建的格式校正功能,並且支援複雜的嵌套格式,而且使用起來也相當的簡便。來看例子吧。讀檔案 代碼如下:from configobj import ConfigObj config = ConfigObj(filename) # value1 = config['keyword1'] value2