I'm trying to write a Python script that will search through a CSV file and identify the number of occurrences when two items appear next to each other.
For example, let's say the CSV looks like the following:
red,green,blue,red,yellow,green,yellow,red,green,purple,blue,yellow,red,blue,blue,green,purple,red,blue,blue,red,green
And I'd like to find the number of times when "red,green" occurs next to each other (but I'd like a solution that isn't just specific to the words in this CSV).
So far, I thought that possibly converting the CSV to a list might be a good start:
import csv
with open('examplefile.csv', 'rb') as f:
reader = csv.reader(f)
your_list = list(reader)
print your_list
Which returns:
[['red', 'green', 'blue', 'red', 'yellow', 'green', 'yellow', 'red', 'green', 'purple', 'blue', 'yellow', 'red', 'blue', 'blue', 'green', 'purple', 'red', 'blue', 'blue', 'red', 'green ']]
In this list, there are three occurrences of 'red', 'green'
— what is an approach/module/loop structure I could use to find out if there are more than one occurrence of two items in a list that are right next to each other in a list?