从单词列表中删除/n-python
我有一个清单如下 [果冻]、[猫]、[是]、[黑]、[白]、[是]、[小]、[是]、[小]、[是]、[快乐]、[明亮]、[和]、[愉快]、[听]、[何时]、[他们]、[猫]、[快乐]、[脸]、[椭圆]、[猫]、[有]、[明亮]、[明亮]、[和]、[愉快]、[听]、[听]、[何时]、[他们]、[猫]、[猫]、[快乐]、[脸]、[脸]、[椭圆]、eir、airs、and、GRACE\and、wait、FORT、the、Jellicle、Moon、to、RUSE。\n'] 如何删除/n,这样我就可以得到一个列表,每个单词都是一个单独的东西,没有/n 语法可以留在列表中 谢谢最简单的方法(虽然不是最好的方法)可能是加入然后拆分:从单词列表中删除/n-python,python,list,string,Python,List,String,我有一个清单如下 [果冻]、[猫]、[是]、[黑]、[白]、[是]、[小]、[是]、[小]、[是]、[快乐]、[明亮]、[和]、[愉快]、[听]、[何时]、[他们]、[猫]、[快乐]、[脸]、[椭圆]、[猫]、[有]、[明亮]、[明亮]、[和]、[愉快]、[听]、[听]、[何时]、[他们]、[猫]、[猫]、[快乐]、[脸]、[脸]、[椭圆]、eir、airs、and、GRACE\and、wait、FORT、the、Jellicle、Moon、to、RUSE。\n'] 如何删除/n,这样我就可以得
l = ('\n'.join(l)).split('\n')
事实上,看起来您是通过在空格上拆分创建此列表的。如果是这样,您可能需要重新考虑如何首先创建此列表以避免此额外步骤。您可以通过在匹配空格的正则表达式上拆分来直接拆分到正确的结果,或者更好地使用s.split()
没有任何参数。事实上,如果您调用“foo bar\nbaz”.split(),您将立即得到正确的结果。
>>> [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.']
>>> 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()]