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
Python Articles
Page 8 of 854
Python – 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.Below is a demonstration of the same −Exampledef 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)OutputThe list is : [['pyt', 'is', 'fun'], ['python', 'fun'], ['py', '4', 'good'], ['python']] The result is : [['python'], ['py', '4', 'good'], ['pyt', 'is', 'fun'], ['python', 'fun']]ExplanationA method named ’total_characters’ is defined that ...
Read MorePython – Elements with factors count less than K
When it is required to display elements with factors count less than K, a method is defined that takes two parameters and uses list comprehension to iterate over the elements and use ‘modulus’ operator to determine the result.Below is a demonstration of the same −Exampledef factors(element, K): return len([index for index in range(1, element + 1) if element % index == 0])
Read MorePython – Elements with same index
When it is required to display elements with same index, a simple iteration and the ‘enumerate’ attribute is used.Below is a demonstration of the same −Examplemy_list = [33, 1, 2, 45, 41, 13, 6, 9] print("The list is :") print(my_list) my_result = [] for index, element in enumerate(my_list): if index == element: my_result.append(element) print("The result is :") print(my_result)OutputThe list is : [33, 1, 2, 45, 41, 13, 6, 9] The result is : [1, 2, 6]ExplanationA list is defined and displayed on the console.An empty list is defined.The list is iterated over, and ...
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.Below is a demonstration of the same −Examplemy_list = [4, 1, 8, 6, 2, 4, 1, 3, 2, 4, 4] print("The list is :") print(my_list) my_result = list(set([element for element in my_list if my_list.count(element) == element])) print("The result is :") print(my_result)OutputThe list is : [4, 1, 8, 6, 2, 4, 1, 3, 2, 4, 4] The result is : [2, 4]ExplanationA list is defined and displayed on the console.A list comprehension is used to ...
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 list element as parameter and uses the ‘count’ and ‘str’ methods to determine the results.Below is a demonstration of the same −Exampledef sort_count_digits(elements): return str(elements).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)OutputThe list is : [4522, 2452, 1233, 2465] The value of key is 2 The result is : [1233, 2465, 4522, ...
Read MorePython – Filter rows with required elements
When it is required to filter rows with required elements, a list comprehension and the ‘all’ operator is used.Below is a demonstration of the same −Examplemy_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 = [index for index in my_list if all(element in check_list for element in index)] print("The result is :") print(my_result)OutputThe list is : [[261, 49, 61], [27, 49, 3, 261], [261, 49, 85], [1, 1, 9]] The result is : [[261, 49, 61], [261, 49, ...
Read MorePython – Incremental Slice concatenation in String list
When it is required to display incremental slice concatenation in string list, a simple iteration and list slicing is used.Below is a demonstration of the same −Examplemy_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)OutputThe list is : ['pyt', 'is', 'all', 'fun'] The result is : pisallfunExplanationA list is defined and displayed on the console.An empty string is created.The list is iterated over, and the element is concatenated with the consecutive element.This result is assigned to ...
Read MorePython – Sort Tuples by Total digits
When it is required to sort tuples by total digits, a method is defined that converts every element in the list to a string, and gets the length of each of these strings, and adds them up. This is displayed as result of the method.Below is a demonstration of the same −Exampledef 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 tuple is :") print(my_tuple) my_tuple.sort(key = count_tuple_digits) print("The result is :") print(my_tuple)OutputThe tuple is : [(32, 14, 65, 723), (13, 26), (12345, ...
Read MorePython – Extract Row with any Boolean True
When it is required to extract row with any Boolean True, a list comprehension is used along with the ‘any’ operator.Below is a demonstration of the same −Examplemy_tuple = [[False, True], [False, False], [True, False, True], [False]] print("The tuple is :") print(my_tuple) my_result = [row for row in my_tuple if any(element for element in row)] print("The result is ") print(my_result)OutputThe tuple is : [[False, True], [False, False], [True, False, True], [False]] The result is [[False, True], [True, False, True]]ExplanationA list of list is defined and displayed on the console.A list comprehension is used to check if any ...
Read MorePython – Sort Matrix by Maximum String Length
When it is required to sort matrix by maximum string length, a method is defined that takes a list as parameter and uses list comprehension and the ‘max’ and ‘len’ methods to determine the result.Below is a demonstration of the same −Exampledef 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 )OutputThe matrix is : [['pyt', 'fun'], ['python'], ['py', 'cool'], ['py', 'ea']] The result is : [['py', 'ea'], ['pyt', 'fun'], ['py', 'cool'], ['python']]ExplanationA method named ...
Read More