Skip to content
Advertisement

Python – Reset Increment

I have a list of values in a column (A) and want to increment by 1 in column (B) until the value in A changes, however, having difficulty in my loop to reset the increment when A changes.

JavaScript

Here is desired output:

Product(A) No.(B)
Apple 1
Apple 2
Apple 3
Orange 1
Orange 2
Orange 3
Orange 4
Orange 5
Orange 6
Melon 1
Melon 2
Peach 1
Peach 2
Peach 3
Peach 4

Advertisement

Answer

The other two answers are awesome, but I was initially stuck to follow your initial approach with the counter, so here’s what I did:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement