Programming Articles

Page 341 of 2547

Python – Split Strings on Prefix Occurrence

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 490 Views

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 More

Python – Extract Percentages from String

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 515 Views

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 More

Python – Filter tuple with all same elements

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 293 Views

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 More

Python – Convert Rear column of a Multi-sized Matrix

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 148 Views

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 More

Python – Maximum of K element in other list

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 258 Views

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 More

Python – Split Numeric String into K digit integers

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 429 Views

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 More

Python – Strings with all given List characters

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 431 Views

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 More

Python – Filter consecutive elements Tuples

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 344 Views

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

Python – Remove Rows for similar Kth column element

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 176 Views

When working with lists of lists in Python, you may need to remove rows that have duplicate values in a specific column (Kth position). This can be achieved using simple iteration and the append() method to build a new list with unique values. Example Below is a demonstration of removing rows with duplicate Kth column elements − data = [[45, 95, 26], [70, 35, 74], [87, 65, 23], [70, 35, 74], [67, 85, 12], [45, 65, 0]] print("The list is:") print(data) K = 1 print("The value of K is:") print(K) result = [] ...

Read More

Python – Count frequency of sublist in given list

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 526 Views

When it is required to count the frequency of a sub−list in a given list, a list comprehension and the 'len' method along with the 'if' condition are used. Example Below is a demonstration of the same − my_list = [23, 33, 45, 67, 54, 43, 33, 45, 67, 83, 33, 45, 67, 90, 0] print("The list is:") print(my_list) sub_list = [33, 45, 67] print("The sub-list is:") print(sub_list) my_result = len([sub_list for index in range(len(my_list)) if my_list[index : index + len(sub_list)] == sub_list]) print("The frequency count is:") print(my_result) The ...

Read More
Showing 3401–3410 of 25,466 articles
« Prev 1 339 340 341 342 343 2547 Next »
Advertisements