Note that there are some explanatory texts on larger screens.

plurals
  1. POOrder a list by all item's digits in Python
    primarykey
    data
    text
    <h1>I want to sort a list by each item's digit.</h1> <p>Example:</p> <pre><code>myCmpItem = '511' myList = ['111','222','333','444','555','123'] (some magic) mySortedList = ['111', '222', '333', '123', '444', '555'] </code></pre> <h1>How the algorithm should work:</h1> <ul> <li>Compare each digit of current item in myList with myCmpItem <ul> <li>For the first item in the list it would be like that:</li> <li>Difference between 5 and 1 is 4</li> <li>Difference between 1 and 1 is 0</li> <li>Difference between 1 and 1 is 0</li> <li>Difference between those two numbers is 4 (the sum of the digit comparison)</li> </ul></li> <li>Do the same for all other items</li> <li>Order the list by this calculated similarity</li> </ul> <p>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?</p> <h1>Further Limitations</h1> <ul> <li>In my example all items have a length of 3, in the real scenario they have a length of 25</li> <li>All items have the same length, len(myList[x])==25 is always true</li> <li>Items can be strings, ints, floats or whatever fits better to the algorithm</li> <li>There are only digits between 1 and 5</li> </ul> <h1>Background</h1> <p>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).</p> <p><em>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.</em></p> <h1>The Answer (thanks to all participants!)</h1> <p>@larsmans' answer (<a href="https://stackoverflow.com/a/10790714/511484">https://stackoverflow.com/a/10790714/511484</a>) 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 (<a href="https://stackoverflow.com/a/10791838/511484">https://stackoverflow.com/a/10791838/511484</a>) 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!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload