Skip to content
Minsing Log
Go back

English Edition

When Returning Multiple Values in Functional Programming with the `apply` Function

Programming & CS

Situation

Here is more detailed code related to the text above!

result = make_passages.apply(self.__make_passages_and_retrieval_gt, axis=1)
make_passages['passages'], self.ingest_data['retrieval_gt'], self.ingest_data['retrieval_gt_order'] = zip(*result)
            
            
def __make_passages_and_retrieval_gt(self, row):
    passages = []
    retrieval_gt = []
    retrieval_gt_ord = []
    ord_rank = 0
    for passage_idx, passage_text in enumerate(row['passage_text']):
        passages.append(Passage(
            id=str(row['query_id']) + '_' + str(passage_idx),
            content=passage_text,
            filepath=self.file_path,
            metadata_etc={
                'url': row['url'][passage_idx]
            }
        ))
        # Make retrieval gt and retrieval gt order.(27 is max count of passage texts in v2.1)
        if row['is_selected'][passage_idx] == 1:
            retrieval_gt.append(str(row['query_id']) + '_' + str(passage_idx))
            retrieval_gt_ord.append(27 - ord_rank)
            ord_rank += 1

    return passages, retrieval_gt, retrieval_gt_ord

How can you receive the multiple values returned by the self.__make_passages_and_retrieval_gt function?

Solution

result = make_passages.apply(self.__make_passages_and_retrieval_gt, axis=1)
make_passages['passages'], self.ingest_data['retrieval_gt'], self.ingest_data['retrieval_gt_order'] = zip(*result)

As in the code above, you can use zip and unpacking~

Follow the journey

Get new posts by email.

Short, honest notes on building, startups, learning, failures, and retrospectives, for readers who follow the journey rather than pass through from search.

The newsletter is coming soon. Until then, follow via RSS or email me to get notified.


Share this post:

Comments

Sign in with GitHub to leave a comment.


Previous Post
RAGchain Glossary
Next Post
Creating multiple dataframes with apply