## Python set a column in list of list 2D matrix

initialize 2d array python

python dynamic 2d array

python 2d array indexing

append 2d list python

python print 2d array

python multidimensional list

how to take 2d array input in python

So given two lists

y_new = ( 165, 152, 145, 174) pos_2D = ( (2,3), (32,52), (73,11), (43,97) )

I would like to so something like

pos_2D_new = setCol(2, y_new, pos_2D)

where column 2 is the Y coordinate.

pos_2D_new = ( (2,165), (32,152), (73,145), (43,174) )

How to set a 1D into a 2D tuple in Python?

You can do this with:

pos_2D_new = [ (x, y2) for (x, _), y2 in zip(pos_2D, y_new) ]

or if you want a tuple:

pos_2D_new = tuple((x, y2) for (x, __), y2 in zip(pos_2D, y_new))

We thus concurrently iterate over the `pos_2D`

and `ynew`

, and each time we construct a new tuple `(x, y2)`

.

The above is of course not very generic, we can make it more generic, and allow to specify what item to replace, like:

def replace_coord(d, old_pos, new_coord): return tuple(x[:d] + (y,) + x[d+1:] for x, y in zip(old_pos, new_coord))

So for the *x*-coordinate you can use `replace_coord(0, old_pos, new_x_coord)`

whereas for the *y*-coordinate it is `replace_coord(1, old_pos, new_y_coord)`

. This also works for coordinates in three or more dimensions.

Nested lists: creating. Suppose that two numbers are given: the number of rows of n and the number of columns m . You must create a In Python any table can be represented as a list of lists (a list, where each element is in turn a list). For example, here's the program that creates a numerical table with two rows and three columns, and then makes some manipulations with it:

You can use a generator expression with `zip`

:

pos_2D_new = tuple((x, y) for (x, _), y in zip(pos_2D, y_new))

With your sample input, `pos_2D_new`

would become:

((2, 165), (32, 152), (73, 145), (43, 174))

For example m = [[10, 20], [40, 50], [30, 60]] represents a matrix of 3 rows and 2 columns. First element of the list – m[0] and element in first row, first column – m[0][0 to extend this to a number of columns you could store the column number and it's value in a dict. So to set colum 5 to 10 and column 2 to 7: cols = 7 rows = 8 data = [ [0] * cols for i in range(rows)] valdict = {5:10, 2:7} for col, val in valdict.items(): for row in data: row[col] = val.

Which would give

def setCol(idx, coords_1d, coords_nd): # recalling that indexing starts from 0 idx -= 1 return [ c_nd[:idx] + (c_1d,) + c_nd[idx+1:] for (c_1d, c_nd) in zip(coords_1d, coords_nd) ]

and

>>> setCol(2, y_new, pos_2D) [(2, 165), (32, 152), (73, 145), (43, 174)]

Python provides many ways to create 2-dimensional lists/arrays. However one must know Using above first method to create a. # 2D array. rows, cols = ( 5 , 5 ). Select a copy of row at index 1 from 2D array and set all the elements in selected sub array to 100 Python # Select a copy of row at index 1 from 2D array row = nArr2D[1].copy() # Set all the elements in selected sub array to 100 row[:] = 100

Create a NumPy array ndarray from the original 2D list and get the transposed object with the T attribute. If you want a list type object, convert it to I want to extract a part of a two-dimensional list (=list of lists) in Python. I use Mathematica a lot, and there it is very convenient to write. matrix[[2;;4,10;;13]] which would extract the part of the matrix which is between the 2nd and 4th row as well as the 10th and 13th column. In Python, I just used

Create a list of lists, or a 2D list. Step 4: This loop can iterate rows and columns in the 2D list. Nested Python program that uses 2D list # Step 1: create a list. # . It is possible to index into a flat array as though it is two-dimensional. We add I have a 2D array in python modeled by a list of lists and I want to extract the column. I made a quick research and I found a way that uses numpy arrays. The problem is that I do not want to use numpy so I don't want to convert my list of lists into a numpy array and then use [:,1] syntax. I tried using it on a normal list of lists but it

Creating 2d Lists; Getting 2d List Dimensions; Copying and Aliasing 2d Lists Try, and FAIL, to create a variable-sized 2d list rows = 3 cols = 2 a = [ [0] * cols ] * rows len(str(a[row][col]))) return maxLen # Because Python prints 2d lists on one Python program that uses 2D list # Step 1: create a list. # Append empty lists in first two indexes. Append empty lists in first two indexes. elements = [] elements.append( [] ) elements.append( [] ) # Step 2: add elements to empty lists.

##### Comments

- So you replace the second part of the tuple with
`y_new`

? What happens with the "old" second item? - Yes... I am creating a new tuple, with the the old X, and a new Y.
- Thanks Willem! Amazing!... :)