Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find similar items in list of dictionaries based on values

I want to match similar articles from a django database based on tags which are stored in a list of dictionary like follows:

myarticle = {'pk': 17, 'tags': [0, 1, 0, 1, 0]}
allarticles = [{'pk': 1, 'tags': [0, 0, 0, 1, 0]}, 
               {'pk': 2, 'tags': [0, 1, 0, 1, 0]},
               {'pk': 3, 'tags': [1, 1, 0, 0, 0]},
               {'pk': 4, 'tags': [1, 0, 1, 0, 1]},
               {'pk': 5, 'tags': [0, 0, 0, 0, 1]}]

What is the most convenient way to get a list back that ranks the number of matching tags based on the input myarticle. Expected result:

result = [2, 1, 3, 5, 4]
like image 514
Helmut Herglotz Avatar asked Jul 26 '18 14:07

Helmut Herglotz


People also ask

How do you compare two dictionaries of values?

The compare method cmp() is used in Python to compare values and keys of two dictionaries. If method returns 0 if both dictionaries are equal, 1 if dic1 > dict2 and -1 if dict1 < dict2.

Can we sort dictionary based on values?

To correctly sort a dictionary by value with the sorted() method, you will have to do the following: pass the dictionary to the sorted() method as the first value. use the items() method on the dictionary to retrieve its keys and values. write a lambda function to get the values retrieved with the item() method.

How are dictionaries similar to lists?

Dictionaries are similar to lists, except instead of using a numerical index to insert and retrieve an "item", you use a key and value. For example, if you think of a dictionary as a phone book, the key would be a person's name, and the value would be their phone number.

How do I sort a list of dictionaries by a value of the dictionary?

To sort a list of dictionaries according to the value of the specific key, specify the key parameter of the sort() method or the sorted() function. By specifying a function to be applied to each element of the list, it is sorted according to the result of that function.


1 Answers

You can use sorted:

myarticle = {'pk': 17, 'tags': [0, 1, 0, 1, 0]}
allarticles = [{'pk': 1, 'tags': [0, 0, 0, 1, 0]}, 
           {'pk': 2, 'tags': [0, 1, 0, 1, 0]},
           {'pk': 3, 'tags': [1, 1, 0, 0, 0]},
           {'pk': 4, 'tags': [1, 0, 1, 0, 1]},
           {'pk': 5, 'tags': [0, 0, 0, 0, 1]}]
new_articles = sorted(allarticles, key=lambda x:sum(a == b for a, b in zip(myarticle['tags'], x['tags'])), reverse=True)
final_results = [i['pk'] for i in new_articles]

Output:

[2, 1, 3, 5, 4]
like image 73
Ajax1234 Avatar answered Sep 18 '22 15:09

Ajax1234