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
Python Articles
Page 36 of 855
Python – Trim tuples by K
When it is required to trim tuples based on a K value, a simple iteration and the ‘append’ method is used.Below is a demonstration of the same −Examplemy_list = [(44, 3, 68, 11, 5), (68, 44, 9, 5, 8), (8, 11, 2, 68, 5), (44, 68, 2, 5, 7)] print("The list is :") print(my_list) K = 1 print("The value for K is ") print(K) my_result = [] for element in my_list: list_length = len(element) my_result.append(tuple(list(element)[K: list_length - K])) print("The resultant list is :") print(my_result)OutputThe list is : [(44, 3, 68, 11, 5), (68, ...
Read MorePython – Sort row by K multiples
When it is required to sort a row by multiples of K, a method is defined that uses list comprehension and the modulus operator.Below is a demonstration of the same −Exampledef multiple_sort_val(row): return len([ele for ele in row if ele % K == 0]) my_list = [[11, 44, 7, 11], [7, 5, 44, 11], [11, 6, 35, 44], [92, 92, 5]] print("The list is :") print(my_list) K = 11 print("The value for K is ") print(K) my_list.sort(key=multiple_sort_val) print("The resultant list is :") print(my_list)OutputThe list is : [[11, 44, 7, 11], [7, 5, 44, 11], ...
Read MorePython – Filter Sorted Rows
When it is required to filter sorted rows, a list comprehension and the ‘sorted’ and ‘list’ methods are used.Below is a demonstration of the same −Examplemy_list = [[99, 6, 75, 10], [1, 75, 2, 4, 99], [75, 15, 99, 2], [1, 4, 15, 99]] print("The list is :") print(my_list) my_result = [sub for sub in my_list if sub == list(sorted(sub)) or sub == list(sorted(sub, reverse=True))] print("The resultant list is :") print(my_result) OutputThe list is : [[99, 6, 75, 10], [1, 75, 2, 4, 99], [75, 15, 99, 2], [1, 4, 15, 99]] The resultant list is ...
Read MorePython – K middle elements
When it is required to determine K middle elements, the ‘//’ operator and list slicing is used.Below is a demonstration of the same −Examplemy_list = [34, 56, 12, 67, 88, 99, 0, 1, 21, 11] print("The list is : ") print(my_list) K = 5 print("The value of K is ") print(K) beg_indx = (len(my_list) // 2) - (K // 2) end_indx = (len(my_list) // 2) + (K // 2) my_result = my_list[beg_indx: end_indx + 1] print("The result is : " ) print(my_result)OutputThe list is : [34, 56, 12, 67, 88, 99, 0, 1, 21, 11] ...
Read MorePython – Group Consecutive elements by Sign
When it is required to group consecutive elements by sign, the ‘^’ operator and the simple iteration along with ‘enumerate’ is used.Below is a demonstration of the same −Examplemy_list = [15, -33, 12, 64, 36, -12, -31, -17, -49, 12, 43, 30, -23, -35, 53] print("The list is :") print(my_list) my_result = [[]] for (index, element) in enumerate(my_list): if element ^ my_list[index - 1] < 0: my_result.append([element]) else: my_result[-1].append(element) print("The result is :") print(my_result)OutputThe list is : [15, -33, 12, 64, 36, -12, -31, -17, -49, 12, 43, ...
Read MorePython – Extract Particular data type rows
When it is required to extract particular data type rows, the list comprehension, the ‘isinstance’ method, and the ‘all’ operator are used.Below is a demonstration of the same −Examplemy_list = [[14, 35, "Will"], [12, 26, 17], ["p", "y", "t"], [29, 40, 21]] print("The list is :") print(my_list) my_data_type = int my_result = [row for row in my_list if all(isinstance(element, my_data_type) for element in row)] print("The result is :") print(my_result)OutputThe list is : [[14, 35, 'Will'], [12, 26, 17], ['p', 'y', 't'], [29, 40, 21]] The result is : [[12, 26, 17], [29, 40, 21]]ExplanationA list of ...
Read MorePython – Filter immutable rows representing Dictionary Keys from Matrix
When it is required to filter immutable rows representing dictionary keys from a matrix, a list comprehension and the ‘isinstance’ method can be used.Below is a demonstration of the same −Examplemy_list = [[24, 15, [32, 33, 12]], ["pyt", 8, (14, 54)], [{15:24}, 13, "fun"], [True, "cool"]] print("The list is :") print(my_list) my_result = [row for row in my_list if all(isinstance(element, int) or isinstance(element, bool) or isinstance(element, float) or isinstance(element, tuple) or isinstance(element, str) for element in row)] print("The result is :") print(my_result)OutputThe list is : [[24, 15, [32, 33, 12]], ['pyt', 8, (14, 54)], [{15: 24}, 13, ...
Read MorePython – Consecutive Division in List
When it is required to find the consecutive division in a list, a method is defined that iterates over the elements of the list and uses the ‘/’ operator to determine the result.Below is a demonstration of the same −Exampledef consec_division(my_list): my_result = my_list[0] for idx in range(1, len(my_list)): my_result /= my_list[idx] return my_result my_list = [2200, 500, 100, 50, 20, 5] print("The list is :") print(my_list) my_result = consec_division(my_list) print("The result is :") print(my_result)OutputThe list is : [2200, 500, 100, 50, 20, 5] The result is : ...
Read MorePython Program – Print the count of either peaks or valleys from a list
When it is required to print the count of either peaks or valleys from a list, a simple iteration and a specific condition is placed.Below is a demonstration of the same −Examplemy_list = [11, 12, 24, 12, 36, 17, 28, 63] print("The list is :") print(my_list) my_result = 0 for index in range(1, len(my_list) - 1): if my_list[index + 1] > my_list[index] < my_list[index - 1] or my_list[index + 1] < my_list[index] > my_list[index - 1]: my_result += 1 print("The result is :") print(my_result)OutputThe list is : [11, 12, 24, 12, ...
Read MorePython - Print rows from the matrix that have same element at a given index
When it is required to print the rows from the matrix that have the same element at the given index, a list comprehension and the ‘all’ operator is used.Below is a demonstration of the same −Examplemy_list = [[7745, 6755, 87, 978], [727, 927, 845], [192, 997, 49], [98, 74, 27]] print("The list is :") print(my_list) my_key = 1 print("The key is ") print(my_key) my_result = [element for element in my_list if all(str(i)[my_key] == str(element[0])[my_key] for i in element)] print("The result is :") print(my_result)OutputThe list is : [[7745, 6755, 87, 978], [727, 927, 845], [192, 997, 49], ...
Read More