Order a list of all items in Python

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 . !

+5
5

myCmpItem, .

myCmpItem = map(int, myCmpItem)

, myCmpItem. . - L1 ( "", ).

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

key .

sorted(myList, key=dist)

(PS: , L1 ? , , 1 2, 3 .. , .)

+7

lambda :

sorted(myList, key=lambda item: sum([abs(int(x) - int(y)) for x, y in zip(item, myCmpItem)])
+5
def cmpWith(num):
    def compare(item):
        """ calculate the difference between num and item """
        return sum(
            abs(int(n) - int(x)) # cast to int to make the substraction possible
            for x,n in zip(item, num) # zip makes pairs from both lists 
        )

    return compare

lst = ['111','222','333','444','555','123']
print sorted(lst, key=cmpWith('511'))
+4

?

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

def make_key(x):
    diff = 0
    for a, b in zip(x, myCmpItem):
        diff += abs(int(a)-int(b))
    return diff

mySortedList = sorted(myList, key=make_key)

print mySortedList
+2

, int

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

# only need to compute this dict once
dists = {(i,j):abs(int(i)-int(j)) for i in '12345' for j in '12345'}

print sorted(myList, key=lambda j: sum(dists[i] for i in zip(j, myCmpItem)))

2,9 , larsmans 100000 x 25 .

+2

All Articles