python字符串過(guò)濾性能比較5種方法
更新時(shí)間:2017年06月22日 14:32:15 投稿:lqh
這篇文章主要介紹了python字符串過(guò)濾性能比較5種方法的相關(guān)資料,需要的朋友可以參考下
python字符串過(guò)濾性能比較5種方法比較
總共比較5種方法。直接看代碼:
import random import time import os import string base = string.digits+string.punctuation total = 100000 def loop(ss): """循環(huán)""" rt = '' for c in ss: if c in '0123456789': rt = rt + c return rt def regular(ss): """正則表達(dá)式""" import re rt = re.sub(r'\D', '', ss) return rt def filter_mt(ss): """函數(shù)式""" return filter(lambda c:c.isdigit(), ss) def list_com(ss): """列表生成式""" isdigit = {'0': 1, '1': 1, '2': 1, '3': 1, '4': 1, '5':1, '6':1, '7':1, '8':1, '9':1}.has_key return ''.join([x for x in ss if isdigit(x)]) def str_tran(ss): """string.translate()""" table = string.maketrans('', '') ss = ss.translate(table,string.punctuation) return ss if __name__ == '__main__': lst = [] for i in xrange(total): num = random.randrange(10, 50) ss = '' for j in xrange(num): ss = ss + random.choice(base) lst.append(ss) s1 = time.time() map(loop,lst) print "loop: ",time.time() - s1 print '*'*20 s1 = time.time() map(regular, lst) print "regular: ", time.time() - s1 print '*' * 20 s1 = time.time() map(str_tran, lst) print "str_tran: ", time.time() - s1 print '*' * 20 s1 = time.time() map(filter_mt, lst) print "filter_mt: ", time.time() - s1 print '*' * 20 s1 = time.time() map(list_com, lst) print "list_com: ", time.time() - s1
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
您可能感興趣的文章:
- Python sql注入 過(guò)濾字符串的非法字符實(shí)例
- Python中字符串String的基本內(nèi)置函數(shù)與過(guò)濾字符模塊函數(shù)的基本用法
- python實(shí)現(xiàn)mysql的單引號(hào)字符串過(guò)濾方法
- python過(guò)濾字符串中不屬于指定集合中字符的類(lèi)實(shí)例
- Python 過(guò)濾字符串的技巧,map與itertools.imap
- Python中使用filter過(guò)濾列表的一個(gè)小技巧分享
- Python學(xué)習(xí)小技巧之列表項(xiàng)的推導(dǎo)式與過(guò)濾操作
- Python過(guò)濾列表用法實(shí)例分析
- Python中過(guò)濾字符串列表的方法
相關(guān)文章
Django Admin 管理工具的實(shí)現(xiàn)
這篇文章主要介紹了Django Admin 管理工具的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05Python實(shí)現(xiàn)Smtplib發(fā)送帶有各種附件的郵件實(shí)例
本篇文章主要介紹了Python實(shí)現(xiàn)Smtplib發(fā)送帶有各種附件的郵件實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06python實(shí)現(xiàn)跳表SkipList的示例代碼
這篇文章主要介紹了python實(shí)現(xiàn)跳表SkipList的示例代碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-07-07用Python 爬取貓眼電影數(shù)據(jù)分析《無(wú)名之輩》
這篇文章主要介紹了用Python 爬取貓眼電影數(shù)據(jù)分析《無(wú)名之輩》,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07Tensorflow 實(shí)現(xiàn)分批量讀取數(shù)據(jù)
今天小編就為大家分享一篇Tensorflow 實(shí)現(xiàn)分批量讀取數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01vscode調(diào)試django項(xiàng)目的方法
這篇文章主要介紹了vscode調(diào)試django項(xiàng)目的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08Python實(shí)現(xiàn)FTP文件定時(shí)自動(dòng)下載的步驟
這篇文章主要介紹了Python實(shí)現(xiàn)FTP文件定時(shí)自動(dòng)下載的示例,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-12-12