Apricot
Apricot

Reputation: 3011

Python sorting multiple tuples

I am using python for image matching. The feature vectors for reference images is stored in a mysql database. For every new image, i generate the feature vector, find the difference between the test image and the reference vectors in mysql db and return the matches based on distance. This is working well. However, I want to sort the results based on the distance and get only the top 10 results that has the highest distance. My code so far is as follows:

for i,a in enumerate(data):
    features=[float(x) for x in a[6:]]
    d = chi2_distance(features, queryFeatures)
    results=a[1],d
    #res=sorted(results, key=itemgetter(1))
    #results.sort(key=lambda x:x[1])
    #results=sorted(results,key=lambda x: x[1])
    #results=sorted(results, key=lambda x: x[1], reverse=True)
    print(type(results))

When I print type of results i get the following:

<type 'tuple'>
<type 'tuple'>
<type 'tuple'>

When I print just the results, I get:

(127209905L, 6.459347693123215)
(127281515L, 7.790508647801378)
(127312977L, 6.374409391826666)

I tried various methods including, converting the tuples to list and then sorting by list, but none of them worked. I am relatively a new bee working on python...any guidance on how to go about this would be very helpful.

The expected output is

(127281515L, 7.790508647801378)
(127209905L, 6.459347693123215)
(127312977L, 6.374409391826666)

Upvotes: 0

Views: 42

Answers (1)

ravi
ravi

Reputation: 10733

First of all, instead of

results=a[1],d

use

results.append((a[1], d))

to collect all the data. And then

results = sorted(results, key=lambda x: x[1], reverse=True)

to sort results based on distance.

Upvotes: 2

Related Questions