AmitDiwan has Published 10744 Articles

Python program to sort matrix based upon sum of rows

AmitDiwan

AmitDiwan

Updated on 06-Sep-2021 08:52:13

242 Views

When it is required to sort matrix based upon sum of rows, a method is defined that uses ‘sum’ method to determine the result.Below is a demonstration of the same −Example Live Demodef sort_sum(row):    return sum(row) my_list = [[34, 51], [32, 15, 67], [12, 41], [54, 36, 22]] ... Read More

Python – Sort by Maximum digit in Element

AmitDiwan

AmitDiwan

Updated on 06-Sep-2021 08:50:49

200 Views

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.Below is a demonstration of the same −Example Live Demodef max_digits(element):    return max(str(element)) my_list = [224, 192, 145, 18, 3721] print("The list is :") ... Read More

Python – Calculate the percentage of positive elements of the list

AmitDiwan

AmitDiwan

Updated on 06-Sep-2021 08:49:20

360 Views

When it is required to calculate the percentage of positive elements of the list, a list comprehension and the ‘len’ method are used.Below is a demonstration of the same −Example Live Demomy_list = [14, 62, -22, 13, -87, 0, -21, 81, 29, 31] print("The list is :") print(my_list) my_result ... Read More

Python – Filter Rows with Range Elements

AmitDiwan

AmitDiwan

Updated on 06-Sep-2021 08:47:47

261 Views

When it is required to filter rows with range elements, a list comprehension and the ‘all’ operator is used to determine the output.Below is a demonstration of the same −Example Live Demomy_list = [[3, 2, 4, 5, 10], [32, 12, 4, 51, 10], [12, 53, 11], [2, 3, 31, 5, 8, ... Read More

Python program to Sort Strings by Punctuation count

AmitDiwan

AmitDiwan

Updated on 06-Sep-2021 08:45:59

275 Views

When it is required to sort strings by punctuation count, a method is defined that takes a string as a parameter and uses list comprehension and ‘in’ operator to determine the result.Below is a demonstration of the same −Example Live Demofrom string import punctuation def get_punctuation_count(my_str):    return len([element for ... Read More

Python – Sort Matrix by Row Median

AmitDiwan

AmitDiwan

Updated on 06-Sep-2021 08:43:04

215 Views

When it is required to sort matrix by row median, a method is defined that uses the ‘median’ method to determine the result.Below is a demonstration of the same −Example Live Demofrom statistics import median def median_row(row):    return median(row) my_list = [[43, 14, 27], [13, 27, 24], [32, ... Read More

Python – All combinations of a Dictionary List

AmitDiwan

AmitDiwan

Updated on 06-Sep-2021 08:39:24

927 Views

When it is required to display all the combinations of a dictionary list, a simple list comprehension and the ‘zip’ method along with the ‘product’ method are used.Below is a demonstration of the same −Example Live Demofrom itertools import product my_list_1 = ["python", "is", "fun"] my_list_2 = [24, 15] ... Read More

Python – Average digits count in a List

AmitDiwan

AmitDiwan

Updated on 06-Sep-2021 08:33:22

224 Views

When it is required to count average digits in a list, a simple iteration, the ‘str’ method and the ‘/’ operator is used.Below is a demonstration of the same −Example Live Demomy_list = [324, 5345, 243, 746, 432, 463, 946787] print("The list is :") print(my_list) sum_digits = 0 ... Read More

Python – Test String in Character List and vice-versa

AmitDiwan

AmitDiwan

Updated on 06-Sep-2021 08:19:07

270 Views

When it is required to test string in character list and vice-versa, a simple ‘in’ operator and ‘join’ method are used.Below is a demonstration of the same −Example Live Demomy_string = 'python' print("The string is :") print(my_string) my_key = ['p', 'y', 't', 'h', 'o', 'n', 't', 'e', 's', 't'] ... Read More

Python – Test for all Even elements in the List for the given Range

AmitDiwan

AmitDiwan

Updated on 06-Sep-2021 08:17:15

183 Views

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.Below is a demonstration of the same −Example Live Demomy_list = [32, 12, 42, 61, 58, 60, 19, 16] print("The list is :") print(my_list) i, ... Read More

Advertisements