string = 'abba com mother bill mother com abba dog abba mother com' words = string.split() res = {} for n in range(len(words)-2): comb = ' '.join(sorted(words[n:n+3])) res[comb] = res.get(comb, 0) + 1 print(res)
Standard input is empty
{'abba com mother': 3, 'bill com mother': 2, 'bill mother mother': 1, 'abba com dog': 1, 'abba abba dog': 1, 'abba dog mother': 1}