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
Server Side Programming Articles
Page 350 of 2109
Python – Test String in Character List and vice-versa
When checking if a string exists within a character list or vice-versa, Python provides several approaches using the in operator and join() method. This is useful for string validation and pattern matching. Testing String in Character List You can check if all characters of a string exist in a character list by joining the list and using the in operator ? my_string = 'python' print("The string is:") print(my_string) my_key = ['p', 'y', 't', 'h', 'o', 'n', 't', 'e', 's', 't'] print("The character list is:") print(my_key) joined_list = ''.join(my_key) print("Joined string:", joined_list) my_result = ...
Read MorePython – Test for all Even elements in the List for the given Range
When it is required to test all even elements in the list for the given range, a simple iteration and the modulus operator is used. Example Below is a demonstration of the same − my_list = [32, 12, 42, 61, 58, 60, 19, 16] print("The list is :") print(my_list) i, j = 2, 7 my_result = True for index in range(i, j + 1): if my_list[index] % 2: my_result = False break ...
Read MorePython – Sort Matrix by Maximum String Length
When working with matrices (lists of lists) containing strings, you might need to sort rows based on the maximum string length in each row. Python provides an elegant solution using the sort() method with a custom key function. Example Here's how to sort a matrix by the maximum string length in each row ? def max_length(row): return max([len(element) for element in row]) my_matrix = [['pyt', 'fun'], ['python'], ['py', 'cool'], ['py', 'ea']] print("The matrix is:") print(my_matrix) my_matrix.sort(key=max_length) print("The result is:") print(my_matrix) The matrix is: [['pyt', 'fun'], ...
Read MorePython – 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 More