面试Python字符切割,replace+split

Posted ptest

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了面试Python字符切割,replace+split相关的知识,希望对你有一定的参考价值。

string = ‘i am a chinese boy,but she is a japanese girl,she is russia girl.please tell me that how do i choice?‘ 
‘and can we happyniess?can we happyniess?‘

# 1.直接替换不需要的符号,在使用精灵函数切割
print(string.replace(,,  ).replace(?,  ).replace(.,  ).split())

 

# 2.依次查找函数中不需要的符号,与列表对比后替换,在使用精灵函数切割
def st(text, list):
    for i in list:
        text = text.replace(i, ‘‘)
    print(text.split())


st(string, [,, ?, .])

结果:

[‘i‘, ‘am‘, ‘a‘, ‘chinese‘, ‘boybut‘, ‘she‘, ‘is‘, ‘a‘, ‘japanese‘, ‘girlshe‘, ‘is‘, ‘russia‘, ‘girlplease‘, ‘tell‘, ‘me‘, ‘that‘, ‘how‘, ‘do‘, ‘i‘, ‘choiceand‘, ‘can‘, ‘we‘, ‘happyniesscan‘, ‘we‘, ‘happyniess‘]




以上是关于面试Python字符切割,replace+split的主要内容,如果未能解决你的问题,请参考以下文章

Python基础2

python基础面试题整理---从零开始 每天十题(04)

.NET 实现 Split字符切割

python之字符串切割

Python用特殊符号切割字符串并生成list(简单)

python 切割回文字符串