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 8096631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:30:49+00:00 2026-06-05T21:30:49+00:00

lets say i have one array a = numpy.arange(8*6*3).reshape((8, 6, 3)) #and another: l

  • 0

lets say i have one array

    a = numpy.arange(8*6*3).reshape((8, 6, 3))
    #and another:
    l = numpy.array([[0,0],[0,1],[1,1]]) #an array of indexes to array "a"
    #and yet another:
    b = numpy.array([[0,0,5],[0,1,0],[1,1,3]])

where “l” and “b” are of equal length,
and i want to say

    a[l] = b

such that a[0][0] becomes [0,0,5], a[0][1] becomes [0,1,0] etc.

it seems to work fine when ive got one-dimensional arrays, but it gives me the error

    ValueError: array is not broadcastable to correct shape

when i try it with a 3-dimensional array.

  • 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-06-05T21:30:50+00:00Added an answer on June 5, 2026 at 9:30 pm
    import numpy as np
    
    a = np.arange(8*6*3).reshape((8, 6, 3))
    l = np.array([[0,0],[0,1],[1,1]]) #an array of indexes to array "a"
    b = np.array([[0,0,5],[0,1,0],[1,1,3]])
    
    a[tuple(l.T)] = b
    print(a[0,0])
    # [0 0 5]
    
    print(a[0,1])
    # [0 1 0]
    
    print(a[1,1])
    # [1 1 3]
    

    Anne Archibald says,

    When you are supplying arrays in all index slots, what you get back
    has the same shape as the arrays you put in; so if you supply
    one-dimensional lists, like

    A[[1,2,3],[1,4,5],[7,6,2]]

    what you get is

    [A[1,1,7], A[2,4,6], A[3,5,2]]

    When you compare that with your example, you see that

    a[l] = b tells NumPy to set

    a[0,0,1] = [0,0,5]
    a[0,1,1] = [0,1,0]
    

    and leaves the third element of b unassigned. This is why you get the error

    ValueError: array is not broadcastable to correct shape
    

    The solution is to transpose the array l into the correct shape:

    In [50]: tuple(l.T)
    Out[50]: (array([0, 0, 1]), array([0, 1, 1]))
    

    (You could also use zip(*l), but tuple(l.T) is a bit quicker.)

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

Sidebar

Related Questions

Lets say a have a string such as this one: string txt = Lore
What I want to do is following. Lets say we have array like that
Lets say I have an array of structs and I want to delete an
Lets say I have an array $aSomeArray = array(1,2,3,4,5,6,7,8,9,10,11,12); Now displaying one array value
Lets say I have an array of bad words: $badwords = array(one, two, three);
Okay, this one is pretty complicated... lets say I have an array called $data
Lets say I have one table called REVIEWS This table has Reviews that customers
Lets say I have one row with three columns - some buttons on left
Lets say that I have one component which is doing something with Workbook object
Lets say I have data in two tables. In one I have Order ID

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.