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
Articles on Trending Technologies
Technical articles with clear explanations and examples
Python program to find Most Frequent Character in a String
When it is required to find the most frequent character in a string, an empty dictionary is created, and the elements in the string are iterated over. When a character is found in the dictionary, it is incremented, else it is assigned to 1. The maximum of the values in the dictionary is found, and assigned to a variable. Using Dictionary to Count Characters Below is a demonstration of the same ? my_string = "Python-Interpreter" print("The string is : ") print(my_string) max_frequency = {} for i in my_string: if i ...
Read MoreHow to do groupby on a multiindex in Pandas?
A MultiIndex DataFrame in Pandas has multiple levels of row or column indices. You can perform groupby operations on specific levels of the MultiIndex using the level parameter or by referencing index names directly. Creating Sample Data Let's create a sample sales dataset to demonstrate groupby operations on MultiIndex ? import pandas as pd # Create sample sales data data = { 'Car': ['BMW', 'Mercedes', 'Lamborgini', 'Audi', 'Mercedes', 'Porsche', 'RollsRoyce', 'BMW'], 'Place': ['Delhi', 'Hyderabad', 'Chandigarh', 'Bangalore', 'Hyderabad', 'Mumbai', 'Mumbai', 'Delhi'], 'UnitsSold': [95, 80, ...
Read MorePython program to find the Decreasing point in a List
When it is required to find the decreasing point in a list, a simple iteration and the break statement are used. The decreasing point is the first index where an element is greater than its next element. Example Below is a demonstration of the same − my_list = [21, 62, 53, 94, 55, 66, 18, 1, 0] print("The list is :") print(my_list) my_result = -1 for index in range(0, len(my_list) - 1): if my_list[index + 1] < my_list[index]: my_result = index ...
Read MorePython – Sort by Rear Character in Strings List
When working with string lists in Python, you may need to sort them based on their last character. Python provides multiple approaches to achieve this using the sort() method with a custom key function. Using a Custom Function Define a function that returns the last character using negative indexing ? def get_rear_position(element): return element[-1] my_list = ['python', 'is', 'fun', 'to', 'learn'] print("The list is :") print(my_list) my_list.sort(key=get_rear_position) print("The result is :") print(my_list) The list is : ['python', 'is', 'fun', 'to', 'learn'] The result is : ...
Read MorePython – Sort Matrix by None frequency
When it is required to sort a matrix by None frequency, we can define a helper function that counts None values in each row using list comprehension and the not operator. The matrix is then sorted based on the count of None values in ascending order. Example Below is a demonstration of sorting a matrix by None frequency − def get_None_freq(row): return len([element for element in row if not element]) my_list = [[None, 24], [None, 33, 3, None], [42, 24, 55], [13, None, 24]] print("The list is:") print(my_list) my_list.sort(key=get_None_freq) ...
Read MorePython – Extract range of Consecutive similar elements ranges from string list
When working with lists containing consecutive similar elements, you often need to extract ranges showing where each group of identical elements starts and ends. Python provides a simple approach using iteration and the append() method to identify these consecutive groups. Example Below is a demonstration of extracting consecutive similar element ranges ? my_list = [12, 23, 23, 23, 48, 48, 36, 17, 17] print("The list is:") print(my_list) my_result = [] index = 0 while index < (len(my_list)): start_position = index val = my_list[index] ...
Read MorePython – Filter Tuples with Strings of specific characters
When it is required to filter tuples with strings that have specific characters, a list comprehension and the all() function can be used to check if all characters in each string exist within a given character set. Example Below is a demonstration of filtering tuples containing only strings whose characters are present in a specific character set − my_list = [('pyt', 'best'), ('pyt', 'good'), ('fest', 'pyt')] print("The list is :") print(my_list) char_string = 'pyestb' my_result = [index for index in my_list if all(all(sub in char_string for sub in element) for element in index)] ...
Read MorePython – Filter rows with Elements as Multiple of K
When working with nested lists in Python, you might need to filter rows where all elements are multiples of a specific value K. This can be achieved using list comprehension combined with the all() function and modulus operator. Syntax The general syntax for filtering rows with elements as multiples of K is − result = [row for row in nested_list if all(element % K == 0 for element in row)] Example Below is a demonstration of filtering rows where all elements are multiples of K − my_list = [[15, 10, 25], ...
Read MorePython – Character indices Mapping in String List
When working with string lists, you may need to map each character to the indices where it appears. Python provides an efficient solution using defaultdict from the collections module combined with enumeration and set operations. Syntax from collections import defaultdict result = defaultdict(set) for index, string in enumerate(string_list): for char in string.split(): result[char].add(index + 1) Example Below is a demonstration that maps each character to its string positions ? from collections import defaultdict my_list = ['p y t ...
Read MorePython – Extract dictionaries with values sum greater than K
Sometimes we need to filter a list of dictionaries based on the sum of their values. This is useful when working with data where you want to find records whose total values exceed a certain threshold. Using Loop Iteration The most straightforward approach is to iterate through each dictionary and calculate the sum of its values ? student_scores = [ {"Math": 14, "Science": 18, "English": 19}, {"Math": 12, "Science": 4, "English": 16}, {"Math": 13, "Science": 17, "English": 11}, {"Math": 13, ...
Read More