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 333 of 2547
Python Program to get indices of sign change in a list
When it is required to get indices of sign change in a list, a simple iteration along with append method can be used. A sign change occurs when consecutive numbers have different signs (positive to negative or negative to positive). Example Below is a demonstration of finding sign change indices ? my_list = [71, 24, -34, -25, -76, 87, 29, -60, 70, 8] print("The list is :") print(my_list) my_result = [] for index in range(0, len(my_list) - 1): # Check for sign change between current and next element ...
Read MorePython - Restrict Tuples by frequency of first element's value
When working with lists of tuples, you may need to restrict tuples based on how frequently their first element appears. This technique is useful for filtering duplicates or limiting occurrences to a specific threshold. Example Below is a demonstration of restricting tuples by frequency of the first element ? my_list = [(21, 24), (13, 42), (11, 23), (32, 43), (25, 56), (73, 84), (91, 40), (40, 83), (13, 27)] print("The list is :") print(my_list) my_key = 1 my_result = [] mems = dict() for sub in my_list: if sub[0] not in mems.keys(): mems[sub[0]] = 1 else: mems[sub[0]] += 1 if mems[sub[0]]
Read MorePython - Find starting index of all Nested Lists
When working with nested lists, you might need to find the starting index of each sublist if all elements were flattened into a single list. This is useful for indexing and data processing tasks. Understanding the Problem Given a nested list like [[51], [91, 22, 36, 44], [25, 25]], if we flatten it to [51, 91, 22, 36, 44, 25, 25], we want to know where each original sublist starts: [0, 1, 5]. Method 1: Using Simple Iteration Track the cumulative length as we iterate through each sublist ? my_list = [[51], [91, 22, ...
Read MorePython - Check if list contain particular digits
When you need to check if all numbers in a list are composed only of specific digits, you can use string operations and set comparison. This is useful for validating numbers that should only contain certain digits. Example Below is a demonstration of checking if list elements contain only particular digits − numbers = [415, 133, 145, 451, 154] print("The list is:") print(numbers) allowed_digits = [1, 4, 5, 3] # Convert allowed digits to string set for faster lookup digit_string = ''.join([str(digit) for digit in allowed_digits]) all_numbers = ''.join([str(num) for num in numbers]) ...
Read MorePython - Total equal pairs in List
When it is required to find the total equal pairs in a list, we can use the set() function along with the floor division operator // and iteration. This approach counts how many pairs can be formed from duplicate elements in the list. Example Below is a demonstration of finding equal pairs in a list − numbers = [34, 56, 12, 32, 78, 99, 67, 34, 52, 78, 99, 10, 0, 11, 23, 9] print("The list is :") print(numbers) unique_elements = set(numbers) total_pairs = 0 for element in unique_elements: total_pairs ...
Read MorePython program to return rows that have element at a specified index
When it is required to return rows that have an element at a specified index, a simple iteration and the append() function can be used. This technique is useful for filtering rows based on matching elements at specific positions. Basic Example Below is a demonstration of comparing elements at a specific index ? my_list_1 = [[21, 81, 35], [91, 14, 0], [64, 61, 42]] my_list_2 = [[21, 92, 63], [80, 19, 65], [54, 65, 36]] print("The first list is :") print(my_list_1) print("The second list is :") print(my_list_2) my_key = 0 my_result = [] ...
Read MorePython Program to find the Next Nearest element in a Matrix
Finding the next nearest element in a matrix involves searching for a specific value that appears after a given position. This is useful in applications like pathfinding, data analysis, and grid-based algorithms where you need to locate the next occurrence of a value. Algorithm The approach starts from a given position (x, y) and searches row by row for the target element. It first checks the remaining elements in the current row, then moves to subsequent rows. Example Below is a demonstration of finding the next nearest element ? def get_nearest_elem(matrix, x, y, target): ...
Read MorePython program to find the redundancy rates for each row of a matrix
When it is required to find the redundancy rates for every row of a matrix, a simple iteration and the append method can be used. The redundancy rate represents how many duplicate values exist in each row, calculated as 1 - (unique_elements / total_elements). Understanding Redundancy Rate The redundancy rate formula is ? redundancy_rate = 1 - (number_of_unique_elements / total_elements) A redundancy rate of 0 means all elements are unique, while 1 means all elements are identical. Example Below is a demonstration of finding redundancy rates for each row ? ...
Read MorePython - Change the signs of elements of tuples in a list
When it is required to change the signs of elements in a list of tuples, you can use iteration along with the abs() method to manipulate positive and negative values. This technique is useful for standardizing tuple formats or data preprocessing. Example Below is a demonstration of changing tuple elements to have positive first elements and negative second elements − my_list = [(51, -11), (-24, -24), (11, 42), (-12, 45), (-45, 26), (-97, -4)] print("The list is :") print(my_list) my_result = [] for sub in my_list: my_result.append((abs(sub[0]), -abs(sub[1]))) print("The ...
Read MorePython program to convert a list to a set based on a common element
When you need to group sublists that share common elements, you can convert them to sets and merge those with overlapping elements. This technique uses the union method to combine sets and recursion to handle multiple merges. Example Below is a demonstration of merging sublists based on common elements − def common_elem_set(my_set): for index, val in enumerate(my_set): for j, k in enumerate(my_set[index + 1:], index + 1): if val & k: ...
Read More