99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀Python字符串拼接、截取及替換方法總結(jié)分析
Python字符串拼接、截取及替換方法總結(jié)分析
2017-11-18
收藏

Python字符串拼接、截取及替換方法總結(jié)分析

本文實(shí)例講述了Python字符串拼接、截取及替換方法。分享給大家供大家參考,具體如下:
python字符串連接
python字符串連接有幾種方法,我開始用的第一個(gè)方法效率是最低的,后來看了書以后就用了后面的2種效率高的方法,跟大家分享一下。

先介紹下效率比較低的方法:    
a = ['a','b','c','d']
content = ''
for i in a:
content = content + i
print content

content的結(jié)果是:'abcd'

后來我看了書以后,發(fā)現(xiàn)書上說的是python連接字符串,特別是在循環(huán)連接字符串的時(shí)候,他每次連接一次,就要重新開辟空間,然后把字符串連接起來,再放入新的空間,再一次循環(huán),又要開辟新的空間,把字符串連接起來放入新的空間,如此反復(fù),內(nèi)存操作比較頻繁,每次都要計(jì)算內(nèi)存空間,然后開辟內(nèi)存空間,再釋放內(nèi)存空間,效率非常低,你也許操作比較少的數(shù)據(jù)的時(shí)候看不出來,感覺影響不大,但是你碰到操作數(shù)據(jù)量比較多的時(shí)候,這個(gè)方法就要退休了,還是看看下面2個(gè)比較先進(jìn)的方法把。

先進(jìn)方法1:

用字符串的join方法:
    
a = ['a','b','c','d']
content = ''
content = ''.join(a)
print content

content的結(jié)果:'abcd'

先進(jìn)方法2:

用字符串的替換占位符替換    
a = ['a','b','c','d']
content = ''
content = '%s%s%s%s' % tuple(a)
print content

content的結(jié)果是:'abcd'

有興趣可以看看關(guān)于python字符串有關(guān)的 python 字符串替換 和 python 字符串截取

字符串截取

python的字符串是有序集合,我們可以通過索引來提取想要獲取的字符,可以把python的字符串也做為字符串的列表就更好理解。

python的字串列表有2種取值順序

1.是從左到右索引默認(rèn)0開始的,最大范圍是字符串長(zhǎng)度少1

s = 'ilovepython'

s[0]的結(jié)果是i

2.是從右到左索引默認(rèn)-1開始的,最大范圍是字符串開頭

s = 'ilovepython'

s[-1]的結(jié)果是n

上面這個(gè)是取得一個(gè)字符,如果你的實(shí)際要取得一斷字符的話,可以用到python 字符串截取功能

比如:s = 'ilovepython'

s[1:5]的結(jié)果是love

當(dāng)使用以冒號(hào)分隔的字符串,python返回一個(gè)新的對(duì)象,結(jié)果包含了以這對(duì)偏移標(biāo)識(shí)的連續(xù)的內(nèi)容,左邊的開始是包含了下邊界,比如上面的結(jié)果包含了s[1]的值l,而取到的最大范圍不包括上邊界,就是s[5]的值p

字符串替換

python 字符串替換是python操作字符串的時(shí)候經(jīng)常會(huì)碰到的問題,這里簡(jiǎn)單介紹下字符串替換方法。

python 字符串替換可以用2種方法實(shí)現(xiàn):

1.用字符串本身的replace方法
1
    
a.replace('word','python')

輸出的結(jié)果是hello python

2.用正則表達(dá)式來完成替換:
    
import re
strinfo = re.compile('word')
b = strinfo.sub('python',a)
print b

輸出的結(jié)果也是hello python

數(shù)據(jù)分析咨詢請(qǐng)掃描二維碼

若不方便掃碼,搜微信號(hào):CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對(duì)象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺(tái)檢測(cè)極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請(qǐng)參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請(qǐng)輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請(qǐng)輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }