Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Programming Articles
Page 353 of 2547
Python – Sort by Maximum digit in Element
When it is required to sort by maximum digit in element, a method is defined that uses str() and max() method to determine the result. This technique converts each number to a string, finds its largest digit, and uses that digit as the sorting key. Syntax def max_digits(element): return max(str(element)) list.sort(key=max_digits) Example Here's how to sort a list of numbers by their maximum digit ? def max_digits(element): return max(str(element)) my_list = [224, 192, 145, 18, 3721] print("The list is :") print(my_list) ...
Read MorePython – Calculate the percentage of positive elements of the list
When calculating the percentage of positive elements in a list, we can use list comprehension along with the len() method to count positive values and compute the percentage. Example Here's how to calculate the percentage of positive elements in a list ? my_list = [14, 62, -22, 13, -87, 0, -21, 81, 29, 31] print("The list is :") print(my_list) my_result = (len([element for element in my_list if element > 0]) / len(my_list)) * 100 print("The result is :") print(my_result) Output The list is : [14, 62, -22, 13, -87, ...
Read MorePython – Filter Rows with Range Elements
When working with nested lists, you may need to filter rows that contain all elements from a specific range. Python provides an elegant solution using list comprehension with the all() function to check if every element in a range exists within each row. Syntax filtered_rows = [row for row in nested_list if all(element in row for element in range(start, end + 1))] Example Let's filter rows that contain all numbers from 2 to 5 ? my_list = [[3, 2, 4, 5, 10], [32, 12, 4, 51, 10], [12, 53, 11], [2, 3, ...
Read MorePython program to Sort Strings by Punctuation count
When you need to sort strings by their punctuation count, you can define a custom function that counts punctuation marks and use it as a sorting key. This technique uses Python's string.punctuation module and list comprehension to efficiently count punctuation characters. Example Here's how to sort strings based on the number of punctuation marks they contain − from string import punctuation def get_punctuation_count(my_str): return len([element for element in my_str if element in punctuation]) my_list = ["python@%^", "is", "fun!", "to@#r", "@#$learn!"] print("The list is :") print(my_list) my_list.sort(key = get_punctuation_count) ...
Read MorePython – Sort Matrix by Row Median
When working with matrices in Python, you may need to sort rows based on their median values. Python's statistics module provides a median() function that can be used with the sort() method's key parameter to achieve this. Syntax from statistics import median matrix.sort(key=lambda row: median(row)) Example Here's how to sort a matrix by row median using a custom function ? from statistics import median def median_row(row): return median(row) my_matrix = [[43, 14, 27], [13, 27, 24], [32, 56, 18], [34, 62, 55]] print("Original matrix:") ...
Read MorePython – All combinations of a Dictionary List
When you need to create all possible combinations of dictionaries from keys and values, Python's itertools.product function combined with list comprehension provides an elegant solution. This technique generates the Cartesian product of values and maps them to corresponding keys. Basic Example Here's how to create all combinations where keys from one list are paired with all possible value combinations from another list ? from itertools import product keys = ["python", "is", "fun"] values = [24, 15] print("The keys list is:") print(keys) print("The values list is:") print(values) # Generate all combinations using product temp ...
Read MorePython – Average digits count in a List
When it is required to count average digits in a list, a simple iteration, the str() method and the division operator is used to convert numbers to strings and calculate the mean digit count. Below is a demonstration of the same − Example my_list = [324, 5345, 243, 746, 432, 463, 946787] print("The list is :") print(my_list) sum_digits = 0 for ele in my_list: sum_digits += len(str(ele)) my_result = sum_digits / len(my_list) print("The result is :") print(my_result) Output The ...
Read MorePython – Test String in Character List and vice-versa
When checking if a string exists within a character list or vice-versa, Python provides several approaches using the in operator and join() method. This is useful for string validation and pattern matching. Testing String in Character List You can check if all characters of a string exist in a character list by joining the list and using the in operator ? my_string = 'python' print("The string is:") print(my_string) my_key = ['p', 'y', 't', 'h', 'o', 'n', 't', 'e', 's', 't'] print("The character list is:") print(my_key) joined_list = ''.join(my_key) print("Joined string:", joined_list) my_result = ...
Read MorePython – Test for all Even elements in the List for the given Range
When it is required to test all even elements in the list for the given range, a simple iteration and the modulus operator is used. Example Below is a demonstration of the same − my_list = [32, 12, 42, 61, 58, 60, 19, 16] print("The list is :") print(my_list) i, j = 2, 7 my_result = True for index in range(i, j + 1): if my_list[index] % 2: my_result = False break ...
Read MorePython – Sort Matrix by Maximum String Length
When working with matrices (lists of lists) containing strings, you might need to sort rows based on the maximum string length in each row. Python provides an elegant solution using the sort() method with a custom key function. Example Here's how to sort a matrix by the maximum string length in each row ? def max_length(row): return max([len(element) for element in row]) my_matrix = [['pyt', 'fun'], ['python'], ['py', 'cool'], ['py', 'ea']] print("The matrix is:") print(my_matrix) my_matrix.sort(key=max_length) print("The result is:") print(my_matrix) The matrix is: [['pyt', 'fun'], ...
Read More