2012-11-27 21 views
1

我試圖找出如何做到這一點的一部分:改變一個單詞

>>>sentence = "There's something right there." 
>>>change_s(sentence) 
['there', 'his', 'something', 'right', 'there.'] 
>>>sentence = "it's Patrick's car." 
>>>change_s(sentence) 
['it', 'his', 'Patrick', 'his', 'car.'] 

所以基本上我想改變「的」給‘他’(即使它在語法上不正確)並把這些單詞放在一個列表中。

+0

你知道「it his」!=「it's」,對嗎? – jdotjdot

回答

1
In [6]: sentence = "There's something right there." 

In [7]: sentence.replace("'s", " his").split() 
Out[7]: ['There', 'his', 'something', 'right', 'there.'] 
+2

're.sub()'是一個*小*矯枉過正,如果你實際上沒有使用正則表達式... –

+0

@ IgnacioVazquez-Abrams:過去漫長的一天,感謝您的建議。 – NPE

+0

您編輯的解決方案現在與我的完全相同。 –

相關問題