zoukankan      html  css  js  c++  java
  • 【面试】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']

  • 相关阅读:
    每周总结13
    每周总结12
    每周总结11
    每周总结10
    每周总结9
    启明5-1总结
    启明4-30团队进度博客
    期末总结
    每日日报2021.6.11
    每日日报2021.6.10
  • 原文地址:https://www.cnblogs.com/ptest/p/10122690.html
Copyright © 2011-2022 走看看