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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:33:08+00:00 2026-05-25T18:33:08+00:00

I have an array like this numpy array dd= [[foo 0.567 0.611] [bar 0.469

  • 0

I have an array like this numpy array

dd= [[foo 0.567 0.611]
     [bar 0.469 0.479]
     [noo 0.220 0.269]
     [tar 0.480 0.508]
     [boo 0.324 0.324]]

How would one loop through array
selecting foo and getting 0.567 0.611 as floats as a singleton.
Then select bar and getting 0.469 0.479 as floats as a singleton …..

I could get vector the first elements as list by using

dv=  dd[:,1]

The ‘foo’, and ‘bar’ elements are not unknown variables, they can change.

How would I change if element is in position [1]?

[[0.567 foo2 0.611]
  [0.469 bar2 0.479]
  [0.220 noo2 0.269]
  [0.480 tar2 0.508]
  [0.324 boo2 0.324]]
  • 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-25T18:33:09+00:00Added an answer on May 25, 2026 at 6:33 pm

    You have put the NumPy tag on your Question, so i’ll assume you want NumPy syntax, which the answer before mine doesn’t use.

    If in fact you wish to use NumPy, then you likely don’t want the strings in your array, otherwise you will also have to represent your floats as strings.

    What you are looking for is the NumPy syntax to access elements of a 2D array by row (and exclude the first column).

    That syntax is:

    M[row_index,1:]        # selects all but 1st col from row given by 'row_index'
    

    W/r/t the second scenario in your Question–selecting non-adjacent columns:

    M[row_index,[0,2]]     # selects 1st & 3rd cols from row given by 'row_index'
    

    The small complication in your Question is just that you want to use a string for row_index, so it’s necessary to remove the strings (so you can create a 2D NumPy array of floats), replace them with numerical row indices and then create a look-up table to map the the strings with the numerical row indices:

    >>> import numpy as NP
    >>> # create a look-up table so you can remove the strings from your python nested list,
    >>> # which will allow you to represent your data as a 2D NumPy array with dtype=float
    >>> keys
          ['foo', 'bar', 'noo', 'tar', 'boo']
    >>> values    # 1D index array comprised of one float value for each unique string in 'keys'
          array([0., 1., 2., 3., 4.])
    >>> LuT = dict(zip(keys, values))
    
    >>> # add an index to data by inserting 'values' array as first column of the data matrix
    >>> A = NP.hstack((vals, A))
    >>> A
            NP.array([  [ 0., .567, .611],
                        [ 1., .469, .479],
                        [ 2., .22, .269],
                        [ 3., .48, .508],
                        [ 4., .324, .324] ])
    
    >>> # so now to look up an item, by 'key':
    >>> # write a small function to perform the look-ups:
    >>> def select_row(key):
            return A[LuT[key],1:]
    
    >>> select_row('foo')
          array([ 0.567,  0.611])
    
    >>> select_row('noo')
          array([ 0.22 ,  0.269])
    

    The second scenario in your Question: what if the index column changes?

    >>> # e.g., move index to column 1 (as in your Q)
    >>> A = NP.roll(A, 1, axis=1)
    >>> A
          array([[ 0.611,  1.   ,  0.567],
                 [ 0.479,  2.   ,  0.469],
                 [ 0.269,  3.   ,  0.22 ],
                 [ 0.508,  4.   ,  0.48 ],
                 [ 0.324,  5.   ,  0.324]])
    
    >>> # the original function is changed slightly, to select non-adjacent columns:
    >>> def select_row2(key):
            return A[LuT[key],[0,2]]
    
    >>> select_row2('foo')
            array([ 0.611,  0.567])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array like this numpy array dd =[[0.567 2 0.611] [0.469 1
When you have an array like this: int foo[3][2][2]; and you make: int *bar
I have a numpy.ndarray like this: array([[ 11.18033989, 0. ], [ 8.24621125, 3. ],
I have array like this: $path = array ( [0] => site\projects\terrace_and_balcony\mexico.jpg [1] =>
I have an array like this: object[] args and need to insert those args
I have an array like this: $sports = array( 'Softball - Counties', 'Softball -
I have an array like this: var arr1 = [a, b, c, d]; How
Lets say I have an array like this: string [] Filelist = ... I
Say I have an array like this: [white, red, blue, red, white, green, red,
For example i have an array like this: $test= array(0 => 412, 1 =>

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.