Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Programming Articles - Page 1099 of 3363
232 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 by using a function.Below is a demonstration of the same −Example Live Demodef 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]] print("The list is :") print(my_list) my_list.sort(key=fetch_greater_freq) print("The resultant list is :") print(my_list)OutputThe list is : [[11, ... Read More
361 Views
When it is required to filter strings within the ASCII range, the ‘ord’ method that helps with Unicode representation and the ‘all’ operator are used.Below is a demonstration of the same −Example Live Demomy_string = "Hope you are well" print("The string is :") print(my_string) my_result = all(ord(c) < 128 for c in my_string) if(my_result == True): print("The string contains ASCII characters") else: print("The string doesn't contain all ASCII characters")OutputThe string is : Hope you are well The string contains ASCII charactersExplanationA string is defined and is displayed on the console.The ‘ord’ method is called on every ... Read More
140 Views
When it is required to remove strings, which have a non-required character, a list comprehension and the ‘any’ operator is used.Below is a demonstration of the same −Example Live Demomy_list = ["python", "is", "fun", "to", "learn"] print("The list is :") print(my_list) my_char_list = ['p', 's', 'l'] print("The character list is :") print(my_char_list) my_result = [sub for sub in my_list if not any(element in sub for element in my_char_list )] print("The resultant list is :") print(my_result)OutputThe list is : ['python', 'is', 'fun', 'to', 'learn'] The character list is : ['p', 's', 'l'] The resultant list is : ... Read More
168 Views
When it is required to extract rows with even length strings, a list comprehension along with the ‘all’ operator and ‘%’ operator is used.Below is a demonstration of the same −Example Live Demomy_list = [["python", "is", "best"], ["best", "good", "python"], ["is", "better"], ["for", "coders"]] print("The list is :") print(my_list) my_result = [row for row in my_list if all(len(element ) % 2 == 0 for element in row)] print("The resultant list is :") print(my_result)OutputThe list is : [['python', 'is', 'best'], ['best', 'good', 'python'], ['is', 'better'], ['for', 'coders']] The resultant list is : [['python', 'is', 'best'], ['best', 'good', 'python'], ['is', ... Read More
181 Views
When it is required to check if rows have similar frequency, the ‘all’ operator, the ‘Counter’ method and a simple iteration is used.Below is a demonstration of the same −Example Live Demofrom 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 similar frequency") else: print("All rows do not have similar frequency")OutputThe list is : [[21, 92, 64, 11, 3], [21, 3, 11, 92, ... Read More
222 Views
When it is required to filter out non-empty rows from a matrix, a simple list comprehension along with the ‘len’ method can be used.Below is a demonstration of the same −Example Live Demomy_list = [[21, 52, 4, 74], [], [7, 8, 4, 1], [], []] print("The list is :") print(my_list) my_result = [row for row in my_list if len(row) > 0] print("The resultant list is :") print(my_result)OutputThe list is : [[21, 52, 4, 74], [], [7, 8, 4, 1], [], []] The resultant list is : [[21, 52, 4, 74], [7, 8, 4, 1]]ExplanationA list of list with ... Read More
486 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 Live Demomy_list = [[11, 24, 84], [24, 55, 11], [7, 11, 9]] print("The list is :") print(my_list) my_result = True for index in range(len(my_list[0])): column = [ele[index] for ele in my_list] if len(list(set(column ))) != len(column ): my_result = False break if(my_result == True): print("All columns are unique") else: ... Read More
205 Views
When it is required to remove characters, which are greater than ‘K’, a simple iteration is used along with the ‘ord’ (Unicode representation) method.Below is a demonstration of the same −Example Live Demomy_list = ["python", "is", "easy", "to", "learn"] print("The list is :") print(my_list) K = 9 print("The value of K is ") print(K) my_result = [] for element in my_list: result_string = '' for sub in element: if (ord(sub) - 97
958 Views
When it is required to check if any list element is present in a tuple or not, a Boolean value and a simple iteration are used.Below is a demonstration of the same −Example Live Demomy_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 : my_result = True break print("The result is :") if(my_result == True): print("The element is present in the ... Read More
231 Views
When it is required to sort rows of a matrix by custom element count, a method is defined that uses the list comprehension and ‘len’ method to find the output.Below is a demonstration of the same −Example Live Demodef 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 :") print(my_list) custom_list = [31, 85, 7] my_list.sort(key=get_count_matrix) print("The resultant list is :") print(my_list)OutputThe list is : [[31, 5, 22, 7], [85, 5], [9, 11, 22], [7, 48]] The ... Read More