#字符串常用方法
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_字符串查找与分隔的更多相关文章

  1. Rabin-Karp指纹字符串查找算法

    首先计算模式字符串的散列函数, 如果找到一个和模式字符串散列值相同的子字符串, 那么继续验证两者是否匹配. 这个过程等价于将模式保存在一个散列表中, 然后在文本中的所有子字符串查找. 但不需要为散列表 ...

  2. 自己动手写文件查找,字符串查找,查询jar包等工具

    文件查找——搜索当前目录下的文件 知道大概的文件名称,使用 findf FileName findf.py import argparse, re, os from os.path import jo ...

  3. 关于字符串查找 charindex ,Patindex 还有一个like

    字符串查找.在模糊朝找的情况下,其实3者的效率是差不多的.都需要一个一个取出来然后扫一遍╮(╯_╰)╭.然而用法还是会有一点儿的区别 1 charindex (查找的字符串,字符串表达式[,开始查找的 ...

  4. python 字符串查找

    python 字符串查找有4个方法,1 find,2 index方法,3 rfind方法,4 rindex方法. 1 find()方法: )##从下标1开始,查找在字符串里第一个出现的子串:返回结果3 ...

  5. Sunday算法(字符串查找、匹配)

    字符串查找算法中,最著名的两个是KMP算法(Knuth-Morris-Pratt)和BM算法(Boyer-Moore).两个算法在最坏情况下均具有线性的查找时间.但是在实用上,KMP算法并不比最简单的 ...

  6. lintcode:strStr 字符串查找

    题目: 字符串查找 字符串查找(又称查找子字符串),是字符串操作中一个很有用的函数.你的任务是实现这个函数. 对于一个给定的 source 字符串和一个 target 字符串,你应该在 source ...

  7. Rabin-Karp字符串查找算法

    1.简介 暴力字符串匹配(brute force string matching)是子串匹配算法中最基本的一种,它确实有自己的优点,比如它并不需要对文本(text)或模式串(pattern)进行预处理 ...

  8. php中常用的字符串查找函数strstr()、strpos()实例解释

    string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] ) 1.$haystack被查找的字 ...

  9. 数据结构与算法--Boyer-Moore和Rabin-Karp子字符串查找

    数据结构与算法--Boyer-Moore和Rabin-Karp子字符串查找 Boyer-Moore字符串查找算法 注意,<算法4>上将这个版本的实现称为Broyer-Moore算法,我看了 ...

随机推荐

  1. css文本样式-css学习之旅(4)

    color:颜色derction:方向:line-height:行高:letter-spaceing:字符间距:text-align:对齐方向:text-decoration:装饰:text-inde ...

  2. Python学习笔记 - 列表生成式listComprehensions

    #!/usr/bin/env python3 # -*- coding: utf-8 -*- list(range(1, 11)) # 生成1乘1,2乘2...10乘10 L = [] for x i ...

  3. 三消游戏FSM状态机设计图

    三消游戏FSM状态机设计图 1) 设计FSM图 2) smc配置文件 ///////////////////////////////////////////////////////////////// ...

  4. LeetCode之“动态规划”:Unique Binary Search Trees && Unique Binary Search Trees II

    1. Unique Binary Search Trees 题目链接 题目要求: Given n, how many structurally unique BST's (binary search ...

  5. 高通 android平台LCD驱动分析

    目前手机芯片厂家提供的源码里包含整个LCD驱动框架,一般厂家会定义一个xxx_fb.c的源文件,注册一个平台设备和平台驱动,在驱动的probe函数中来调用register_framebuffer(), ...

  6. ZooKeeper 会话超时

    1.会话概述 在ZooKeeper中,客户端和服务端建立连接后,会话随之建立,生成一个全局唯一的会话ID(Session ID).服务器和客户端之间维持的是一个长连接,在SESSION_TIMEOUT ...

  7. 关于NSString和NSMutableString的相关用法和基本介绍

    Objective-C 中核心处理字符串的类是 NSString 与 NSMutableString ,这两个类最大的区别就是NSString 创建赋值以后该字符串的内容与长度不能在动态的更改,除非重 ...

  8. LeetCode(62)-Two Sum

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  9. jquery 加法 乘法运算 精确计算函数

    int类型相加不会出现问题,但小数点相加就会出现问题 //乘法函数 var accMul = function(arg1, arg2){ var m=0,s1=arg1.toString(),s2=a ...

  10. 【大前端攻城狮之路】JavaScript函数式编程

    转眼之间已入五月,自己毕业也马上有三年了.大学计算机系的同学大多都在北京混迹,大家为了升职加薪,娶媳妇买房,熬夜加班跟上线,出差pk脑残客户.同学聚会时有不少兄弟已经体重飙升,开始关注13号地铁线上铺 ...