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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:58:28+00:00 2026-06-04T15:58:28+00:00

I want to sort a list by each item’s digit. Example: myCmpItem = ‘511’

  • 0

I want to sort a list by each item’s digit.

Example:

myCmpItem = '511'
myList = ['111','222','333','444','555','123']

(some magic)

mySortedList = ['111', '222', '333', '123', '444', '555']

How the algorithm should work:

  • Compare each digit of current item in myList with myCmpItem
    • For the first item in the list it would be like that:
    • Difference between 5 and 1 is 4
    • Difference between 1 and 1 is 0
    • Difference between 1 and 1 is 0
    • Difference between those two numbers is 4 (the sum of the digit comparison)
  • Do the same for all other items
  • Order the list by this calculated similarity

I could code this with alot of for-loops, but I am actually looking for a faster way to do this. Is there any algorithm that does something like that? Fast?

Further Limitations

  • In my example all items have a length of 3, in the real scenario they have a length of 25
  • All items have the same length, len(myList[x])==25 is always true
  • Items can be strings, ints, floats or whatever fits better to the algorithm
  • There are only digits between 1 and 5

Background

All item’s digits are answers to questions and I want to find the most similar answer-set to a given answer-set. So “123” means that a user answered to Questions 1 = Answer 1, Question 2 = Answer 2, Question 3 = Answer 3. They are multiple choice questions with 25 questions in total (= length of 25) and there are always 5 different possibilites to answer (Those are the digits 1-5).

PS: This is the first question I asked on Stackoverflow so please be kind with me. I already googled for hours but I could not find any solution, so I asked here. I hope that is fine. Also english is not my native language.

The Answer (thanks to all participants!)

@larsmans’ answer (https://stackoverflow.com/a/10790714/511484) explains very well how to solve this with reasonable speed. You can even speed up the algorithm by calculating the distances between every digit in advance, see @gnibbler’s post (https://stackoverflow.com/a/10791838/511484) All the other answers were also nice and correct, but I found that @larsmans had the best explanation. Thanks everybody once again for the help!

  • 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-04T15:58:29+00:00Added an answer on June 4, 2026 at 3:58 pm

    First, make a list of integers from myCmpItem to make subtraction possible.

    myCmpItem = map(int, myCmpItem)
    

    Then, define a function that calculates the distance between an item and myCmpItem. We need to map the items to lists of integers as well. The rest is just the vanilla formula for L1 distance (the mathematical name of the “difference” you’re computing).

    def dist(item):
        item = map(int, item)
        return sum(abs(item[i] - myCmpItem[i]) for i in xrange(len(item)))
    

    Then, use this function as a key function for sorting.

    sorted(myList, key=dist)
    

    (PS: are you sure L1 distance makes sense for this application? Using it expresses the assumption that answer 1 is more similar to answer 2 than to answer 3, etc. If that’s not the case, Hamming distance might be more appropriate.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to sort a dictionary of lists, by third item in each list.
i want to sort dictionary based upon value of each dictioanry item.but if i
I have an unordered list of items, I want to display each item as
I'm looking on how to sort a list effectively using weighted values. Each item
I want to sort a list of strings based on the string length. I
I have List I want to sort Desc by Priority, which is int and
I have a list of artists, albums and tracks that I want to sort
I have an array containing a list of files. I want to sort it
I have a list of divs that I want to be able to sort.
I want to sort items where the comparison is performed by humans: Pictures Priority

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.