I have a matrix [['1', '2'], ['3', '4']] which I want to convert to a matrix of integers. Is there is a way to do it using comprehensions?
            Asked
            
        
        
            Active
            
        
            Viewed 109 times
        
    0
            
            
        - 
                    Try searching. You'll find numerous such examples. – devnull Apr 24 '14 at 02:37
- 
                    Have you tried anything?? Here is a link which might be helpful http://stackoverflow.com/questions/20639180/python-list-comprehension-explained – Narendra Apr 24 '14 at 03:05
4 Answers
2
            
            
        In the general case:
int_matrix = [[int(column) for column in row] for row in matrix]
 
    
    
        aruisdante
        
- 8,875
- 2
- 30
- 37
1
            
            
        You could do it like:
>>> test = [['1', '2'], ['3', '4']]
>>> [[int(itemInner) for itemInner in itemOuter] for itemOuter in test]
[[1, 2], [3, 4]]
As long as all the items are integer, the code could work.
Hope it be helpful!
 
    
    
        Sheng
        
- 3,467
- 1
- 17
- 21
 
     
    