

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python – Sort Matrix by None frequency
<p>When it is required to sort a matrix by ‘None’ frequency, a method is defined that takes a parameter and uses list comprehension, ‘not’ operator and ‘len’ method to determine the result.</p><h2>Example</h2><p>Below is a demonstration of the same −</p><pre class="demo-code notranslate language-python" data-lang="python">def get_None_freq(row): return len([element for element in row if not element]) my_list = [[None, 24], [None, 33, 3, None],[42, 24, 55], [13, None, 24]] print("The list is : ") print(my_list) my_list.sort(key = get_None_freq) print("The result is : ") print(my_list)</pre><h2>Output</h2><pre class="result notranslate">The list is : [[None, 24], [None, 33, 3, None], [42, 24, 55], [13, None, 24]] The result is : [[42, 24, 55], [None, 24], [13, None, 24], [None, 33, 3, None]]</pre><h2>Explanation</h2><ul class="list"><li><p>A method named ‘get_None_freq’ is defined that takes a list as a parameter, and uses list comprehension to iterate over the elements and uses ‘not’ operator to check if an element is not present in the list, and determines its length.</p></li><li><p>This length is returned as output.</p></li><li><p>Outside the method, a list is defined and displayed on the console.</p></li><li><p>The list is sorted using the ‘sort’ method and the key is specified as the previously defined method.</p></li><li><p>This is the output that is displayed on the console.</p></li></ul>
- Related Questions & Answers
- Python – Sort by Uppercase Frequency
- Python - Sort rows by Frequency of K
- Python – Sort Matrix by total characters
- Python – Sort Matrix by Row Median
- Python - Sort Matrix by Maximum Row element
- Python – Sort Matrix by Palindrome count
- Sort Characters By Frequency in C++
- Python – Sort String list by K character frequency
- Python – Sort Matrix by Maximum String Length
- Program to sort array by increasing frequency of elements in Python
- Python program to sort tuples by frequency of their absolute difference
- Python – Sort Matrix by K Sized Subarray Maximum Sum
- Python Program to sort rows of a matrix by custom element count
- Python - Count the frequency of matrix row length
- Python – Sort Matrix by Number of elements greater than its previous element
Advertisements