Computer Science, asked by kumarnirmal447, 3 months ago

write a Python program to count the frequency of a given element in list of numbers ​

Answers

Answered by Anonymous
35

The python code will be..

import collections

my_list = [10,10,10,10,20,20,20,20,40,40,50,50,30]

print("Original List : ",my_list)

ctr = collections.Counter(my_list)

print("Frequency of the elements in the List : ",ctr)

And the output will be..

Original List : [10, 10, 10, 10, 20, 20, 20, 20, 40, 40, 50, 50, 30]

Frequency of the elements in the List : Counter({10: 4, 20: 4, 40: 2, 50: 2, 30: 1})

.

.

.

ʜᴏᴘᴇ ɪᴛ ʜᴇʟᴘꜱ ᴜ.. ❤️

ᴍᴀʀᴋ ɪᴛ ᴀꜱ ʙʀᴀɪɴᴀʟɪꜱᴛ ᴘʟᴢ.. ❤️

Answered by syed2020ashaels
0

Answer:

Given below is the code

Explanation:

def count_frequency(numbers, x):

   return numbers.count(x)

numbers = [1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 1]

x = 1

frequency = count_frequency(numbers, x)

print("Frequency of", x, "in the list is:", frequency)

def count_frequency(numbers, x):

   return numbers.count(x)

The code defines a function count_frequency(numbers, x) that takes two arguments: numbers, a list of numbers, and x, the element whose frequency we want to count.

The function returns the result of calling the count method on the numbers list, which returns the number of occurrences of the x element in the list.

The code sets the numbers list to [1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 1] and x to 1. It then calls the count_frequency function with these arguments and stores the result in the variable frequency.

Finally, the code prints the result with the statement print("Frequency of", x, "in the list is:", frequency). The output of the program will be: Frequency of 1 in the list is: 4.

See more:

https://brainly.in/question/13308450

#SPJ3

Similar questions