Skip to content
Advertisement

How to define an array without defining its elements?

I want to optimize a problem in Python using scipy.

To achieve this goal:

  1. I define an array of a certain length, but without defining its elements explicitly.

  2. Then, I need to use this array to optimize the problem.

  3. In the last step, I want to print this array as a result.

Here is the simple example:

import numpy as np
from scipy.optimize import minimize

b0 = [1,3,4,2,5]
a = [] # This is the array I want to define. The length of the a should be equal to the b.

def objective(b):
    print(a*b)
    z=a.b.(-1)
    return z 

def constraint(b)
    return b[0]*b[1]*b[2]*b[3]*b[4]

print(objective(b0))

Advertisement

Answer

Python has following syntax:

array_name = [some_value]*length

it creates an array filled with some_value with provided length

so try:

a = [None]*len(b)

or if you want to not change the type:

a=[0]*len(b)

as an alternative use numPy array

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement