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]
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.
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.
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.
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.
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With