Python 2.7 : Remove elements from a multidimensional list
Question: Basically, I have a 3dimensional list (it is a list of tokens, where the first
dimension is for the text, second for the sentence, and third for the word).

Addressing an element in the list (lets call it mat) can be done for example:
mat[2][3][4]. That would give us the fifth word or the fourth sentence in the
third text.

But, some of the words are just symbols like '.' or ',' or '?'. I need to
remove all of them. I thought to do that with a procedure:

    
    
        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        
    

Now, when I try to use that:

    
    
        finalMat = removePunc(mat) 
    

it is giving me the same list (mat is a 3 dimensional list). My idea was to
iterate over the list and remove only the 'words' which are actually
punctuation symbols.

I don't know what I am doing wrong but surely there is a simple logical
mistake.

Edit: I need to keep the structure of the array. So, words of the same
sentence should still be in the same sentence (just without the 'punctuation
symbol' words). Example:

    
    
        a = [[['as', '.'], ['w', '?', '?']], [['asas', '23', '!'], ['h', ',', ',']]]
    

after the changes should be:

    
    
        a = [[['as'], ['w']], [['asas', '23'], ['h']]]
    

Thanks for reading and/or giving me a reply.


Answer: I would suspect that your data are not organized as you think they are. And
although I am usually not the one to propose regular expressions, I think in
your case they may be among the best solutions. I would also suggest that
instead of eliminating non-alphabetic characters from words, you process
sentences

    
    
    >>> 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']
    >>> 
    

