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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:10:31+00:00 2026-06-11T07:10:31+00:00

Desired Output I want a function to return a list such that, given a

  • 0

Desired Output

I want a function to return a list such that, given a “jumbled” list l, each element is the index of the corresponding element of l, if l was sorted. (I’m failing to think of a less convoluted way of saying this, sorry.)

Examples

f([3,1,2]) = [2,0,1]

f([3,1,2,2,3]) = [3,0,1,2,4], since the input sorted is [1,2,2,3,3].

(This is useful for some stats calculations.)

My Attempt

I came up with a way to do this function, but this is python– it seems like there should be a one-liner to do this, or at least a much cleaner, clearer way.

def getIndiciesInSorted(l):
    sortedL = sorted(l)
    outputList = []
    for num in l:
        sortedIndex = sortedL.index(num)
        outputList.append(sortedIndex)
        sortedL[sortedIndex] = None
    return outputList

l=[3,1,2,2,3] 
print getIndiciesInSorted(l)

So, how can I write this more concisely? Is there a legible list comprehension solution?

  • 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-11T07:10:33+00:00Added an answer on June 11, 2026 at 7:10 am
    def argsort(seq):
        # http://stackoverflow.com/questions/3382352/3382369#3382369
        # http://stackoverflow.com/questions/3071415/3071441#3071441
        '''
        >>> seq=[1,3,0,4,2]
        >>> index=argsort(seq)
        [2, 0, 4, 1, 3]
    
        Given seq and the index, you can construct the sorted seq:
        >>> sorted_seq=[seq[x] for x in index]
        >>> assert sorted_seq == sorted(seq)
    
        Given the sorted seq and the index, you can reconstruct seq:
        >>> assert [sorted_seq[x] for x in argsort(index)] == seq
        '''
        return sorted(range(len(seq)), key=seq.__getitem__)
    
    def f(seq):
        idx = argsort(seq)
        return argsort(idx)
    
    print(f([3,1,2]))
    # [2, 0, 1]
    
    print(f([3,1,2,2,3]))
    # [3, 0, 1, 2, 4]
    

    Note that nightcracker’s function is faster:

    def get_sorted_indices(l):
        sorted_positions = sorted(range(len(l)), key=l.__getitem__)
        result = [None for _ in range(len(l))]
        for new_index, old_index in enumerate(sorted_positions):
            result[old_index] = new_index
        return result
    

    The difference may be significant for long lists:

    In [83]: import random
    In [98]: l = [random.randrange(100) for _ in range(10000)]
    In [104]: timeit get_sorted_indices(l)
    100 loops, best of 3: 4.73 ms per loop
    
    In [105]: timeit f(l)
    100 loops, best of 3: 6.64 ms per loop
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building this function but I'm not getting the desired output, it's been more
For example Input <url wiki=https://wiki.archlinux.org/index.php/Main_Page forums=https://bbs.archlinux.org/> Desired output <url wiki=https://wiki.archlinux.org/index.php/Main_Page forums=https://bbs.archlinux.org/> Here's a sample
Desired Outcome I want to have Vertical List with custom items on the left/right
Let's say I have a list of elements @list=(1,2,3); #desired output 1,2,3 and I
I want to write a function that outputs something to a ostream that's passed
Given a char[] in c, I want to specify the length of output from
I have a table MissingData 1 10 NULL NULL 22 NULL The desired output
Sample input string: char *str = 12345.567675; And the desired output if I need
MY_TABLE = Table with 2 columns Number, City. Desired Output = City and count
Input: I have a LaTeX file, with plain text & math formulas. Desired output:

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.