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

时间:2018-12-15
本文章向大家介绍【面试】Python字符切割,replace+split,主要包括【面试】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']