79511688

Date: 2025-03-15 19:51:53
Score: 0.5
Natty:
Report link

I just investigated some more myself and found this solution making use of the walrus operator:

import numpy as np

a = np.arange(10)

dim_a, dim_total = 2, 4

(shape := [1] * dim_total)[dim_a] = -1
np.reshape(a, shape)

I like that it's very compact, but the := is still not very commonly used.

Reasons:
  • Low length (0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
Posted by: Axel Donath