Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6753403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:09:25+00:00 2026-05-26T13:09:25+00:00

import numpy as np a = np.zeros((3,2)) ind_row = np.array([0,1]) a[ind_row, 1]=3 now, a

  • 0
import numpy as np

a = np.zeros((3,2))
ind_row = np.array([0,1])    
a[ind_row, 1]=3

now, a is, as expected:

 [[ 0.  3.]
 [ 0.  3.]
 [ 0.  0.]]

I want to assign a value to a sub-array of a[ind_row, 1], and expect to be able to do this as follows:

a[ind_row, 1][1] = 5

However, this leaves a unchanged! Why should I expect this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:09:26+00:00Added an answer on May 26, 2026 at 1:09 pm

    The problem here is that advanced indexing creates a copy of the array, and only the copy is modified. (This is in contrast to basic indexing, which results in a view into the original data.)

    When directly assigning to an advanced slice

    a[ind_row, 1] = 3
    

    no copy is created, but when using

    a[ind_row, 1][1] = 5
    

    the a[ind_row, 1] part creates a copy, and the [1] part indexes into this temporary copy. The copy is indeed changed, but since you don’t hold any references to it, you can’t see the changes, and it is immediately garbage collected.

    This is analogous to slicing standard Python lists (which also creates copies):

    >>> a = range(5)
    >>> a[2:4] = -1, -2
    >>> a
    [0, 1, -1, -2, 4]
    >>> a[2:4][1] = -3
    >>> a
    [0, 1, -1, -2, 4]
    

    A solution to the problem for this simple case is obviously

    a[ind_row[1], 1] = 5
    

    More complex cases could also be rewritten in a similar way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created an array thusly: import numpy as np data = np.zeros( (512,512,3),
import numpy as np import numpy.ma as ma This operates as expected with one
What's wrong with this snippet of code? import numpy as np from scipy import
why do the following lines not work as I expect? import numpy as np
for example, there is a matrix: import numpy as np A = np.array([[ 8.
>>> import numpy as np >>> a = np.array(['zero', 'one', 'two', 'three']) >>> a[1]
How can I create a huge numpy array using pytables. I tried this but
import Image from numpy import zeros, asarray YUV = zeros((240, 320, 3), dtype='uint8') im
import numpy as np a = np.arange(1000000).reshape(1000,1000) print(a**2) With this code I get this
I am using matplotlib for doing this from mpl_toolkits.mplot3d import Axes3D import numpy as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.