#๐Ÿ”’ simple 2d array help

6 messages ยท Page 1 of 1 (latest)

somber bridge
#

what is a quick way to switch all values in a colum to something else:

if I have the 2d array:
[[1,2,3],[4,5,6],[7,8,9]]

I want to switch all of the 2nd colum to it's value +1
[[1,3,3],[4,6,6],[7,9,9]]

winged tapirBOT
#

@somber bridge

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

split ember
#

For a list of lists of ints, a for loop works fine: ```py

x = [[1,2,3],[4,5,6],[7,8,9]]
for row in x:
... row[1] += 1
...
print(x)
[[1, 3, 3], [4, 6, 6], [7, 9, 9]]

For numpy, [dimensional indexing](https://numpy.org/doc/2.1/user/basics.indexing.html#dimensional-indexing-tools) plus [broadcasting](https://numpy.org/doc/2.1/user/basics.broadcasting.html#broadcasting) ```py
>>> x = np.arange(1, 10).reshape((3,3))
>>> print(x)
[[1 2 3]
 [4 5 6]
 [7 8 9]]
>>> x[:, 1] += 1
>>> print(x)
[[1 3 3]
 [4 6 6]
 [7 9 9]]
winged tapirBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.