AmitDiwan has Published 10744 Articles

Python Program that prints the rows of a given length from a matrix

AmitDiwan

AmitDiwan

Updated on 14-Sep-2021 10:39:05

146 Views

When it is required to print the row of a specific length from a matrix, list comprehension is used.ExampleBelow is a demonstration of the samemy_list = [[22, 4, 63, 7], [24, 4, 85], [95], [2, 55, 4, 7, 91], [5, 31, 1]] print("The list is :") print(my_list) my_key ... Read More

Python Program to print a specific number of rows with Maximum Sum

AmitDiwan

AmitDiwan

Updated on 14-Sep-2021 10:37:55

198 Views

When it is required to print a specific number of rows with the maximum sum, the ‘sorted’ method, and the ‘lambda’ method are used.ExampleBelow is a demonstration of the samemy_list = [[2, 4, 6, 7], [2, 4, 8], [45], [1, 3, 5, 6], [8, 2, 1]] print("The list is ... Read More

Python – Sort Matrix by Palindrome count

AmitDiwan

AmitDiwan

Updated on 14-Sep-2021 10:36:50

218 Views

When it is required to sort matrix based on palindrome count, a method is defined that takes a list as parameter. It uses the list comprehension and ‘join’ method to iterate and see if an element is a palindrome or not. Based on this, results are determined and displayed.ExampleBelow is ... Read More

Python – Extract rows with Complex data types

AmitDiwan

AmitDiwan

Updated on 14-Sep-2021 10:34:58

137 Views

When it is required to extract rows with complex data types, the ‘isinstance’ method and list comprehension are used.ExampleBelow is a demonstration of the samemy_list = [[13, 1, 35], [23, [44, 54], 85], [66], [75, (81, 2), 29, 7]] my_result = [row for row in my_list if any(isinstance(element, list) ... Read More

Python Program to return the Length of the Longest Word from the List of Words

AmitDiwan

AmitDiwan

Updated on 14-Sep-2021 10:33:19

1K+ Views

When it is required to return the length of the longest word from a list of words, a method is defined that takes a list as parameter. It checks if an element is in the list and depending on this, the output is displayed.ExampleBelow is a demonstration of the samedef ... Read More

Python Program to Sort Matrix Rows by summation of consecutive difference of elements

AmitDiwan

AmitDiwan

Updated on 14-Sep-2021 10:31:43

190 Views

ExampleBelow is a demonstration of the samedef diff_summation_elem(row): return sum([abs(row[index + 1] - row[index]) for index in range(0, len(row) - 1)]) my_list = [[97, 6, 47, 3], [6, 88, 3, 26], [71, 53, 34, 65], [15, 36, 5, 62]] print("The list is : ") print(my_list) ... Read More

Python – Append given number with every element of the list

AmitDiwan

AmitDiwan

Updated on 14-Sep-2021 10:28:01

336 Views

When it is required to append given number with every element of the list, a list comprehension is used.ExampleBelow is a demonstration of the samemy_list = [25, 36, 75, 36, 17, 7, 8, 0] print ("The list is :") print(my_list) my_key = 6 my_result = [x + ... Read More

Python – Cumulative Row Frequencies in a List

AmitDiwan

AmitDiwan

Updated on 14-Sep-2021 10:26:49

299 Views

When it is required to get the cumulative row frequencies in a list, the ‘Counter’ method, and a list comprehension are used.ExampleBelow is a demonstration of the samefrom collections import Counter my_list = [[11, 2, 32, 4, 31], [52, 52, 3, 71, 71, 3], [1, 3], [19, 19, 40, ... Read More

Python Program To Get Minimum Element For String Construction

AmitDiwan

AmitDiwan

Updated on 14-Sep-2021 10:19:58

127 Views

When it is required to get the minimum element to construct a string, the ‘set’ operator, the ‘combinations’ method, the ‘issubset’ method and a simple iteration is required.ExampleBelow is a demonstration of the samefrom itertools import combinations my_list = ["python", "is", "fun", "to", "learn"] print("The list is :") ... Read More

Python - Summing all the rows of a Pandas Dataframe

AmitDiwan

AmitDiwan

Updated on 14-Sep-2021 08:24:49

17K+ Views

To sum all the rows of a DataFrame, use the sum() function and set the axis value as 1. The value axis 1 will add the row values.At first, let us create a DataFrame. We have Opening and Closing Stock columns in itdataFrame = pd.DataFrame({"Opening_Stock": [300, 700, 1200, 1500], "Closing_Stock": ... Read More

Advertisements