基本上,我有一个三维列表(它是一个标记列表,其中第一个维度用于文本,第二个维度用于句子,第三个维度用于单词)。
寻址列表中的元素(我们称之为 mat)可以这样完成: 垫[2][3][4]。这将为我们提供第三个文本中的第五个单词或第四个句子。
但是,有些单词只是像“.”这样的符号。或“,”或“?”。我需要将它们全部删除。我想用一个程序来做到这一点:
def removePunc(mat):
newMat = []
newText = []
newSentence = []
for text in mat:
for sentence in text:
for word in sentence:
if word not in " !@#$%^&*()-_+={}[]|\\:;'<>?,./\"":
newSentence.append(word)
newText.append(newSentence)
newMat.append(newText)
return newMat
现在,当我尝试使用它时:
finalMat = removePunc(mat)
它给了我相同的列表(mat 是一个 3 维列表)。我的想法是迭代列表并仅删除实际上是标点符号的“单词”。
我不知道我做错了什么,但肯定有一个简单的逻辑错误。
编辑:我需要保留数组的结构。因此,同一个句子的单词应该仍然在同一个句子中(只是没有“标点符号”单词)。示例:
a = [[['as', '.'], ['w', '?', '?']], [['asas', '23', '!'], ['h', ',', ',']]]
更改后应该是:
a = [[['as'], ['w']], [['asas', '23'], ['h']]]
感谢您阅读和/或给我回复。
最佳答案
我怀疑您的数据没有按照您想象的方式组织。尽管我通常不是提出正则表达式的人,但我认为在您的情况下它们可能是最好的解决方案之一。 我还建议您处理句子,而不是从单词中消除非字母字符
>>> import re
>>> non_word = re.compile(r'\W+') # If your sentences may
>>> sentence = '''The formatting sucks, but the only change that I've made to your code was shortening the "symbols" string to one character. The only issue that I can identify is either with the "symbols" string (though it looks like all chars in it are properly escaped) that you used, or the punctuation is not actually separate words'''
>>> words = re.split(non_word, sentence)
>>> words
['The', 'formatting', 'sucks', 'but', 'the', 'only', 'change', 'that', 'I', 've', 'made', 'to', 'your', 'code', 'was', 'shortening', 'the', 'symbols', 'string', 'to', 'one', 'character', 'The', 'only', 'issue', 'that', 'I', 'can', 'identify', 'is', 'either', 'with', 'the', 'symbols', 'string', 'though', 'it', 'looks', 'like', 'all', 'chars', 'in', 'it', 'are', 'properly', 'escaped', 'that', 'you', 'used', 'or', 'the', 'punctuation', 'is', 'not', 'actually', 'separate', 'words']
>>>
关于list - Python 2.7 : Remove elements from a multidimensional list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28761590/