【发布时间】:2016-10-16 10:33:54
【问题描述】:
我正在尝试将项目从一个列表压缩到另一个列表,我需要能够将标点符号保存为列表中的单独项目,因为如果我不这样做,“你”和“你;”在列表中保存为单独的项目。
例如原始列表是,
['Ask', 'not', 'what', 'your', 'country', 'can', 'do', 'for', 'you;', 'ask', 'what', 'you', 'can', 'do', 'for', 'your', 'country!', 'This', 'is', 'a', 'quote', 'from', 'JFK', 'who', 'is', 'a', 'former', 'American', 'President.']
压缩列表目前是,
['Ask', 'not', 'what', 'your', 'country', 'can', 'do', 'for', 'you;', 'ask', 'you', 'country!', 'This', 'is', 'a', 'quote', 'from', 'JFK', 'who', 'former', 'American', 'President.']
但我希望它在列表中具有标点符号作为单独的项目。
我的预期输出是,
['Ask', 'not', 'what', 'your', 'country', 'can', 'do', 'for', 'you', ';', 'ask', '!', 'This', 'is', 'a', 'quote', 'from', 'JFK', 'who', 'former', 'American', 'President', '.']
【问题讨论】:
-
请举一些例子。
-
你的预期输出是什么?
-
你的意思是
[re.sub(r'[:?.!]', '', s) for s in lst] -
对不起,我不知道该怎么办
标签: python arrays list arraylist compression