1

I am trying to achieve the following:

    # Before
    raw = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

    # Set values to 10
    indice_set1 = np.array([0, 2, 4])
    indice_set2 = np.array([0, 1])
    raw[indice_set1][indice_set2] = 10

    # Result
    print(raw)
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

But the raw values remain exactly the same.

Expecting this:

    # After
    raw = np.array([10, 1, 10, 3, 4, 5, 6, 7, 8, 9])
2
  • by doing raw[indice_set1] you generate a new array, which is the one you modify with the second slicing Commented Dec 15, 2022 at 13:55
  • raw[indice_set1] = 10 would change the original one to -> array([10, 1, 10, 3, 10, 5, 6, 7, 8, 9]) How can I add second layer of indexing? Commented Dec 15, 2022 at 13:58

1 Answer 1

3

After doing raw[indice_set1] you get a new array, which is the one you modify with the second slicing, not raw.

Instead, slice the slicer:

raw[indice_set1[indice_set2]] = 10

Modified raw:

array([10,  1, 10,  3,  4,  5,  6,  7,  8,  9])
Sign up to request clarification or add additional context in comments.

3 Comments

Fantastic, you are better than chatGPT.
@arapEST given all the very bad chatGPT answers that have flooded the site lately, I hope so :p
By the way that example was copied straight out of the chatGPT convo :) But, on some other cases I've got a lot of useful correct solutions from that bot.

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.