I am trying to loop through column in an access table using Python. The column I have is sorted ascending.
I am trying to loop though the rows and when the value in the column changes, I want to get count of of all those values. In the example column below, the first group of values I want to count are the M1‘s. When the next row changes to M21, I want to count the M21‘s until it changes to M23b and so on.
I don’t want to use an if/else statement, because there are a few hundred different possible values. I played around with the groupby function in the itertools module, but I haven’t been able to solve the syntax to work in my example. I also tried a silly loop doing something like if row != row.next(): do_something but that blew up in my face. If anyone can suggest a work-around or show me a sample script that will do this for me, I’d appreciate it.
Example Column:
M1 M1 M1 M21 M21 M23b M23b S2 S23b S23B O1 O2 O2 O2
If you want to add printing in a loop doing other work, the following may be helpful: