1. 程式人生 > >【面試】Python字符切割,replace+split

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

python bsp spa for ESS pytho can 使用 japan

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