- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
Python â€“ Find the frequency of numbers greater than each element in a list
When it is required to find the frequency of numbers greater than each element in a list, a list comprehension and the ‘sum’ method is used.
Below is a demonstration of the same −
my_list = [24, 13, 72, 22, 12, 47] print("The list is :") print(my_list) my_result = [sum(1 for element in my_list if index <= element) for index in my_list] print("The result is :") print(my_result)
The list is : [24, 13, 72, 22, 12, 47] The result is : [3, 5, 1, 4, 6, 2]
A list of integers is defined and is displayed on the console.
A list comprehension is used to iterate over the elements of the list, and check if the index of every element is less than the element itself.
If so, it is added using the ‘sum’ method, converted to a list, and assigned to a variable.
This is displayed as output on the console.
- Related Articles
- Find smallest element greater than K in Python
- Python Indices of numbers greater than K
- Python - Number of values greater than K in list
- Find element in a sorted array whose frequency is greater than or equal to n/2 in C++.
- Python - Get the Index of first element greater than K
- Python Program to find the cube of each list element
- List six rational numbers less than $-2$ but greater than $-3$.
- Find frequency of each element in a limited range array in less than O(n) time in C++
- Element with largest frequency in list in Python
- How to find the frequency of values greater than or equal to a certain value in R?
- Find frequency of each word in a string in Python
- How to check if a list element is greater than a certain value in R?
- Check if frequency of each digit is less than the digit in Python
- Write a Golang program to find the frequency of each element in an array