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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:00:10+00:00 2026-06-06T13:00:10+00:00

This query is somewhat related to this earlier one on sorting where, it was

  • 0

This query is somewhat related to this earlier one on sorting

where, it was required to sort the following list,

data = [[1, .45, 0], [2, .49, 2], [3, .98, 0], [4, .82, 1], [5, .77, 1], [6, .98, 2] ]

first by the values of the last member of the inner list like this way,

[[1, .45, 0], [3, .98, 0],[4, .82, 1], [5, .77, 1], [2, .49, 2], [6, .98, 2]]

and then sort within the sub-lists i.e. first sort the list with its last member as '0' using the middle member as the key, in descending order, then the sub-list with last member as '1' and so on.

Now, instead of first sorting by the values of the last member, I would like like to sort based upon the order of these elements present in an external list. i.e. if the external list is List_1

`List_1 = [2, 0, 1]`

Sorting should produce

[[2, .49, 2], [6, .98, 2] [1, .45, 0], [3, .98, 0], [4, .82, 1], [5, .77, 1]]

Finally, sorting the sub-lists based upon the middle element in descending order should produce:

[ [6, .98, 2],[2, .49, 2],   [3, .98, 0], [1, .45, 0], [4, .82, 1], [5, .77, 1]]

Any suggestions on how to go about this ?

  • 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-06T13:00:12+00:00Added an answer on June 6, 2026 at 1:00 pm

    Use list.index in the key:

    >>> data = [[1, .45, 0], [2, .49, 2], [3, .98, 0], [4, .82, 1],
                [5, .77, 1], [6, .98, 2]]
    >>> List_1 = [2, 0, 1]
    >>> sorted(data, key=lambda e: (List_1.index(e[2]), -e[1]))
    [[6, 0.98, 2], [2, 0.49, 2], [3, 0.98, 0], [1, 0.45, 0],
     [4, 0.82, 1], [5, 0.77, 1]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found a couple of questions somewhat related to this: httpclient-request-set-attribute-question commons-httpclient-adding-query-string-parameters-to-get-post-request but I
If anyone read my last question, this is somewhat of a continuation of it.
Two questions somewhat related to each other: In a find / findAll query, is
I have a query that is somewhat equal to the following: SELECT @address =
I've got a MySQL query somewhat like the following: SELECT * FROM products LEFT
This is somewhat related to this question : I have a table with a
Trying to build a somewhat complicated WordPress query. In one query, I'm trying to:
I can't think through this one. I have this query: SELECT p.person_id, p.first_nm, p.last_nm,
Suppose I have the following objects (one table per object) with this relations: A
This question is somewhat related to What’s the simplest way to make a HTTP

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.