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 – Sort Matrix by total characters
When it is required to sort matrix by total characters, a method is defined that uses list comprehension and the 'sum' and 'len' methods to determine the result. This technique allows you to sort rows based on the combined length of all strings in each row. Example The following example demonstrates sorting a matrix by total characters ? def total_characters(row): return sum([len(element) for element in row]) my_list = [["pyt", "is", "fun"], ["python", "fun"], ["py", "4", "good"], ["python"]] print("The list is :") print(my_list) my_list.sort(key=total_characters) print("The result is :") print(my_list) ...
Read MorePython – Extract list with difference in extreme values greater than K
When you need to filter sublists based on the range between their minimum and maximum values, you can use list comprehension with the min() and max() functions. This technique is useful for finding sublists with high variability in their elements. Example Here's how to extract sublists where the difference between extreme values is greater than K ? my_list = [[13, 52, 11], [94, 12, 21], [23, 45, 23], [11, 16, 21]] print("The list is :") print(my_list) k = 40 my_result = [element for element in my_list if max(element) - min(element) > k] ...
Read MorePython – Negative index of Element in List
Getting the negative index of an element in a list helps you find the position from the end. Python allows negative indexing where -1 refers to the last element, -2 to the second last, and so on. Understanding Negative Indexing In Python, negative indices count backward from the end of the list: 52 47 18 22 ...
Read MorePython – Custom Lower bound a List
When working with numerical data, you may need to set a custom lower bound for a list. This means replacing any values below a threshold with the threshold value itself. Python's list comprehension provides an elegant solution for this task. Syntax result = [element if element >= threshold else threshold for element in original_list] Example Let's apply a lower bound of 50 to a list of integers ? numbers = [51, 71, 86, 21, 11, 35, 67] print("Original list:") print(numbers) threshold = 50 print(f"Lower bound threshold: {threshold}") result = ...
Read MorePython – Remove Elements in K distance with N
When it is required to remove elements which are at K distance with N, a list comprehension along with a specific condition is used. This means removing elements that fall within the range [N-K, N+K]. Understanding K Distance An element is considered at K distance from N if the absolute difference between the element and N is less than or equal to K. In other words, elements in the range [N-K, N+K] are at K distance from N. Elements at K Distance from N ...
Read MorePython – Disjoint Strings across Lists
When we need to find pairs of strings from two lists that share no common characters, we use disjoint string analysis. Two strings are disjoint if they have no characters in common. Understanding Disjoint Strings Disjoint strings are strings that don't share any common characters. For example, "fun" and "its" are disjoint because they have no letters in common. Example Here's how to find all disjoint string pairs from two lists ? from functools import reduce def determine_disjoint_pairs(disjoint_data, my_result=[]): if not disjoint_data and not reduce(lambda a, b: set(a) & ...
Read MorePython – Check if particular value is present corresponding to K key
When working with a list of dictionaries in Python, you often need to check if a particular value exists for a specific key across all dictionaries. This can be efficiently done using list comprehension with the in operator. Example Here's how to check if a value is present corresponding to a specific key ? my_list = [ {"python": "14", "is": "great", "fun": "1"}, {"python": "cool", "is": "fun", "best": "81"}, {"python": "93", "is": "CS", "amazing": "16"} ] print("The list is:") print(my_list) K = ...
Read MoreHow to find the common elements in a Pandas DataFrame?
Finding common elements between Pandas DataFrames is essential for data analysis tasks like identifying shared records or performing data validation. Python provides several methods including merge(), intersection(), and set operations. Using merge() Method The merge() method performs an inner join by default, returning only common rows between DataFrames − import pandas as pd df1 = pd.DataFrame({ "x": [5, 2, 7, 0], "y": [4, 7, 5, 1], "z": [9, 3, 5, 1] }) df2 = pd.DataFrame({ "x": [5, 2, ...
Read MorePython – Sort by Uppercase Frequency
Sorting strings by uppercase frequency means arranging them based on the count of capital letters they contain. This is useful for organizing text data by capitalization patterns. Python's sort() method with a custom key function makes this straightforward. How It Works The approach uses a custom function that counts uppercase letters in each string, then sorts the list using this count as the sorting key ? def count_uppercase(text): return len([char for char in text if char.isupper()]) words = ["pyt", "is", "FUN", "to", "Learn"] print("Original list:") print(words) words.sort(key=count_uppercase) print("Sorted ...
Read MorePython program to extract only the numbers from a list which have some specific digits
When you need to extract numbers from a list that contain only specific digits, you can use list comprehension with the all() function. This technique filters numbers by checking if every digit in each number exists in your allowed digits set. Syntax result = [num for num in numbers if all(int(digit) in allowed_digits for digit in str(num))] Example Let's extract numbers that contain only the digits 2, 3, 4, and 5 ? numbers = [3345, 2345, 1698, 2475, 1932] print("The original list is:") print(numbers) allowed_digits = [2, 3, 4, 5] ...
Read More