日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

将python3.7降为3.5_python3.7降至3.5【python cookbook】python访问子字符串

發布時間:2025/3/20 python 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 将python3.7降为3.5_python3.7降至3.5【python cookbook】python访问子字符串 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

訪問子字符串最簡單的的方式是使用切片

afiled=theline[3:8]但一次只能取一個子字符串

如果還要考慮字段的長度struct.unpack可能更合適

importstruct

#得到一個5字節的字符串跳過三字節得到兩個8字節的字符串以及其余部分

baseformat="5s3x8s8s"

#theline超出的長度也由這個base-format確定

numremain=len(theline)-struct.calcsize(baseformat)

#用合適的s或者x字段完成格式然后unpack

format="%s%ds"%(baseformat,numremain)

l,s1,s2,t=struct.unpack(format,theline)#test

>>>theline="numremain=len(theline)-struct.calcsize(baseformat)"

>>>numremain=len(theline)-struct.calcsize(baseformat)

>>>format="%s%ds"%(baseformat,numremain)

>>>format

'5s3x8s8s30s'

>>>l,s1,s2,t=struct.unpack(format,theline)

>>>l

'numre'

>>>s1

'n=len('

>>>s2

'theline)'

>>>t

'-struct.calcsize(baseformat)'

如果獲取固定字長的數據,可以利用帶列表推導(LC)的切片方法

www.002pc.com認為此文章對《python3.7降至3.5【python cookbook】python訪問子字符串》說的很在理。

pieces=[theline[k:k+n]forkinxrange(0,len(theline),n)]如果想把數據切成指定長度的列用帶LC的切片方法比較容易實現

cuts=[8,14,20,26,30]

pieces=[theline[i,j]forijinzip([0]+cuts,cuts+[None])]在LC中調用zip,返回的是一個列表每項形如cuts[k],cuts[k+1]

第一項和最后一項為(0,cuts[0])(cuts[len(cuts)-1],None)

將以上代碼片段封裝成函數deffields(baseformat,theline,lastfield=False):

#theline超出的長度也有這個base-format確定

#(通過struct.calcsize計算切片的長度)

numremain=len(theline)-struct.calcsize(baseformat)

#用合適的s或者x字段完成格式然后unpack

format="%s%d%s"%(baseformat,numre

下邊這個是使用memoizing機制的版本

deffields(baseformat,theline,lastfield=False,_cache={}):

#生成鍵并嘗試獲得緩存的格式字符串

key=baseformat,len(theline),lastfield

format_cache.get(key)

ifformatisNone:

#m沒有緩存的格式字符串創建并緩存

numremain=len(theline)-struct.calcsize(baseformat)

_cache[key]=format="%s%d%s"%(

baseformat,numremain,lastfieldand"s"or"x")

returnstruct.unpack(format,theline)cookbook上說的這個比優化之前的版本快30%到40%不過如果這里不是瓶頸部分,沒必要使用這種方法

使用LC切片函數

defsplit_by(theline,n,lastfield=False):

#切割所有需要的片段

pieces=[theline[k:k+n]forkinxrange(0,len(theline),n)]

#弱最后一段太短或不需要,丟棄

ifnotlastfieldandlen(pieces[-1]

pieces.pop()

returnpiecesdefsplit_at(theline,cuts,lastfield=False):

#切割所有需要的片段

pieces=[theline[i,j]forijinzip([0]+cuts,cuts+[None])]

#若不需要最后一段丟棄

ifnotlastfield:

pieces.pop()

returnpieces

使用生成器的版本

defsplit_at(the_line,cuts,lastfield=False):

last=0

forcutincuts:

yieldthe_line[last:cut]

last=cut

iflastfield:

yieldthe_line[last:]

defsplit_by(the_line,n,lastfield=False):

returnsplit_at1(the_line,xrange(n,len(the_line),n),lastfield)zip()的用法

zip([iterable,...])Thisfunctionreturnsalistoftuples,wherethei-thtuplecontainsthei-thelementfromeachoftheargumentsequencesoriterables.Thereturnedlististruncatedinlengthtothelengthoftheshortestargumentsequence.Whentherearemultipleargumentswhichareallofthesamelength,zip()issimilartomap()withaninitialargumentofNone.Withasinglesequenceargument,itreturnsalistof1-tuples.Withnoarguments,itreturnsanemptylist.

Theleft-to-rightevaluationorderoftheiterablesisguaranteed.Thismakespossibleanidiomforclusteringadataseriesinton-lengthgroupsusingzip(*[iter(s)]*n).

zip()inconjunctionwiththe*operatorcanbeusedtounzipalist:>>>x=[1,2,3]

>>>y=[4,5,6]

>>>zipped=zip(x,y)

>>>zipped

[(1,4),(2,5),(3,6)]

>>>x2,y2=zip(*zipped)

>>>x==list(x2)andy==list(y2)

True

>>>x2

(1,2,3)

>>>y2

(4,5,6)

生成器的用法參見這篇博客/content/5987587.html

更多:python3.7降至3.5【python cookbook】python訪問子字符串

https://www.002pc.comhttps://www.002pc.com/python/5654.html

你可能感興趣的python,cookbook,字符串,訪問

No alive nodes found in your cluster

0踩

0 贊

總結

以上是生活随笔為你收集整理的将python3.7降为3.5_python3.7降至3.5【python cookbook】python访问子字符串的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。