Server Side Programming Articles

Page 354 of 2109

Python – Sort Matrix by Number of elements greater than its previous element

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 268 Views

When it is required to sort a matrix based on the number of elements that is greater than the previous element, a list comprehension and the len() method is used with a custom function as the sorting key. Example Here's how to sort a matrix by counting ascending pairs in each row ? def fetch_greater_freq(row): return len([row[idx] for idx in range(0, len(row) - 1) if row[idx] < row[idx + 1]]) my_list = [[11, 3, 25, 99, 10], [5, 3, 25, 4], [77, 11, 5, 3, 77, 77], [11, 3, 25]] ...

Read More

Python – Filter Strings within ASCII range

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 410 Views

When working with text data, you may need to verify if all characters in a string fall within the ASCII range (0-127). Python provides the ord() function to get Unicode values and all() to check conditions across all elements. Understanding ASCII Range ASCII characters have Unicode values from 0 to 127. This includes letters (A-Z, a-z), digits (0-9), punctuation, and control characters. Characters with Unicode values 128 and above are non-ASCII. Method 1: Using all() with ord() The most concise approach uses all() with a generator expression ? my_string = "Hope you are well" ...

Read More

Python – Remove strings with any non-required character

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 178 Views

When working with string filtering in Python, you often need to remove strings that contain unwanted characters. Python provides several approaches to filter out strings containing specific characters using list comprehension with the any() function. Using List Comprehension with any() The any() function returns True if any element in an iterable is True. Combined with not any(), we can filter strings that don't contain unwanted characters ? words = ["python", "is", "fun", "to", "learn"] print("The word list is:") print(words) unwanted_chars = ['p', 's', 'l'] print("The unwanted characters are:") print(unwanted_chars) filtered_words = [word ...

Read More

Python – Extract rows with Even length strings

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 209 Views

When working with nested lists of strings, you may need to extract rows where all strings have even length. This can be accomplished using list comprehension with the all() function and modulus operator %. Syntax result = [row for row in nested_list if all(len(element) % 2 == 0 for element in row)] How It Works The solution uses three key components ? len(element) % 2 == 0 − Checks if string length is even all() − Returns True only if all elements in the row have even length List comprehension − Filters ...

Read More

Python – Test if Rows have Similar frequency

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 215 Views

When checking if rows have similar frequency in a list of lists, you can use Python's Counter class along with the all() function to compare element frequencies across all rows. Example Here's how to test if all rows contain the same elements with identical frequencies ? from collections import Counter my_list = [[21, 92, 64, 11, 3], [21, 3, 11, 92, 64], [64, 92, 21, 3, 11]] print("The list is :") print(my_list) my_result = all(dict(Counter(row)) == dict(Counter(my_list[0])) for row in my_list) if my_result == True: print("All rows have ...

Read More

Python Program that filters out non-empty rows of a matrix

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 266 Views

When working with matrices (lists of lists), you often need to filter out empty rows. Python provides several approaches to remove non-empty rows from a matrix using list comprehension, the filter() function, or loops. Using List Comprehension List comprehension with len() provides a clean and readable solution ? my_matrix = [[21, 52, 4, 74], [], [7, 8, 4, 1], [], [9, 2]] print("The original matrix is:") print(my_matrix) # Filter out empty rows using list comprehension filtered_matrix = [row for row in my_matrix if len(row) > 0] print("The matrix after filtering empty rows:") print(filtered_matrix) ...

Read More

Python – Test if all elements are unique in columns of a Matrix

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 531 Views

When it is required to test if all elements are unique in columns of a matrix, a simple iteration and a list comprehension along with the 'set' operator are used. Below is a demonstration of the same − Example my_matrix = [[11, 24, 84], [24, 55, 11], [7, 11, 9]] print("The matrix is:") print(my_matrix) my_result = True for index in range(len(my_matrix[0])): column = [row[index] for row in my_matrix] if len(list(set(column))) != len(column): ...

Read More

Python – Remove characters greater than K

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 247 Views

When working with strings, you may need to filter out characters based on their position in the alphabet. This technique removes characters that appear after a certain position K in the alphabet using the ord() function to get Unicode values. Understanding the Approach The method compares each character's alphabetical position with K. Since lowercase 'a' has ASCII value 97, we subtract 97 from each character's ASCII value to get its alphabetical position (a=0, b=1, c=2, etc.). Example Here's how to remove characters greater than K from a list of strings ? words = ["python", ...

Read More

Python – Check if any list element is present in Tuple

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 1K+ Views

When it is required to check if any list element is present in a tuple or not, Python provides several approaches. We can use simple iteration, the any() function, or set intersection for efficient checking. Using Simple Iteration This approach uses a loop to check each list element against the tuple ? my_tuple = (14, 35, 27, 99, 23, 89, 11) print("The tuple is :") print(my_tuple) my_list = [16, 27, 88, 99] print("The list is :") print(my_list) my_result = False for element in my_list: if element in my_tuple: ...

Read More

Python Program to sort rows of a matrix by custom element count

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 266 Views

When sorting rows of a matrix based on custom element count, we can define a helper function that counts how many elements from each row match our custom criteria. This approach uses list comprehension with the len() method to determine the sort order. Example Let's sort matrix rows by counting how many elements from each row appear in our custom list ? def get_count_matrix(my_key): return len([element for element in my_key if element in custom_list]) my_list = [[31, 5, 22, 7], [85, 5], [9, 11, 22], [7, 48]] print("The list is ...

Read More
Showing 3531–3540 of 21,090 articles
« Prev 1 352 353 354 355 356 2109 Next »
Advertisements