2010-08-29 37 views
2

我有一個列表如下去除/ N從單詞列表 - 蟒

[ '傑利克', '貓', '是', '黑', '和', '白色,\ nJellicle', 「貓」,「是」,「寧可」,「小」; \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ '聽','當','他們','caterwaul。\ nJellicle','貓','擁有','快樂','面孔,\'Jellicle','貓','擁有','明亮', '黑','眼睛; \ n他們','像','到','練習','他們''''''和','恩斯\ n和','等待','爲',' \ n']

如何刪除/ n,最後以每個單詞的列表作爲單獨的單詞列表,並將其作爲單獨的/不包含/ ñ。

語法允許留在列表中。

感謝

回答

2

最簡單的(雖然不是最好的表演)很可能是加入再拆:

l = ('\n'.join(l)).split('\n') 

事實上,它看起來像你通過空間拆分創建這個列表。如果是這樣,您可能需要重新考慮如何創建此列表以避免這一額外步驟。通過使用s.split()而不帶任何參數,可以通過在匹配空白的正則表達式上進行拆分來直接拆分爲正確的結果。

+2

事實上,如果你叫 「富酒吧\ nbaz」 .split(),你會得到蝙蝠正確的事情。 – Ian 2010-08-29 11:05:20

+0

@Ian:+1好點。我會修改我的答案。 :) – 2010-08-29 11:06:24

1
>>> [i for el in lst for i in el.splitlines()] 
['Jellicle', 'Cats', 'are', 'black', 'and', 'white,', 'Jellicle', 'Cats', 'are', 'rather', 'small;', 'Jellicle', 'Cats', 'are', 'merry', 'and', 'bright,', 'And', 'pleasant', 'to', 'hear', 'when', 'they', 'caterwaul.', 'Jellicle', 'Cats', 'have', 'cheerful', 'faces,', 'Jellicle', 'Cats', 'have', 'bright', 'black', 'eyes;', 'They', 'like', 'to', 'practise', 'their', 'airs', 'and', 'graces', 'And', 'wait', 'for', 'the', 'Jellicle', 'Moon', 'to', 'rise.'] 
0
>>> l = ['Jellicle', 'Cats', 'are', 'black', 'and', 'white,\nJellicle', 'Cats', 'are', 'rather', 'small;\nJellicle', 'Cats', 'are', 'merry', 'and', 'bright,\nAnd', 'pleasant', 'to', 'hear', 'when', 'they', 'caterwaul.\nJellicle', 'Cats', 'have', 'cheerful', 'faces,\nJellicle', 'Cats', 'have', 'bright', 'black', 'eyes;\nThey', 'like', 'to', 'practise', 'their', 'airs', 'and', 'graces\nAnd', 'wait', 'for', 'the', 'Jellicle', 'Moon', 'to', 'rise.\n'] 
>>> [i.strip(',;') for v in l for i in v.split()]