Sort Rows by K-Multiples in Python

AmitDiwan
Updated on 04-Sep-2021 11:00:24

160 Views

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 −Example Live Demodef 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, ... Read More

Trim Tuples by k in Python

AmitDiwan
Updated on 04-Sep-2021 10:59:15

205 Views

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 −Example Live Demomy_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), ... Read More

Sort By Range Inclusion in Python

AmitDiwan
Updated on 04-Sep-2021 10:57:59

510 Views

When it is required to sort the list based on range, the ‘abs’ method, the ‘sum’ method and the list comprehension are used using a function.Below is a demonstration of the same −Example Live Demodef sum_range_incl(my_row):    return sum([abs(element [1] - element [0]) for element in my_row if element [0] > i and element [0] < j and element [1] > i and element [1] < j]) my_list = [[(12, 4), (55, 10), (11, 16)], [(42, 14)], [(2, 5), (2, 28), (9, 16)], [(12, 6), (6, 13)]] print("The list is :") print(my_list) i, j = 2, 15 ... Read More

Python Maximum in Row Range

AmitDiwan
Updated on 04-Sep-2021 10:56:34

292 Views

When it is required to find the maximum value in a row range, a simple iteration and the ‘max’ method is used.Below is a demonstration of the same −Example Live Demomy_list = [[11, 35, 6], [9, 11, 3], [35, 4, 2], [8, 15, 35], [5, 9, 18], [5, 14, 2]] print("The list is :") print(my_list) i, j = 2, 4 print("The values for integers are ") print(i, j) my_result = 0 for index in range(i, j):    my_result = max(max(my_list[index]), my_result) print("The result is :") print(my_result)OutputThe list is : [[11, 35, 6], [9, 11, 3], [35, ... Read More

Sort Matrix by Number of Elements Greater than Previous Element in Python

AmitDiwan
Updated on 04-Sep-2021 10:55:28

215 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

Filter Strings Within ASCII Range in Python

AmitDiwan
Updated on 04-Sep-2021 10:54:15

345 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

Remove Strings with Non-Required Characters in Python

AmitDiwan
Updated on 04-Sep-2021 10:52:58

130 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

Extract Rows with Even Length Strings in Python

AmitDiwan
Updated on 04-Sep-2021 10:51:48

148 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

Test if Rows Have Similar Frequency in Python

AmitDiwan
Updated on 04-Sep-2021 10:50:50

168 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

Filter Out Non-Empty Rows of a Matrix in Python

AmitDiwan
Updated on 04-Sep-2021 10:49:43

209 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

Advertisements