AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 90 of 840

Python – Find Product of Index Value and find the Summation

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 201 Views

When working with lists in Python, you might need to calculate the product of each element with its position (index) and then find the sum of all these products. The enumerate() function is perfect for this task as it provides both the index and value during iteration. Example Below is a demonstration of finding the product of index value and summation ? my_list = [71, 23, 53, 94, 85, 26, 0, 8] print("The list is :") print(my_list) my_result = 0 for index, element in enumerate(my_list): my_result += (index + ...

Read More

Python – Sort Matrix by K Sized Subarray Maximum Sum

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 284 Views

When working with matrices, sometimes we need to sort rows based on the maximum sum of K-sized subarrays within each row. This technique is useful in data analysis and algorithm problems where we want to prioritize rows based on their highest consecutive sum. Understanding the Problem For each row in the matrix, we need to: Find all possible K-sized subarrays Calculate the sum of each subarray Take the maximum sum as the sorting key Example Let's implement a function to sort a matrix by K-sized subarray maximum sum ? def sort_matrix_K(row): ...

Read More

Python - How to Group Pandas DataFrame by Minutes?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 3K+ Views

Grouping a Pandas DataFrame by minutes is useful for time-series analysis. We can use pd.Grouper() with the freq parameter to specify minute intervals and aggregate data within those time windows. Creating a Sample DataFrame with Timestamps Let's create a DataFrame with car sales data that includes timestamps ? import pandas as pd # Create DataFrame with timestamp data dataFrame = pd.DataFrame( { "Car": ["Audi", "Lexus", "Tesla", "Mercedes", "BMW", "Toyota", "Nissan", "Bentley", "Mustang"], "Date_of_Purchase": [ ...

Read More

Python – Extract element from a list succeeded by K

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 273 Views

When it is required to extract elements from a list that are succeeded by K (elements that come before K), we can use simple iteration with the append method or list comprehension. Method 1: Using Loop Iterate through the list and check if the next element equals K − numbers = [45, 65, 32, 78, 99, 10, 21, 2] print("The list is:") print(numbers) K = 99 print("The value of K is:") print(K) result = [] for i in range(len(numbers) - 1): if numbers[i + 1] == K: ...

Read More

Python – Test if list is Palindrome

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 2K+ Views

A palindrome is a sequence that reads the same forwards and backwards. To test if a Python list is a palindrome, we can compare it with its reversed version using slice notation [::-1]. Method 1: Direct List Comparison The simplest approach is to compare the original list with its reverse ? def is_palindrome_list(data): return data == data[::-1] # Test with palindrome list numbers = [1, 2, 3, 2, 1] print("List:", numbers) print("Is palindrome:", is_palindrome_list(numbers)) # Test with non-palindrome list numbers2 = [1, 2, 3, 4, 5] print("List:", numbers2) print("Is palindrome:", ...

Read More

Python – Row with Minimum difference in extreme values

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 119 Views

When it is required to get the row with minimum difference in extreme values, list comprehension, the min() method and max() methods are used. Example Below is a demonstration of the same − my_list = [[41, 1, 38], [25, 33, 1], [13, 44, 65], [1, 22]] print("The list is : ") print(my_list) my_min_val = min([max(elem) - min(elem) for elem in my_list]) my_result = [elem for elem in my_list if max(elem) - min(elem) == my_min_val] print("The result is : ") print(my_result) The output of the above code is − ...

Read More

Python - Filter Pandas DataFrame by Time

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 731 Views

Filtering a Pandas DataFrame by time allows you to extract records that meet specific date or time conditions. Use the loc indexer with datetime comparisons to filter rows based on date ranges. Basic Time Filtering with loc First, create a DataFrame with date information ? import pandas as pd # Create dictionary with car purchase data data = { 'Car': ['BMW', 'Lexus', 'Audi', 'Mercedes', 'Jaguar', 'Bentley'], 'Date_of_Purchase': ['2021-07-10', '2021-08-12', '2021-06-17', '2021-03-16', '2021-05-19', '2021-08-22'] } # Create DataFrame dataFrame = pd.DataFrame(data) print("Original DataFrame:") print(dataFrame) ...

Read More

Python Program to Remove Palindromic Elements from a List

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 408 Views

When it is required to remove palindromic elements from a list, list comprehension and the 'not' operator are used. A palindromic number reads the same forwards and backwards, like 121 or 9. Example Below is a demonstration of removing palindromic elements from a list − my_list = [56, 78, 12, 32, 4, 8, 9, 100, 11] print("The list is:") print(my_list) my_result = [elem for elem in my_list if int(str(elem)[::-1]) not in my_list] print("The result is:") print(my_result) Output The list is: [56, 78, 12, 32, 4, 8, 9, 100, 11] ...

Read More

Python Pandas - Count the number of rows in each group

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 439 Views

Pandas groupby() operations allow you to split data into groups and count rows in each group using size(). This is useful for analyzing data distribution and finding group frequencies. Creating the DataFrame First, let's create a sample DataFrame with product data ? import pandas as pd # Create a DataFrame dataFrame = pd.DataFrame({ 'Product Category': ['Computer', 'Mobile Phone', 'Electronics', 'Electronics', 'Computer', 'Mobile Phone'], 'Quantity': [10, 50, 10, 20, 25, 50], 'Product Name': ['Keyboard', 'Charger', 'SmartTV', 'Camera', 'Graphic Card', 'Earphone'] }) print("DataFrame:") print(dataFrame) ...

Read More

Python – N sized substrings with K distinct characters

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 331 Views

When working with strings, you might need to find all substrings of a specific length that contain exactly K distinct characters. This can be achieved by iterating through the string and using Python's set() method to count unique characters in each substring. Syntax The general approach involves: for i in range(len(string) - n + 1): substring = string[i:i+n] if len(set(substring)) == k: # Add to result Example Below is a demonstration that finds all 2-character substrings with ...

Read More
Showing 891–900 of 8,392 articles
« Prev 1 88 89 90 91 92 840 Next »
Advertisements