Search code examples
pythonlistexponent

Numbers with the same base


I'm quite new to python and need help; i have built a program that finds the prime factors of a number, for example: Input: 64 Output: [2, 2, 2, 2, 2, 2] (the factors are stored in a list) but I want to join the numbers with the same base, and write them in the following form: 64 = (2^6), or 360 = (2^3)*(3^2)*5

I cannot figure out a way to do that, can someone please help me?


Solution

  • The type you are looking for is Counter from the collections module.

    The usage is very straightforward:

    from collections import Counter
    
    dividers_counter = Counter(dividers)
    

    It will contain the dividers as keys and the number of occurrence as values: Counter({2: 3, 3: 2})