Python lamda function in dict
Python lamda function in dict
This woking code gives me the 5 most relevant documents for a topic out of my corpus.
most_relevant_docs = sorted(bow_corpus, reverse=True, key=lambda doc: abs(dict(doc).get(topic_number, 0.0)))
print most_relevant_docs[ :5]
But since the corpus is not readable by human I want to zip an index to the corpus so I can recover the depending documents.
corpus_ids = range(0,len(corpus))
most_relevant_docs = sorted(zip(corpus_ids, bow_corpus), reverse=True, key=lambda my_id, doc : abs(dict(doc).get(topic_number, 0.0)))
print most_relevant_docs[ :5]
Where do I have to adapt the lamda function so it returns the id together with the document?
the lambda is used as a key only, will not modify the structure of the output but the order of it
– Netwave
Jun 29 at 9:38
1 Answer
1
Try this
sortingFunc = lambda doc: abs(dict(doc).get(topic_number, 0.0))
corpus_ids = range(0,len(corpus))
most_relevant_docs = sorted(zip(corpus_ids, bow_corpus), reverse=True, key=lambda pair: sortingFunc(pair[1]))
When you zip it, each element becomes like (index, value)
, so the original sorting key wouldn't work. You'd need to modify it so it sorts by the value as opposed to the pair
(index, value)
This seems to work. Thanks a lot.
– nlp_noob
Jun 29 at 9:54
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Can you mock up some data so we can visualize what you are trying to achieve? Of course, as it stands, we can't run any of your code.
– jpp
Jun 29 at 9:36