Finding n greatest differences between two lists

I have two lists oldand newwith the same number of items.

I am trying to write an effective function that takes nas a parameter, compares the elements of two lists in the same places (by index), finds the nbiggest differences and returns the indices of those n.

I thought this was best solved with a sorted dictionary, but not available in Python (and I don't know any libraries that offer it). Maybe there is a better solution?

+5
source share
5 answers

Whenever you think " n is the greatest , think about it ." heapq

>>> import heapq
>>> import random
>>> l1 = [random.randrange(100) for _ in range(100)]
>>> l2 = [random.randrange(100) for _ in range(100)]
>>> heapq.nlargest(10, (((a - b), a, b) for a, b in zip(l1, l2)))
[(78, 99, 21), (75, 86, 11), (69, 90, 21), (69, 70, 1), (60, 86, 26), (55, 95, 40), (52, 56, 4), (48, 98, 50), (46, 80, 34), (44, 81, 37)]

Here, the largest elements x in time O (n log x) will be found, where n is the total number of elements in the list; sorting is done in O (n log n) time.

It just occurred to me that the above does not do what you requested. You need an index! Still very easy. I will also use abshere if you want to get the absolute value of the difference:

>>> heapq.nlargest(10, xrange(len(l1)), key=lambda i: abs(l1[i] - l2[i]))
[91, 3, 14, 27, 46, 67, 59, 39, 65, 36]
+8
source

Assuming that the number of items in the lists is not large, you can simply separate them all, sort and select the first one n:

print sorted((abs(x-y) for x,y in zip(old, new)), reverse=True)[:n]

This will be O(k log k)where kis the length of your source lists.

n k, nlargest, heapq:

import heapq
print heapq.nlargest(n, (abs(x-y) for x,y in zip(old, new))

O(k log n) O(k log k), k >> n. , , itertools.izip zip.

+2

, , :

.py

l1 = [15,2,123,4,50]
l2 = [9,8,7,6,5]


l3 = zip(l1, l2)

def f(n):
    diff_val = 0
    index_val = 0
    l4 = l3[:n]

    for x,y in l4:
        if diff_val < abs(x-y):
            diff_val = abs(x-y)
            elem = (x, y)
            index_val = l3.index(elem)

    print "largest diff: ", diff_val
    print "index of values:", index_val


n = input("Enter value of n:") 

f(n)

:

[avasal@avasal ]# python difference.py 
Enter value of n:4
largest diff:  116
index of values: 2
[avasal@avasal]#

, , , .

0
>>> l = []
... for i in itertools.starmap(lambda x, y: abs(x-y), itertools.izip([1,2,3],   [100,102,330])):
...     l.append(i)
>>> l
5: [99, 100, 327]

itertools . starmap tuples *args. . With max . index .

l.index(max(l)

>>> l.index(max(l))
6: 2
0

, numpy ( , numpy, ). , , . . n - sorted_inds[:n], .

, , , , , , , , , numpy .

import numpy

list1 = numpy.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
list2 = numpy.array([9, 8, 7, 6, 5, 4, 3, 2, 1])

#Caculate the delta between the two lists
delta = numpy.abs(numpy.subtract(list1, list2))
print('Delta: '.ljust(20) + str(delta))

#Get a list of the indexes of the sorted order delta
sorted_ind = numpy.argsort(delta)
print('Sorted indexes: '.ljust(20) + str(sorted_ind))

#reverse sort
sorted_ind = sorted_ind[::-1]
print('Reverse sort: '.ljust(20) + str(sorted_ind))

Delta:              [8 6 4 2 0 2 4 6 8]
Sorted indexes:     [4 3 5 2 6 1 7 0 8]
Reverse sort:       [8 0 7 1 6 2 5 3 4]
0

All Articles