-1

I'm trying to count the number of bigrams in a large group of text. I have already taken the text line by line from standard input, cleaned the text, and generated by bigrams. Now I have a nested loop that looks like this line by line:

Input:
[['breakfast', 'large'], ['large', 'portions'], ['portions', 'and'], ['friendly', 'staff']]
[['highly', 'recommend'), ['recommend', 'coming'], ['coming', 'here'], ['here', 'excellent'], ['excellent', 'service']]

What I want to do is split each of these nested lists into one line, so that I can print to standard out using

print ('%s\t%s' % (list(bigrams), 1))

This would give a line by line output such as:

Output:
['breakfast', 'large'], 1
['large', 'portions'], 1
['portions', 'and'], 1 
chaztize7
  • 63
  • 5

1 Answers1

0

You can convert the individual list items as tuples and then compare tuples. The create a dictionary with tuples as keys to maintain the count. Python compares tuples with the content inside as they are immutable.

count_dict= {}

for element in input_list:
  element =tuple(element )
  if(element in count_dict):
    count_dict[a]+=1
  else:
    count_dict[a]=1

print(count_dict)
venkata krishnan
  • 1,961
  • 1
  • 13
  • 20