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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:55:07+00:00 2026-06-03T06:55:07+00:00

I am trying to slice the below array to get rows 2 and 3

  • 0

I am trying to slice the below array to get rows 2 and 3 and the first column in addition to the columns between the 2nd and last columns, but every slice I have tried does not seem to work. For example, the first print statement below gives a syntax error because of the : in the brackets. I have also tried to simply concatenate the arrays, but I don’t think this is the most efficient way to accomplish this problem.

import numpy as np
y = np.arange(35).reshape(5, 7)

# My ultimate goal is to do a slice similar to this expression, but this of course gives
# an error. 
print y[[1, 2], [0, 2:-1]]

# This works, but I feel it is inefficient, although I could be wrong.
print np.hstack((y[[1, 2], 0][:, np.newaxis], y[[1, 2], 2:-1]))

Any suggestions would be greatly appreciated.

  • 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-03T06:55:09+00:00Added an answer on June 3, 2026 at 6:55 am

    I don’t know if this is what you’re asking for but try

    In [11]: y[2:4,[1,3,4,5,6]]
    Out[11]: 
    array([[15, 17, 18, 19, 20],
           [22, 24, 25, 26, 27]])
    
    In [12]: 
    

    Numpy can be sliced similar to standard Python lists but the dimensions add some trickiness but I still find this solution to be really elegant compared to nesting or looping reshapes but sometimes this will not always be the end-all-be-all solution.

    Edit:

    It doesn’t look good but it’s better than a reshape or huge matrix changes

    This is the same as saying y[1:3, [0, 2:-1]] without having to reshape the array or iterate through excess elements, you specify the indexes you care about by making a list of [0] + the remaining columns in that dimension.

    In [33]: y[1:3, [0] + list(xrange(2,y.shape[1]))]
    Out[33]: 
    array([[ 7,  9, 10, 11, 12, 13],
           [14, 16, 17, 18, 19, 20]])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was trying to create an extension that could slice any array-like class (since
I am trying to deserialize this JSON array into my android project. [{Name:Ban,Price:1},{Name:Banana,Price:1},{Name:chicken,Price:14},{Name:pizza,Price:16},{Name:slice,Price:1}] I
I'm trying to get Rails to capitalize the first character of a string, and
basically I'm trying to get #toggle_box to slide down using Jquery, the code below
using the jquery below I'm trying to submit the array paramlist to the appropriate
I have the strings below and I am trying to remove the last directory
I am trying to create input text like below Image : First I used
I am new to Python. Here's what I am trying to do: Slice a
I am making an android app in which i am trying to slide between
I am trying to do a function after my first slide has shown. I

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.