I want to sort the list by each item.
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 the current item in myList with myCmpItem
- For the first item in the list, it will be like this:
- The difference between 5 and 1 is 4
- The difference between 1 and 1 is 0
- The difference between 1 and 1 is 0
- The difference between the two numbers is 4 (the sum of the comparison of digits)
- Do the same for all other elements.
- Order a list for this calculated similarity
I could code this with alo for-loops, but I'm really looking for a faster way to do this. Is there any algorithm that does something like this? Quickly?
Further restrictions
- In my example, all elements have a length of 3, in a real scenario they have a length of 25
- All elements are the same length, len (myList [x]) == 25 is always true
- Elements can be strings, ints, float, or any other that suits the algorithm better
- There are only numbers from 1 to 5
Background
All position numbers are answers to questions, and I want to find the most similar answers to a given set of answers. Thus, “123” means that the user answered questions 1 = answer 1, question 2 = answer 2, question 3 = answer 3. These are multiple choice questions with 25 questions in total (= length 25), and there are always 5 different it is possible to answer (these are numbers 1-5).
PS: This is the first question I asked on Stackoverflow, so please be kind to me. I’ve been working on the Internet for many hours, but I couldn’t find a solution, so I asked here. I hope everything is in order. English is also not my first language.
Answer (thanks to all participants!)
Answer@larsmans (qaru.site/questions/1114663/...) , . , , . @gnibbler (qaru.site/questions/1114663/...). , , @larsmans . !