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

  • Home
  • SEARCH
  • 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 7672305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:15:18+00:00 2026-05-31T16:15:18+00:00

I want to extract out some specified columns from the matrix. My matrix being

  • 0

I want to extract out some specified columns from the matrix. My matrix being

matrix=[[1,2,3,4,5],
        [6,7,8,9,10],
        [6,4,3,1,2],
        [2,3,4,5,6]]
expected result: [[3,4],[8,9],[3,1],[4,5]] for 2 columns
expected result in case of 3 column:[[1,2,3],[6,7,8],[6,4,3],[2,3,4]]

I am trying to use the method given below:

def func(matrix, p):
     return np.vstack([a[i] for a in matrix])

The above method just returns to be a single column, however I want to write a method which takes in multiple columns as input (e.g. the expected results for 2 & 3 columns), also my input number of columns vary each time. Please suggest a suitable method to extract these columns in python.

  • 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-31T16:15:19+00:00Added an answer on May 31, 2026 at 4:15 pm

    Using pure Python, you can extract columns 1, 5 and 7 by using the following nested list comprehension:

    [[a[i] for i in (1, 5, 7)] for a in matrix]
    

    Example usage for your input:

    >>> [[a[i] for i in (0, 1, 2)] for a in matrix]
    [[1, 2, 3], [6, 7, 8], [6, 4, 3], [2, 3, 4]]
    

    In cases like this where you want to extract a real slice of columns, you can also use list slicing:

    >>> [a[0:3] for a in matrix]
    [[1, 2, 3], [6, 7, 8], [6, 4, 3], [2, 3, 4]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to extract the store/brand names out of the url or some title
I am working with XPATH, Java and want to extract some text out of
I have a object[] containig some values. I want to extract infomation out of
I want to extract some hourly data from rrdtool databases in order to create
I have a string (char) and I want to extract numbers out of it.
I want to extract Urdu phrases out of a user-submitted string in PHP. For
I just want a very handy way to extract the numbers out of a
Given 2 html sources, I want to first extract the main content out of
I want extract the path of images from a html page using PHP-preg_match_all(), the
I want to extract from a webpage all URLs how can I do that

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.