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
Programming Articles
Page 338 of 2547
Python – Test if tuple list has a single element
When it is required to test if a tuple list contains a single unique element across all tuples, we can use different approaches. This means checking if all elements in all tuples are the same value. Using Nested Loop with Flag The traditional approach uses nested loops with a flag variable to track whether all elements are identical ? my_list = [(72, 72, 72), (72, 72), (72, 72)] print("The list is :") print(my_list) my_result = True for sub in my_list: flag = True for element in ...
Read MorePython – Redistribute Trimmed Values
When you need to redistribute trimmed values, you remove elements from both ends of a list and distribute their sum evenly across the remaining elements. This technique uses list slicing and the division operator. What is Redistributing Trimmed Values? Redistributing trimmed values means: Remove a specified number of elements from both ends of a list Calculate the sum of the removed (trimmed) elements Distribute this sum evenly among the remaining elements Example Below is a demonstration of redistributing trimmed values ? my_list = [11, 26, 24, 75, 96, 37, 48, 29, 93] ...
Read MorePython – Consecutive identical elements count
When working with lists in Python, you might need to count how many distinct values appear consecutively (repeated adjacent elements). This can be achieved using iteration, the append() method, and set() to find unique consecutive elements. Example Here's how to count distinct consecutive identical elements in a list − my_list = [24, 24, 24, 15, 15, 64, 64, 71, 13, 95, 100] print("The list is :") print(my_list) my_result = [] for index in range(0, len(my_list) - 1): if my_list[index] == my_list[index + 1]: ...
Read MorePython – Rows with all List elements
When it is required to find rows that contain all elements from a given list, a flag value, simple iteration and the 'append' method can be used. This is useful for filtering data based on multiple criteria. Example Below is a demonstration of finding rows containing all specified elements ? my_list = [[8, 6, 3, 2], [1, 6], [2, 1, 7], [8, 1, 2]] print("The list is :") print(my_list) sub_list = [1, 2] result = [] for row in my_list: flag = True ...
Read MorePython – Filter rows without Space Strings
When working with nested lists containing strings, you might need to filter out rows that contain any strings with spaces. Python provides several approaches to accomplish this task using list comprehensions and string checking methods. Using Regular Expressions The most robust approach uses the re module to search for whitespace characters ? import re data = [["python is", "fun"], ["python", "good"], ["python is cool"], ["love", "python"]] print("The original list is:") print(data) result = [row for row in data if not any(bool(re.search(r"\s", element)) for element in row)] print("Rows without space strings:") print(result) ...
Read MorePython – Equidistant consecutive characters Strings
Equidistant consecutive character strings are strings where the ASCII difference between consecutive characters remains constant throughout the string. For example, in "abc", each character is 1 ASCII value apart, while in "agms", each character is 6 ASCII values apart. Understanding the Concept To check if a string has equidistant consecutive characters, we need to ? Calculate the ASCII difference between the first two characters Verify that all other consecutive pairs have the same difference Use ord() to get ASCII values and all() to check the condition Example Here's how to find all equidistant ...
Read MorePython – Reform K digit elements
When working with lists of numbers, you may need to reform K digit elements by concatenating all numbers into a string and then splitting them into chunks of K digits. This technique uses string manipulation and list comprehension. Example Below is a demonstration of reforming elements into 3-digit chunks − my_list = [231, 67, 232, 1, 238, 31, 793] print("The list is :") print(my_list) K = 3 print("The value of K is") print(K) # Join all numbers as a single string temp = ''.join([str(ele) for ele in my_list]) print("Combined string:", temp) my_result ...
Read MorePython program to test if all y occur after x in List
When it is required to check if all 'y' occurs after 'x' in a list, the enumerate function along with a specific condition is used to compare indices. Example Below is a demonstration of the same − my_list = [11, 25, 13, 11, 64, 25, 8, 9] print("The list is :") print(my_list) x, y = 13, 8 x_index = my_list.index(x) my_result = True for index, element in enumerate(my_list): if element == y and index < x_index: my_result = False ...
Read MorePython – Test if all rows contain any common element with other Matrix
When working with matrices (lists of lists), you might need to check if all corresponding rows share at least one common element. This can be achieved using iteration with a flag variable to track the result. Example Below is a demonstration of testing row-wise common elements between two matrices − matrix_1 = [[3, 16, 1], [2, 4], [4, 31, 31]] matrix_2 = [[42, 16, 12], [42, 8, 12], [31, 7, 10]] print("The first matrix is :") print(matrix_1) print("The second matrix is :") print(matrix_2) result = True for idx in range(len(matrix_1)): ...
Read MorePython – Reorder for consecutive elements
When working with lists in Python, you may need to reorder consecutive elements by grouping identical values together. This can be achieved using the Counter class from the collections module along with list iteration. What is Reordering for Consecutive Elements? Reordering for consecutive elements means rearranging a list so that all identical values appear together consecutively, while maintaining their original frequency count. Using Counter for Reordering The Counter class counts the frequency of each element, then we can reconstruct the list with grouped elements ? from collections import Counter my_list = [21, 83, ...
Read More