How can I make a list with the times that an integer occurs in another list?

NewbieProgram

I wrote some code (Python) that prints that expresses a number in prime numbers. Here is the code that I have:

n = 24
lst=[]
i = 2
while n>1:
    if n%i==0:
      lst.append(i)
      n=n/i
      i-=1
    i+=1  
print(lst)

For example when my n is 24, the output from the list is [2,2,2,3], because 2*2*2*3=24. Now I want to make a list that gives how many times a number occurs in my list. So for this example I want that my output is: [3,1], because 2 occurs 3 times in my list en 3 occurs one time in my list. I have no idea how I can do this. When I uses the function count(), it prints [3,3,3,1], because it checks the other list and checks for each item in it how often it exists, so also for the second and third item, which is the same as the first. Does anyone know how my code must look in order to have the output [3,1]?

thisisrandy

There's a datatype called a Counter in the standard library. Given an input iterable, it will output a dictionary of { value: count of value } pairs. You can use it as follows (output is from the ipython REPL):

In [1]: from collections import Counter

In [2]: primes = [2,2,2,3]

In [3]: counts = Counter(primes)

In [4]: counts
Out[4]: Counter({2: 3, 3: 1})

In [5]: counts.values()
Out[5]: dict_values([3, 1])

In [6]: list(counts.values())
Out[6]: [3, 1]

For illustrative purposes, let's also construct this manually using a defaultdict:

In [7]: from collections import defaultdict

In [8]: counts = defaultdict(int)

In [9]: for p in primes:
   ...:     counts[p] += 1
   ...:

In [10]: counts
Out[10]: defaultdict(int, {2: 3, 3: 1})

In [11]: counts.values()
Out[11]: dict_values([3, 1])

In [12]: list(counts.values())
Out[12]: [3, 1]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can i iterate a list in elixir to make another list?

How can I create a recursive function in LISP that counts the number of times an atom occurs in a nested list

How can I make an integer from list of integers?

How can I solve this error that occurs when I try to sort a nested list based on another nested?

How can I make the Array List be called in another class java

How to check if int occurs in list 3 times?

My function returns a list with a single integer in it, how can I make it return only the integer?

how can I create a list of times in php?

How can I make a loop to run this code 15 times using a list of the range variables I defined?

how can I validate a list index as an integer?

How can I make a list-column that is a subset of another list-column in R?

How can I make an adaptive list to check against another adaptive list?

How can I make my output be printed as '+' for the number of times a letter appears in a list?

How can I access this list in another class?

How can I pass a List to another class

How can I make clickable list with sublinks ?

How can i make the list appear on hover?

How can I make an extension for a list of lists

How can I make list comprehension have "or?"

MYSQL How can I make dropdown list?

How can i make list in class, Python

How can I make such a list using tkinter?

I need to check if word in list of word exist in list of strings and how many times it occurs and return dict({word: total_occurrence})

In python, how do I make a matrix of the number of times(how many rows) each value in one column occurs with values in another column?

How to get all indexes of a value that occurs multiple times in a list?

How can I make a list defined in one method available in another method when both methods are in the same class?

How can I update the contents of a list with data from another list?

How can I verify that a list elements are in same order of another list or not?

How can I reorder a list based on another active sortable list?