码迷,mamicode.com
首页 > 编程语言 > 详细

【面试】Python字符切割,replace+split

时间:2018-12-15 13:46:53      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:ase   col   pytho   nes   hat   ring   lsh   split   ace   

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

标签:ase   col   pytho   nes   hat   ring   lsh   split   ace   

原文地址:https://www.cnblogs.com/ptest/p/10122690.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!