a_list = [1, 2, 3, 1, 2, 3, 1, 2, 3]
I would like to iterate over a_list
and find duplicates. If a duplicate is found I would like to perform a calculation on that duplicate. If another duplicate of the same instance is found, a different calculation should be performed.
For example:
Start iterating a_list
:
1, 2, 3, 1 [Instance of duplicate 1 found - Perform 1+1 on instance]
Continue iterating...
1, 2, 3, 1, 2 [Instance of duplicate 2 found - Perform 2+2 on instance]
Continue iterating...
1, 2, 3, 1, 2, 3 [Instance of duplicate 3 found - Perform 3+3 on instance]
Continue iterating...
1, 2, 3, 1, 2, 3, 1 [Second instance of duplicate 1 found - Perform 1+1+1 on instance]
Continue iterating...
1, 2, 3, 1, 2, 3, 1, 2 [Second instance of duplicate 2 found - Perform 2+2+2 on instance]
Continue iterating...
1, 2, 3, 1, 2, 3, 1, 2, 3 [Second instance of duplicate 3 found - Perform 3+3+3 on instance]
Once completed a new list is created with all calculations included:
new_list = [1, 2, 3, 2, 4, 6, 3, 6, 9]
Could someone explain to me how to find duplicates as well as count the instances of these duplicates, so that I can perform a different calculation on each new instance of the duplicate?