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
Server Side Programming Articles
Page 320 of 2109
Python – Replace value by Kth index value in Dictionary List
Sometimes you need to replace list values in dictionaries with specific elements from those lists. This technique uses isinstance() to identify list values and replaces them with the element at the Kth index. Example Below is a demonstration of replacing list values with their Kth index elements − my_list = [{'python': [5, 7, 9, 1], 'is': 8, 'good': 10}, {'python': 1, 'for': 10, 'fun': 9}, {'cool': 3, 'python': [7, 3, 9, 1]}] print("The ...
Read MorePython – Summation of consecutive elements power
When it is required to add the consecutive elements power, an 'if' condition and a simple iteration along with the '**' operator are used. This technique calculates the sum of each element raised to the power of its consecutive frequency count. Example Below is a demonstration of the same ? my_list = [21, 21, 23, 23, 45, 45, 45, 56, 56, 67] print("The list is :") print(my_list) my_freq = 1 my_result = 0 for index in range(0, len(my_list) - 1): if my_list[index] != my_list[index + 1]: ...
Read MorePython program to find the group sum till each K in a list
When you need to find the cumulative sum of elements in groups separated by a specific key value K, you can use a simple iteration approach. This technique sums elements between occurrences of K and includes K itself in the result. Example Below is a demonstration of finding group sums separated by key value K ? my_list = [21, 4, 37, 46, 7, 56, 7, 69, 2, 86, 1] print("The list is :") print(my_list) my_key = 46 print("The key is") print(my_key) my_sum = 0 my_result = [] for ele in my_list: ...
Read MorePython program to find Non-K distant elements
When it is required to find non 'K' distant elements, a simple iteration along with the 'append' method is used. Non-K distant elements are those numbers in a list that do not have any other number at exactly K distance from them. What are Non-K Distant Elements? An element is considered non-K distant if neither (element + K) nor (element - K) exists in the same list. For example, with K=2, the number 91 is non-K distant if neither 89 nor 93 exists in the list. Example Below is a demonstration of finding non-K distant elements ...
Read MorePython – Find Product of Index Value and find the Summation
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 MorePython – Sort Matrix by K Sized Subarray Maximum Sum
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 MorePython - How to Group Pandas DataFrame by Minutes?
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 MorePython – Extract element from a list succeeded by K
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 MorePython – Test if list is Palindrome
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 MorePython – Row with Minimum difference in extreme values
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