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 – Check if Splits are equal
When you need to check if all parts of a split string are equal, you can use the set() function along with split(). This approach converts the split result to a set to get unique elements, then checks if only one unique element exists. Example Below is a demonstration of checking if splits are equal ? my_string = '96%96%96%96%96%96' print("The string is :") print(my_string) my_split_char = "%" print("The character on which the string should be split is :") print(my_split_char) my_result = len(set(my_string.split(my_split_char))) == 1 print("The resultant check is :") if my_result: ...
Read MorePython – Grouped Consecutive Range Indices of Elements
Sometimes we need to group consecutive occurrences of elements in a list and track their index ranges. Python provides an efficient approach using defaultdict and groupby from the itertools module to identify consecutive elements and map their start and end indices. Understanding Grouped Consecutive Range Indices When elements appear consecutively in a list, we can group them and track their index ranges. For example, in the list [1, 1, 2, 3, 3, 3], element 1 appears at indices 0-1, element 2 at index 2, and element 3 at indices 3-5. Example with Consecutive Elements Let's create ...
Read MorePython – Split Strings on Prefix Occurrence
When we need to split a list of strings based on the occurrence of a specific prefix, we can use itertools.zip_longest() to iterate through the list and look ahead to the next element. This technique groups elements into sublists whenever a prefix match is found. Example Below is a demonstration of splitting strings on prefix occurrence − from itertools import zip_longest my_list = ["hi", 'hello', 'there', "python", "object", "oriented", "object", "cool", "language", 'py', 'extension', 'bjarne'] print("The list is:") print(my_list) my_prefix = "python" print("The prefix is:") print(my_prefix) my_result, my_temp_val = [], [] ...
Read MorePython – Extract Percentages from String
When extracting percentages from a string, we use Python's regular expressions module (re) with the findall() method to locate and extract percentage values. Basic Example with Actual Percentages Let's extract percentage values from a string containing actual numeric percentages ? import re my_string = 'The success rate is 85% and failure rate is 15% with 5% margin error' print("Original string:") print(my_string) # Extract percentages using regex percentages = re.findall(r'\d+%', my_string) print("Extracted percentages:") print(percentages) Original string: The success rate is 85% and failure rate is 15% with 5% margin error ...
Read MorePython – Filter tuple with all same elements
When it is required to filter out tuples that contain only identical elements, a list comprehension combined with the set function and len method can be used. This approach leverages the fact that a set of identical elements has length 1. How It Works The filtering logic works by converting each tuple to a set. Since sets contain only unique elements, a tuple with all identical elements will produce a set of length 1. Example Below is a demonstration of filtering tuples with all same elements ? my_list = [(31, 54, 45, 11, 99), ...
Read MorePython – Convert Rear column of a Multi-sized Matrix
When working with multi-sized matrices (lists containing sublists of different lengths), you may need to extract the last element from each row. This can be accomplished using negative indexing with [-1] to access the rear column efficiently. Extracting Rear Column Elements Here's how to extract the last element from each sublist in a multi-sized matrix ? # Multi-sized matrix with different row lengths matrix = [[41, 65, 25], [45, 89], [12, 65, 75, 36, 58], [49, 12, 36, 98], [47, 69, 78]] print("Original matrix:") print(matrix) # Extract rear column (last elements) rear_column = [] ...
Read MorePython – Maximum of K element in other list
Sometimes we need to find the maximum element from one list where the corresponding elements in another list match a specific value K. This can be achieved using iteration, the append() method, and the max() function. Example Below is a demonstration of finding the maximum element from the first list where corresponding elements in the second list equal K ? my_list_1 = [62, 25, 32, 98, 75, 12, 46, 53] my_list_2 = [91, 42, 48, 76, 23, 17, 42, 83] print("The first list is:") print(my_list_1) print("The first list after sorting is:") my_list_1.sort() print(my_list_1) ...
Read MorePython – Split Numeric String into K digit integers
When working with numeric strings, you may need to split them into equal-sized chunks and convert each chunk to an integer. This can be achieved using simple iteration, slicing, and the int() method. Example Below is a demonstration of splitting a numeric string into K-digit integers ? my_string = '69426874124863145' print("The string is :") print(my_string) K = 4 print("The value of K is") print(K) my_result = [] for index in range(0, len(my_string), K): my_result.append(int(my_string[index : index + K])) print("The resultant list is :") print(my_result) print("The resultant list ...
Read MorePython – Strings with all given List characters
When working with strings and lists in Python, you might need to find strings that contain all characters from a given list. This is useful for filtering data or validating input based on character requirements. Method 1: Using set() and issubset() This approach converts both the required characters and string characters to sets, then checks if all required characters are present ? def has_all_characters(text, required_chars): return set(required_chars).issubset(set(text)) # Test with different strings required = ['a', 'b', 'c'] test_strings = ["abc", "abcdef", "xyz", "cab"] print("Required characters:", required) print("Testing strings:") for ...
Read MorePython – Filter consecutive elements Tuples
When working with tuples in Python, you may need to filter out only those tuples that contain consecutive elements. This involves checking if each element in a tuple is exactly one more than the previous element. Understanding Consecutive Elements Consecutive elements are numbers that follow each other in sequence with a difference of 1. For example, (23, 24, 25, 26) contains consecutive elements, while (65, 66, 78, 29) does not because 78 is not 67. Method to Check Consecutive Elements We can create a function that iterates through a tuple and compares each element with the ...
Read More