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 354 of 2547
Python – Extract Row with any Boolean True
When working with nested lists or tuples containing Boolean values, you may need to extract only those rows that contain at least one True value. Python's any() function combined with list comprehension provides an elegant solution for this task. Syntax result = [row for row in data if any(row)] Example Here's how to extract rows containing at least one Boolean True ? my_data = [[False, True], [False, False], [True, False, True], [False]] print("The original data is:") print(my_data) my_result = [row for row in my_data if any(element for element in row)] ...
Read MorePython – Sort Tuples by Total digits
When working with tuples containing numeric data, you might need to sort tuples by the total number of digits across all elements. This involves counting digits in each tuple and using that count as the sorting key. Method: Using a Custom Function We can define a function that converts each element to a string, counts the digits, and returns the total ? def count_tuple_digits(row): return sum([len(str(element)) for element in row]) my_tuple = [(32, 14, 65, 723), (13, 26), (12345, ), (137, 234, 314)] print("The original tuple is:") print(my_tuple) my_tuple.sort(key=count_tuple_digits) ...
Read MorePython – Incremental Slice concatenation in String list
When working with string lists, sometimes we need to perform incremental slice concatenation where each string contributes an increasing number of characters. This technique uses list iteration and string slicing to build a concatenated result. Example Here's how to perform incremental slice concatenation ? my_list = ['pyt', 'is', 'all', 'fun'] print("The list is :") print(my_list) my_result = '' for index in range(len(my_list)): my_result += my_list[index][:index + 1] print("The result is :") print(my_result) The output of the above code is ? The list is : ['pyt', ...
Read MorePython – Filter rows with required elements
When it is required to filter rows with required elements, a list comprehension and the all() operator can be used to check if all elements in each row are present in a reference list. Example The following example demonstrates how to filter rows where all elements are present in a check list ? my_list = [[261, 49, 61], [27, 49, 3, 261], [261, 49, 85], [1, 1, 9]] print("The list is :") print(my_list) check_list = [49, 61, 261, 85] my_result = [row for row in my_list if all(element in check_list for element in ...
Read MorePython – Sort by a particular digit count in elements
When it is required to sort by a particular digit count in elements, a method is defined that takes a list element as parameter and uses the count() and str() methods to determine the results. Below is a demonstration of the same − Example def sort_count_digits(element): return str(element).count(str(my_key)) my_list = [4522, 2452, 1233, 2465] print("The list is :") print(my_list) my_key = 2 print("The value of key is") print(my_key) my_list.sort(key=sort_count_digits) print("The result is :") print(my_list) Output The list is : [4522, 2452, 1233, ...
Read MorePython – Extract elements with equal frequency as value
When it is required to extract elements with equal frequency as value, a list comprehension, the count() method and the set() operator are used. This technique finds elements where the number of times they appear in the list equals their actual value. For example, if the number 4 appears exactly 4 times in the list, or 2 appears exactly 2 times, these elements would be extracted. Example Below is a demonstration of extracting elements with equal frequency as value − my_list = [4, 1, 8, 6, 2, 4, 1, 3, 2, 4, 4] print("The ...
Read MorePython – Elements with same index
When working with lists, sometimes you need to find elements whose values match their index positions. Python provides several approaches to accomplish this task using iteration and the enumerate() function. Using enumerate() with Loop The most straightforward approach uses enumerate() to get both index and element, then compares them ? numbers = [33, 1, 2, 45, 41, 13, 6, 9] print("The list is:") print(numbers) result = [] for index, element in enumerate(numbers): if index == element: result.append(element) print("Elements matching their index:") ...
Read MorePython – Elements with factors count less than K
When working with lists of numbers, you may need to filter elements based on their factor count. This tutorial shows how to find elements that have fewer than K factors using list comprehension and the modulus operator. Understanding Factors A factor of a number is any integer that divides the number evenly (with no remainder). For example, the factors of 12 are: 1, 2, 3, 4, 6, and 12. Method: Using List Comprehension We'll create a function to count factors and filter elements accordingly ? def factors(element, K): return len([index for index in range(1, element + 1) if element % index == 0])
Read MorePython – 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 More