-2

Can anybody help with a function to convert the following ngram into the result below? The return should concatenate the first N-1 elements of the ngram and count how often the different successors (Nth element) occur. I was thinking of some nested for loops, but I am struggling to build a structure. Thanks a lot!!

ngrams = [['will', 'leave', 'florida'], ['will', 'leave', 'nyc'], ['will', 'leave', 'florida'],['wont', 'leave', 'florida']]

The return should be:

{'will leave': {'florida': 2, 'nyc': 1}, 'wont leave': {'florida': 1}}

Kenan
  • 13,156
  • 8
  • 43
  • 50
Nicolas
  • 31
  • 2

1 Answers1

0

Here is one approach

ngrams = [['will', 'leave', 'florida'], ['will', 'leave', 'nyc'], ['will', 'leave', 'florida'],['wont', 'leave', 'florida']]

dct = {'will leave': {}, 'wont leave': {}}

for i in ngrams:
    a, b, c = i
    if c in dct[a + ' ' + b]:
        dct[a+' '+b][c] += 1
    else:
        dct[a+' '+b].update({c: 1})

print(dct)

{'will leave': {'florida': 2, 'nyc': 1}, 'wont leave': {'florida': 1}}

Kenan
  • 13,156
  • 8
  • 43
  • 50