将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访问子字符串相关推荐
- floodlight ovs 更改拓扑_淘宝更改类目降权多久?被降权了怎么办?_推广运营(淘宝天猫)...
现在淘宝 (淘宝论坛)店主在经营店铺的过程中,会考虑将自己特别店铺进行类目的更改,那么也有一点店主会担心到如果淘宝更改类目会被降权吗?如果被降权了怎么办呢?那就一起了解具体的内容吧! 跟大家分享一些淘 ...
- macos安装python3.6_在CentOS 7/Ubuntu 16.04/Debian 9/macOS上安装Python 3.6的方法
本文介绍在CentOS 7/Ubuntu 16.04/Debian 9/macOS操作系统上安装Python 3.6的方法,包括使用Python 3.6创建virtualenv,目前大多数Linux发 ...
- Python:Python语言的简介(语言特点/pyc介绍/Python版本语言兼容问题(python2 VS Python3))、安装、学习路线(数据分析/机器学习/网页爬等编程案例分析)之详细攻略
Python:Python语言的简介(语言特点/pyc介绍/Python版本语言兼容问题(python2 VS Python3)).安装.学习路线(数据分析/机器学习/网页爬等编程案例分析)之详细攻略 ...
- python3版本升级和系统更新_如何更新mac系统自带的python版本到最新3.3
发表于 2013-11-21 07:14:15 by 月小升 先去 把第一步里安装好的Python目录移到原本系统所持有的目录位置. sudo mv /Library/Frameworks/Pytho ...
- python3.0正式发布的年份是_来喽,来喽,Python 3.9正式版发布了~~~
摘要:2020年10月5日,在全国人员欢度国庆节和中秋节时,Python 3.9 悄摸摸地正式发布了. 2020年10月5日,在全国人员欢度国庆节和中秋节时,Python 3.9 悄摸摸地正式发布了. ...
- 【Python】Python3.7.3 - Windows安装Python以及Python安装目录结构详解
文章目录 Python 3.7.3安装 安装程序下载 启动安装程序 定制安装 设置安装目录 安装完毕 验证安装成功 检查Windows环境变量 确认Python版本 探索Python的安装目录 安装目 ...
- python参数类型限定_python限定方法参数类型、返回值类型、变量类型等|python3教程|python入门|python教程...
https://www.xin3721.com/eschool/python.html typing模块的作用 自python3.5开始,PEP484为python引入了类型注解(type hints ...
- linux降内核版本_ubuntu18.04 降内核版本的问题
图1 2. 步骤1之后重启机器发现没有网卡驱动,也没有显卡驱动,需要重新安装 图2 3. deb安装: 网卡和显卡驱动时可能会提示找不到对应的linux-headers,这时候就要查看linux内核版 ...
- anaconda 设置python3为主_关于在Windows、Linux和Mac上安装设置Python的问题
介绍 在你的机器上安装Python是不是很困难?这实际上是我在数据科学初学者中看到的一个非常常见的问题.安装在理论上可能看起来很简单,但在现实中可能会有点问题. 我个人在尝试在我的Linux和Wind ...
最新文章
- 深度学习在计算机视觉领域(包括图像,视频,3-D点云,深度图)的应用一览
- 批量ping工具fping
- This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its de 错误解决办法
- Spring源码-applicationcontext.xml解析过程
- SQL SERVER 2005中如何获取日期(一个月的最后一日、一年的第一日等等)
- [转]Eclipse下的JavaScript编辑器中文无法保存问题解决
- 你尝试登录的服务器语言不通,七骑士国内服务器正式上线后,各种登录问题解决方案汇总...
- TypeScript 类
- Azure SQL性能调优实践
- Jquery动态进行图片缩略
- 今日份教学:怎么压缩PDF文件大小
- mac chrome更新错误12
- 连接远程电脑的时候提示:此计算机无法连接到远程计算机
- 语音合成商业化:科大讯飞向左,魔音工坊向右
- Linux下C语言 对pow、exp未定义引用问题
- 运维必备的DevOps工具链大盘点
- 75个JavaScript面试题集锦,内含解答,自测 JS 掌握程度
- python最强web框架fastapi,比肩go,nodejs
- 牛客 牛牛与LCM(LCM)
- linux数据库分析报告,写linux数据
热门文章
- 黑马程序员_网络编程总结
- rdcl 报表设置不分页
- Ext.menu.Menu菜单栏
- Http:Get、Post、Put、Delete、Head、Options详解
- [Apache] Apache 從 2.2 換至 2.4 httpd.conf 的調整筆記 (windows 環境)
- oracle mysql sql serve where in 语句的不同
- tomcat配置用户数据
- 安装完php后iis 503,IIS上安装PHP5.3的完整教程方法(及5.3.0以后版本的方法)
- [面向对象] ABAP中程序类如何转化成全局类
- 19、Power Query-快速分析各产品的销售情况