Python_字符串查找与分隔
#字符串常用方法
s='apple,peach,banana,peach,pear'
#返回第一次出现的位置
print(s.find('peach'))
#指定位置开始查找
print(s.find('peach',7))
#指定范围中进行查找
print(s.find('peach',7,20))
#从字符串尾部向前查找
print(s.rfind('p'))
#返回首次出现的位置
print(s.index('p'))
#统计子字符串出现的次数
print(s.count('p')) #Python内置函数和内置对象的方法,运行速度快,并且运行稳定。
from string import ascii_letters
from random import choice
from time import time letters = ''.join([choice(ascii_letters) for i in range(999999)])
def positions_of_character(sentence,ch): #使用字符串呢对象的find()方法
result=[]
index=0
index=sentence.find(ch,index+1)
while index !=-1:
result.append(index)
index=sentence.find(ch,index+1)
return result def demo(s,c): #普通方法,逐个字符比较
result=[]
for i,ch in enumerate(s):
if ch==c:
result.append(i)
return result start = time()
positions_of_character(letters,'a')
print(time()-start) #0.008852958679199219 start=time()
p=demo(letters,'a')
print(time()-start) #0.0904378890991211 #split()从字符串左端开始将其分隔成多个字符串,并返回包含分隔结果的列表 #rsplit()从字符串右端开始将其分隔成多个字符串,并返回包含分隔结果的列表 #partition()、rpartition()用来以指定字符串为分隔符将原字符串分隔为3部分,即分隔符之前的字符串、分隔符字符串和分隔符之后的字符串。如果指定的分隔符不再原字符串中,则返回原字符串和两个空字符串 s='apple,peach,banana,pear'
li=s.split(',')
print(li)
li2=s.partition(',') #从左侧使用逗号进行切分
print(li2)
#('apple,peach,banana,pear', '', '')
li3=s.rpartition(',')
print(li3)
#('apple,peach,banana', ',', 'pear')
li4=s.rpartition('banana') #使用字符串作为分隔符
print(li4)
# ('apple,peach,', 'banana', ',pear')
s1='2014-10-31'
t=s1.split('-')
print(t)
# [2014, 10, 31]
li5=list(map(int,t)) #将分隔结果转换为整数
print(li5)
# ['hello', 'world', 'My', 'nname', 'is', 'zWrite'] #对于split()和rsplit()方法,如果不知定分隔符,则字符串中的任何空白字符(包括空格、换行符、制表符等)的连续出现都将被认为是分隔符,返回包含最终分隔结果的列表
s2='hello world\n\n My nname is zWrite'
li6=s2.split()
print(li6)
# ['hello', 'world', 'My', 'name', 'is', 'zWrite']
s3='\n\nhello world\n\n\n My name is zWrite '
li7=s3.split()
print(li7)
# ['hello', 'world', 'My', 'name', 'is', 'zWrite']
s4='\n\nhello\t\t world \n\n\n My name is zWrite '
li8=s4.split()
print(li8)
# ['hello', 'world \n\n\n My name is zWrite'] #split()与rsplit()方法允许指定最大分隔次数
s5='\n\nhello\t\t world \n\n\n My name is zWrite'
print(s5.split(maxsplit=1)) #分隔1次
li9=s5.rsplit(maxsplit=1)
print(li9)
# ['\n\nhello\t\t world \n\n\n My name is', 'zWrite']
li10=s5.split(maxsplit=20) #最大分隔次数大于实际可分隔次数时,自动忽略
print(li10)
#['hello', 'world', 'My', 'name', 'is', 'zWrite']
Python_字符串查找与分隔的更多相关文章
- Rabin-Karp指纹字符串查找算法
首先计算模式字符串的散列函数, 如果找到一个和模式字符串散列值相同的子字符串, 那么继续验证两者是否匹配. 这个过程等价于将模式保存在一个散列表中, 然后在文本中的所有子字符串查找. 但不需要为散列表 ...
- 自己动手写文件查找,字符串查找,查询jar包等工具
文件查找——搜索当前目录下的文件 知道大概的文件名称,使用 findf FileName findf.py import argparse, re, os from os.path import jo ...
- 关于字符串查找 charindex ,Patindex 还有一个like
字符串查找.在模糊朝找的情况下,其实3者的效率是差不多的.都需要一个一个取出来然后扫一遍╮(╯_╰)╭.然而用法还是会有一点儿的区别 1 charindex (查找的字符串,字符串表达式[,开始查找的 ...
- python 字符串查找
python 字符串查找有4个方法,1 find,2 index方法,3 rfind方法,4 rindex方法. 1 find()方法: )##从下标1开始,查找在字符串里第一个出现的子串:返回结果3 ...
- Sunday算法(字符串查找、匹配)
字符串查找算法中,最著名的两个是KMP算法(Knuth-Morris-Pratt)和BM算法(Boyer-Moore).两个算法在最坏情况下均具有线性的查找时间.但是在实用上,KMP算法并不比最简单的 ...
- lintcode:strStr 字符串查找
题目: 字符串查找 字符串查找(又称查找子字符串),是字符串操作中一个很有用的函数.你的任务是实现这个函数. 对于一个给定的 source 字符串和一个 target 字符串,你应该在 source ...
- Rabin-Karp字符串查找算法
1.简介 暴力字符串匹配(brute force string matching)是子串匹配算法中最基本的一种,它确实有自己的优点,比如它并不需要对文本(text)或模式串(pattern)进行预处理 ...
- php中常用的字符串查找函数strstr()、strpos()实例解释
string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] ) 1.$haystack被查找的字 ...
- 数据结构与算法--Boyer-Moore和Rabin-Karp子字符串查找
数据结构与算法--Boyer-Moore和Rabin-Karp子字符串查找 Boyer-Moore字符串查找算法 注意,<算法4>上将这个版本的实现称为Broyer-Moore算法,我看了 ...
随机推荐
- H5学习之旅-H5与Php交互(12)
1.首先介绍PHP开发环境的搭建 ,在Google搜apachefriends,会有xampp的下载链接,这个工具集成了apache的很多服务 2.搭建php的编辑环境,选取eclipse安装php插 ...
- nginx 编译增加新的模块
原已经安装好的nginx,现在需要添加一个未被编译安装的模块: nginx -V 可以查看原来编译时都带了哪些参数 原来的参数:--prefix=/app/nginx 添加的参数: --with-ht ...
- android cookie持久化
原博客地址:http://blog.csdn.net/shimiso/article/details/39033353 在解析网页信息的时候,需要登录后才能访问,所以使用httpclient模拟登录, ...
- STM32学习笔记(一)时钟和定时器
由于近期在准备海洋航行器比赛,正好趁此机会学习一下ARM,看到周围很多同学都在使用32,所以我也买了一块STM32F103ZET6,准备好好地学习一下. STM32的时钟系统相当的复杂,包含了5个时钟 ...
- hadoop学习大纲
- Process Order API - How To Scripts
In this Document Purpose Questions and Answers References APPLIES TO: Oracle Order Management ...
- erlang在redhat上的安装
erlang在redhat上的安装 1) 下载源码包: http://www.erlang.org/download/otp_src_17.3.tar.gz 2) RHEL6.4预安装包 $ yum ...
- MTK如何烧录IMEI码(俗称串号)
先介绍一下使用环境 主控:MT6582VX android版本:4.4.2 操作系统:windows XP SN烧录工具:SN_Write_tool_exe_v2.1420.00 首先介绍一下IMEI ...
- ThreadPoolExecutor运行机制
最近发现几起对ThreadPoolExecutor的误用,其中包括自己,发现都是因为没有仔细看注释和内部运转机制,想当然的揣测参数导致,先看一下新建一个ThreadPoolExecutor的构建参数: ...
- ZeroC Ice IceGrid Node和IceGrid
IceGrid Node介绍 绝大多数分布式系统都有一个共同特点,即分布在各个主机上的节点进程并不是完全独立的,而是彼此之间有相互联系和通信的.集群对集群中的节点有一些控制指令,如部署.启停或者调整某 ...