Python3: grouping list of objects by words in description

Viewed 25

I have a standard list of objects, where each object is defined as

class MyRecord(object):

  def __init__(self, name, date, category, memo):
      self.name = name 
      self.date = date
      self.category = category 
      self.memo = memo.strip().split()

When I create an object usually the input memo is a long sentence, for example: "Hello world this is a new funny-memo", which then in the init function turns into a list ['Hello', 'world', 'is', 'a', 'new', 'funny-memo'].

Given let's say a 10000 of such records in the list (with different memos) I want to group them (as fast as possible) in the following way:

'Hello' : [all the records, which memo contains word 'Hello']
'world' : [all the records, which memo contains word 'world']
'is' : [all the records, which memo contains word 'is']

I know how to use group-by to group the records by for example name, date, or category (since it is a single value), but I'm having a problem to group in the way described above.

1 Answers

If you want to group them really fast then you should do it once and never recalculate. To achieve this you may try approach used for caching that is group objects during the creation:

class MyRecord():

    __groups = dict()

    def __init__(self, name, date, category, memo):
        self.name = name
        self.date = date
        self.category = category
        self.memo = memo.strip().split()
        for word in self.memo:
            self.__groups.setdefault(word, set()).add(self)

    @classmethod
    def get_groups(cls):
        return cls.__groups


records = list()
for line in [
        'Hello world this is a new funny-memo',
        'Hello world this was a new funny-memo',
        'Hey world this is a new funny-memo']:
    records.append(MyRecord(1, 1, 1, line))


print({key: len(val) for key, val in MyRecord.get_groups().items()})

Output:

{'Hello': 2, 'world': 3, 'this': 3, 'is': 2, 'a': 3, 'new': 3, 'funny-memo': 3, 'was': 1, 'Hey': 1}
Related