0

I need to create something like:

x1 = [1 1 1]
x2 = [2 2 2]
   .
   .
   .
xn = [n n n]

and for this, I was thinking of doing:

for i in range(whatever):
    xi = np.array([i, i, i])

but it doesn't work, obviously. pleasy help me!

0

4 Answers 4

1
x = [np.array([i, i, i]) for i in range(whatever)]

Now x is a nested list and your xi corresponds to x[i]

Sign up to request clarification or add additional context in comments.

1 Comment

omg the same thoughts :-) the same think. but you are faster than me.
1

you cant create names or data types or objects like that in python or any other language. use a list object, append to it and you are the person to know that each entry in the list is your "variable Xi":

Use list comprehension

X = [np.array([i, i, i]) for index in range(100)]

Comments

0

I think you cannot use dynamic variable names in python, but you still can have dynamic dictionary keys. You can use a dictionary to achieve something like this:

data = {}

for i in range(100):
    data['x'+str(i)] = np.array([i, i, i])

Or use the comprehension method

data = {'x{}'.format(i): np.array([i, i, i]) for i in range(100)}

Or using lists:

data = [np.array([i, i, i]) for i in range(100)]

Comments

-1

I highly recommend using a dict() or a list(), but what you're trying to do is fairly simple:

for i in range(whatever):
    locals()[f'x{i+1}'] = np.array([i, i, i])



Dictionary:

d = {f'x{i+1}':np.array([i, i, i]) for i in range(whatever)}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.