3

I have the following arrays:

example_array = [[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]]
indexes = [[1,2,3], [2,3], [0,4,2], [1,4,3], [0,4,3], [1,2,3], [1,2]]

I would like to replace the elements in the example_array with 1 according to the index in indexes. So, the expected result should be this:

example_array = [[0,1,1,1,0],[0,0,1,1,0],[1,0,1,0,1],[0,1,0,1,1],[1,0,0,1,1],[0,1,1,1,0],[0,1,1,0,0]].

I tried different approaches, for example to use pandas, like this:

matrix = pd.DataFrame(example_array)

for row in matrix:
    for i in indexes:
        for j in i:
            x_u.iloc[x, j] = 1

but it doesn't give me the hoped results! The solution does not need to use pandas library. Thanks for the help!

6 Answers 6

2

You can just do this:

example_array = [[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]]
indexes = [[1,2,3], [2,3], [0,4,2], [1,4,3], [0,4,3], [1,2,3], [1,2]]

for i,index in enumerate(indexes):
  for idx in index:
    example_array[i][idx] = 1
print example_array

Output:

[[0, 1, 1, 1, 0], [0, 0, 1, 1, 0], [1, 0, 1, 0, 1], [0, 1, 0, 1, 1], [1, 0, 0, 1, 1], [0, 1, 1, 1, 0], [0, 1, 1, 0, 0]]
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

example_array = [[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]]
matrix = pd.DataFrame(example_array)
indexes = [[1,2,3], [2,3], [0,4,2], [1,4,3], [0,4,3], [1,2,3], [1,2]]
for row_num in range(len(matrix)):
    matrix.iloc[row_num][indexes[row_num]] = 1

It also saves you the nested loop!

Comments

1

Using numpy array you can directly set specified indices to a desired value.

import numpy as np
example_array = np.array([[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]])
indexes = [[1,2,3], [2,3], [0,4,2], [1,4,3], [0,4,3], [1,2,3], [1,2]]

for i, ind in enumerate(indexes):
    example_array[i, ind] = 1
print(a)

Output

[[0 1 1 1 0]
 [0 0 1 1 0]
 [1 0 1 0 1]
 [0 1 0 1 1]
 [1 0 0 1 1]
 [0 1 1 1 0]
 [0 1 1 0 0]]

Comments

1
example_array = [[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]];
indexes = [[1,2,3], [2,3], [0,4,2], [1,4,3], [0,4,3], [1,2,3], [1,2]];

for i in range(0, len(example_array)):
    for j in range(0, len(indexes[i])):
        example_array[i][indexes[i][j]] = 1;

print example_array;

Comments

1

Or you can use the comprehension:

result = [[1 if i in ind else e for i, e in enumerate(ea)] for ea, ind in zip(example_array, indexes)]

Comments

1

with a one liner:

indices = [[1, 2, 3], [2, 3], [0, 4, 2], [1, 4, 3], [0, 4, 3], [1, 2, 3], [1, 2]]
print([[1 if idx in sub else 0 for idx in range(5)] for sub in indices])

output:

[[0, 1, 1, 1, 0], [0, 0, 1, 1, 0], [1, 0, 1, 0, 1], [0, 1, 0, 1, 1], [1, 0, 0, 1, 1], [0, 1, 1, 1, 0], [0, 1, 1, 0, 0]]

Comments

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.