Advertisement
furas

Python - delete rows and cols - (Stackoverflow)

Sep 5th, 2022
964
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.03 KB | None | 0 0
  1. # [python - Combining and slicing tables - Stack Overflow](https://stackoverflow.com/questions/73614101/combining-and-slicing-tables)
  2.  
  3. def func(table, row, col):
  4.     """
  5.    Returns a copy of the table, removing the given row and column.
  6.      
  7.    Examples:
  8.        func([[1,3,5],[6,2,7],[5,8,4]],1,2) returns [[1,3],[5,8]]
  9.    
  10.    Parameter table: the nested list to process
  11.    Precondition: table is a table of numbers of the same length.
  12.  
  13.    Parameter row: the row to remove
  14.    Precondition: row is an index (int) for a row of table
  15.    
  16.    Parameter col: the colummn to remove
  17.    Precondition: col is an index (int) for a column of table
  18.    """
  19.     numrows = len(table)
  20.     numcols = len(table[0])
  21.  
  22.     result = []
  23.     for m in range(numrows):
  24.         if m != row:
  25.            
  26.             rows = []
  27.             for n in range(numcols):
  28.                 if n != col:
  29.                     rows.append(table[m][n])
  30.             result.append(rows)
  31.    
  32.     return result
  33.  
  34. print(func([[1,3,5], [6,2,7], [5,8,4]],1,2))
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement