1
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?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • You can create a dict as a counter, and while looping the list check what's each number's count and perform your operation – Tomerikoo Feb 26 '20 at 23:31
  • Use a [collections.Counter](https://docs.python.org/3/library/collections.html#collections.Counter). – ekhumoro Feb 26 '20 at 23:33
  • Use Counter from collections. An example can be found here https://stackoverflow.com/questions/2600191/how-can-i-count-the-occurrences-of-a-list-item. – Vítor Cézar Feb 26 '20 at 23:40
  • What is the problem? Have you tried anything, done any research? – AMC Feb 26 '20 at 23:57
  • Does this answer your question? [How can I count the occurrences of a list item?](https://stackoverflow.com/questions/2600191/how-can-i-count-the-occurrences-of-a-list-item) – AMC Feb 27 '20 at 00:27
  • @AMC That post did not answer my question, but the answer in this post did. – S Person D Person Feb 27 '20 at 18:44
  • I linked that other question since it's related. Why did you choose to accept that answer, by the way? Isn't the one by @Alexander a bit more straightforward? – AMC Feb 27 '20 at 18:46
  • @AMC I find the answer that I picked more clear. It explains what each section of the code does with comments. The answer by Alexander is more concise, and achieves the same result, but to me the selected answer gives a clearer explanation of how to achieve the result. – S Person D Person Feb 27 '20 at 18:49

2 Answers2

1

I would use a dictionary to keep track of what values have been used, and the number of times.

a_list = [1, 2, 3, 1, 2, 3, 1, 2, 3]
tracking_dict = {}
out_list = []
for item in a_list:
    # checks if item has been seen before
    if item in tracking_dict:
        # add the number as many times as it has been seen
        out_list.append(item + item * tracking_dict[item])
        # since you've seen the number, increase the count by 1
        tracking_dict[item] += 1
    else:
        # add it to the output list as-is
        out_list.append(item)
        # the item is new to this list
        tracking_dict[item] = 1
print(out_list)

Hope this helps!

Parakiwi
  • 591
  • 3
  • 19
  • 2
    I would recommend you to read about the dict method [`setdefault`](https://docs.python.org/3/library/stdtypes.html#dict.setdefault). It can be used to reduce your conditions – Tomerikoo Feb 26 '20 at 23:37
  • @Tomerikoo Or `collections.defaultdict`, similar idea. – AMC Feb 27 '20 at 18:53
1

This is a tweaked version of the answer by @Alexander, for the fans collections.defauldict out there.

import collections as colls

a_list = [1, 2, 3, 1, 2, 3, 1, 2, 3]
d = colls.defaultdict(int)
result = []

for val in a_list:
    d[val] += val
    result.append(d[val])

print(result)

Output:

[1, 2, 3, 2, 4, 6, 3, 6, 9]
AMC
  • 2,642
  • 7
  • 13
  • 35