MLinExpr.__getitem__()

__getitem__ ( )

Index or slice this MLinExpr.

Return value:

An MLinExpr object.

Example usage:

  mle = 2 * m.addMVar((2,2))
  col0 = mle[:, 0]  # The first column of mle, 1-D result
  elmt = mle[1, 0]  # The element at position (1, 0), 0-D result

You can index and slice MLinExpr objects like you would index NumPy's ndarray, and indexing behavior is straightforward to understand if you only read from the returned object. When you write to the returned object, be aware that some kinds of indexing return NumPy views on the indexed expression (e.g., slices), while others result in copies being returned (e.g., fancy indexing). Here is an example:

Example usage:

  mle = 2 * m.addMVar(4)
  leading_part_1 = mle[:2]
  leading_part_2 = mle[[0,1]]
  leading_part_1 += 99  # This modifies mle, too
  leading_part_2 += 1  # This doesn't modify mle

If you are unsure about any of these concepts and want to avoid any risk of accidentally writing back to the indexed object, you should always combine indexing with the copy method.

Example usage:

  expr = 2 * model.addMVar((2,2)) + 1
  first_col = expr[:, 0].copy()
  first_col =+ 1  # Leaves expr untouched

Try Gurobi for Free

Choose the evaluation license that fits you best, and start working with our Expert Team for technical guidance and support.

Evaluation License
Get a free, full-featured license of the Gurobi Optimizer to experience the performance, support, benchmarking and tuning services we provide as part of our product offering.
Academic License
Gurobi supports the teaching and use of optimization within academic institutions. We offer free, full-featured copies of Gurobi for use in class, and for research.
Cloud Trial

Request free trial hours, so you can see how quickly and easily a model can be solved on the cloud.

Search