Python- Percentage occurrence at index


In this article, the user will learn the percentage occurrence at the index in the list. Determining the percentage recurrence of a particular value at a given index in a list or array is a common task in data analysis or processing. This kind of computation can offer insightful information on the distribution and trends in the data. In this article, we will examine two distinct strategies for solving this problem, talk about their algorithms, give code snippets that get the desired results, and then compare the strategies.

For Example −

Given list :  [4, 2, 3, 1, 5, 6, 7] 

Percentage occurrence of the value say 3 occurs at index 2, the percentage occurrence in this case will be 14.29% as it occurs only once.

Approaches

For finding the percentage occurrence at the index using Python, we can follow the two methods −

  • Utilizing naive iteration.

  • Utilizing list Comprehension.

Let us look into both approaches −

Utilizing Naive Iteration

The initial strategy employs an easy iterative procedure. Every entry in the list will be iterated through, compared to the element at the target index, and the number of occurrences will be tracked. The percentage incidence is then determined by dividing the count by the length of the entire list.

Algorithm

The algorithm finding the percentage occurrence at the index using Python is given below −

  • Step 1 − Create a function that takes values as well as indexes as a parameter.

  • Step 2 − Take a variable count to hold the value of occurrence of value at the specified index.

  • Step 3 − Create a loop that will traverse over all the values.

  • Step 4 − Check for the values, if the values matched with the value at the specified index, then increment the count value.

  • Step 5 − Compute the percentage occurrence by dividing the count by the total number of values.

  • Step 6 − Return the percentage occurrence.

  • Step 7 − Call the function by passing the values and displaying the result.

Example

# Create a function that takes values as well as indexes as a parameter
def percentage_occurence_compute(value, index):
   # take a variable count to store the occurrence of value at the index specified
   count = 0
   # Run a loop for all the items in the values
   for item in value:
      # If the value is matched for the value at the specified index 
      # then increment the count value
      if item == value[index]:
         count += 1
   percentage_occurrence = (count / len(value)) * 100
   return percentage_occurrence

# Create an instance of values
value =  [4, 2, 3, 1, 5, 6, 7]
index = 2
percentage = percentage_occurence_compute(value, index)
print(f"The percentage occurrence of {value[index]} at index {index} is {percentage}%.")

Output

The percentage occurrence of 3 at index 2 is 14.285714285714285%.

Utilizing List Comprehension

Utilizing Python's list comprehension functionality is the subsequent method. The original list is filtered employing list comprehension to produce a new list that only comprises the items that match the value at the target index. The length of the filtered list is then divided by the entire length of the original list to determine the percentage occurrence.

Algorithm

The algorithm finding the percentage occurrence at the index using Python, is given below −

  • Step 1 − Create a function that takes values as well as indexes as a parameter.

  • Step 2 − Filter the list by giving only items at the specified index.

  • Step 3 − Compute the percentage for the filtered list and the given value.

  • Step 4 − Resize the images and compute the psnr with the help of skimage. Return the value of psnr.

  • Step 5 − Call the above function and pass the two image paths.

  • Step 6 − Display the psnr value.

Example

#Create a function that takes values as well as indexes as a parameter
def percentage_occurence_compute(value, index):
   # Filter list by finding only item given at any specified index
   filtered_list = [item for item in value if item == value[index]]
   # Compute the percentage for the filtered list and the given value
   percentage_occurrence = (len(filtered_list) / len(value)) * 100
   # return the computed value
   return percentage_occurrence

# Create an example of the list
value =  [4, 2, 3, 1, 5, 6, 7]
index = 2
# Call the above function
percentage = percentage_occurence_compute(value, index)
# Display the result
print(f"The percentage occurrence of {value[index]} at index {index} is {percentage}%.")

Output

The percentage occurrence of 3 at index 2 is 14.285714285714285%.

Conclusion

In this article, we investigated two methods for computing the Python PSNR (Peak Signal-to-Noise Ratio). In the context of image and video processing, PSNR is a critical statistic for evaluating the quality of digital data. One can determine precise PSNR scores and assess the quality of your digital signals using the Mean Squared Error (MSE) method or the skimage library.

Updated on: 18-Oct-2023

34 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements