1. Engineering
  2. Computer Science
  3. 1python 2dimensional list 1a write a python function copy that...

Question: 1python 2dimensional list 1a write a python function copy that...

Question details

1.Python 2-dimensional List:

1a. Write a Python function copy that takes a 2-dimensional list and returns a (deep) copy of it. First make sure you understand why the following code doesn’t work:

def copy(x):

return [x[i] for i in range(len(x))]

Use slicing to help make the copy.

1b. Write a Python function odd_cols that takes a rectangular 2-dimensional list and returns a copy of all the odd-index columns as a new 2-dimensional list. For example,

odd_cols([[3, 5, 2, 8, 9],

[1, 0, 3, 7, 2],

[4, 8, 9, 3, 6]])

should return

[[5, 8],

[0, 7],

[8, 3]]

1c. Write a Python function make_rect that takes a 2-dimensional list that’s not necessarily rectangular, and modifies it to be rectangular (with width equal to the maximum width of the original rows) by appending None values to the rows that are too short. For example,

x = [[0, 1],

[2, 3, 4, 5, 6],

[7, 8, 9]]

make_rect(x)

should not return anything (which is equivalent to returning None), but after the call, x should be

[[0, 1, None, None, None],

[2, 3, 4, 5, 6],

[7, 8, 9, None, None]]

Do not use Python’s built-in max function; find the maximum row length using a loop (as discussed in lecture). (You may wish to use the extend method of lists: if a and b are lists, then a.extend(b) appends a copy of all entries of b onto the end of a.)

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution