Home > Mobile >  Python - Reset Increment
Python - Reset Increment

Time:09-09

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.

B = []
list_length = len(A)

for i in range(0, list_length):
    j = 1
    while A[i] == A[i 1]:
        B = j
        j  = 1
        i  = 1
    else:
        B = 1
        j  = 1

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

CodePudding user response:

You could iterate through A, and build list B;

A = ['a','a','a','b','b','b','b','b','b','c','c','d','d','d','d']  
B = [1]
for i in range(1,len(A)):
    B.append(B[i-1] 1 if A[i]==A[i-1] else 1)

--EDIT-- It's a bit unpythonic though isn't it, looks kinda C-ish.

A = ['a','a','a','b','b','b','b','b','b','c','c','d','d','d','d']  
B = [1]
for i,j in zip(A[:-1],A[1:]):
    B.append(B[-1]*(i==j) 1)

That's a bit better!

CodePudding user response:

If the list A is sorted such that the re-occurences of an entry are in subsequent indices you could use np.unique and simply stack arrays created with np.arange as follows:

A = ['Apple','Apple','Apple','Orange','Orange','Orange','Orange','Orange','Orange','Melon','Melon','Peach','Peach','Peach','Peach']  
Aun, cnts = np.unique(A, return_counts=True)
table = np.concatenate([np.stack([np.tile(Aun[i], [n]), np.arange(1, n 1)], axis=1)
                        for i, n in enumerate(cnts)])
>>>array([['Apple', '1'],
       ['Apple', '2'],
       ['Apple', '3'],
       ['Melon', '1'],
       ['Melon', '2'],
       ['Orange', '1'],
       ['Orange', '2'],
       ['Orange', '3'],
       ['Orange', '4'],
       ['Orange', '5'],
       ['Orange', '6'],
       ['Peach', '1'],
       ['Peach', '2'],
       ['Peach', '3'],
       ['Peach', '4']], dtype='<U21')

CodePudding user response:

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:

from prettytable import PrettyTable
 
fruit_table = PrettyTable(["Product(A)", "No.(B)"])

products = ["Apple", "Apple","Apple", "Orange","Orange","Orange","Orange","Orange","Melon","Melon", "Peach","Peach","Peach"]

counter = 0
for i in range(0, len(products)-1):

    if products[i] == products[i 1]:
        counter = 1
        fruit_table.add_row([products[i],counter])
    else:
        fruit_table.add_row([products[i],counter 1])
        counter=0

print(fruit_table)

Output:

 ------------ -------- 
| Product(A) | No.(B) |
 ------------ -------- 
|   Apple    |   1    |
|   Apple    |   2    |
|   Apple    |   3    |
|   Orange   |   1    |
|   Orange   |   2    |
|   Orange   |   3    |
|   Orange   |   4    |
|   Orange   |   5    |
|   Melon    |   1    |
|   Melon    |   2    |
|   Peach    |   1    |
|   Peach    |   2    |
 ------------ -------- 
  • Related