0

I am trying to create several arrays from a big array that I have. What I mean is:

data = [[0, 1, 0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 1, 0, 0, 1, 0, 0, 0, 0], 
[0, 1, 1, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 1, 0, 0, 0, 0, 1],
[0, 0, 1, 1, 0, 0, 0, 0, 0,1], [0, 0, 0, 0, 1, 1, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 1, 0, 0], [0, 1, 0, 0, 0, 1, 0, 0, 0, 0], 
[0, 0, 0, 0, 1, 0, 0, 0, 1, 0]]  

I want to create 10 different arrays - using the 10 data's columns - with different names.

data1 = [0, 0, 0, 1, 0, 0, 1, 0, 0],
data2 = [1, 0, 1, 0, 0, 0, 0, 1, 0], and so on

I found a close solution here - Also I take the example data from there - However, when I tried the solution suggested:

for d in xrange(0,9):
exec 'x%s = data[:,%s]' %(d,d-1)

A error message appears:

exec(code_obj, self.user_global_ns, self.user_ns)

  File "", line 2, in 
    exec ('x%s = data[:,%s]') %(d,d-1)

  File "", line 1
    x%s = data[:,%s]
                 ^
SyntaxError: invalid syntax

Please, any comments will be highly appreciated. Regards

2
  • 3
    Don't. Just don't. Dynamically creating variables looks fancy but leads to unreadable and unmaintainable code. Use dictionaries or lists instead. Commented May 27, 2020 at 14:55
  • There really isn't a need to even use dictioary. Use numpy array indexing. Commented May 27, 2020 at 14:57

4 Answers 4

1

Use numpy array index:

data = [[0, 1, 0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 1, 0, 0, 1, 0, 0, 0, 0], 
[0, 1, 1, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 1, 0, 0, 0, 0, 1],
[0, 0, 1, 1, 0, 0, 0, 0, 0,1], [0, 0, 0, 0, 1, 1, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 1, 0, 0], [0, 1, 0, 0, 0, 1, 0, 0, 0, 0], 
[0, 0, 0, 0, 1, 0, 0, 0, 1, 0]]

d = np.array(data)

d[:, 0]
#array([0, 0, 0, 1, 0, 0, 1, 0, 0])

d[:, 1]
#array([1, 0, 1, 0, 0, 0, 0, 1, 0])

etc...

d[:, 9]
#array([0, 0, 1, 1, 1, 0, 0, 0, 0])

If you must, then dictionaries are the way to go:

val = {i:d[:,i] for i in range(d.shape[1])}

To access the arrays:

val[0]
#array([0, 0, 0, 1, 0, 0, 1, 0, 0])

...

val[9] 
#array([0, 0, 1, 1, 1, 0, 0, 0, 0])
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the suggestion, however, I would like to automotive these outputs in several arrays with different names each. Regards
@AlíMeresVargas Okay then, dictionaries is the way to go.
I believe that there is a numpy way to do it. Hope to find it soon.
0

Use the following code (it is also more readable -- for python 3.x) if you really want to create dynamic variables:

for d in range(0,9):
  # exec 'x%s = data[:,%s]' %(d,d-1)
  exec(f"data{d} = {data[d]}" )

1 Comment

Hi! I just checked the answer and works fine in case I change the exec for print. When I run with exec this message appears: exec(f"data{d} = {data[:,d]}") File "<string>", line 1 data0 = [0 0 0 1 0 0 1 0 0] ^ SyntaxError: invalid syntax I would like to design this function in order to use data1 and data2, for example, later in other analysis. Is that possible? Regards
0

Either use numpy array as shown by scott boston above or use dictionary like this:

a = {}

for i in range(0,9):
    a[i] = data[i][:]

Output:

{0: [0, 1, 0, 0, 0, 0, 0, 1, 0, 0],
 1: [0, 0, 1, 0, 0, 1, 0, 0, 0, 0],
 2: [0, 1, 1, 0, 0, 0, 0, 0, 0, 1],...

Comments

0
  1. I don't see the proper indentation in your for loop.

  2. I suggest you don't use %s for the second argument (string) but rather %d (number) since you need a number to do the indexing of your array.

2 Comments

Please add comment in comments only.
I am providing my answer to the problem, that's why it's not a comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.