欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

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

发布时间:2025/3/20 python 41 豆豆
生活随笔 收集整理的这篇文章主要介绍了 将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访问子字符串的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。