Programming Articles - Page 1088 of 3363

Python – Rows with K string in Matrix

AmitDiwan
Updated on 08-Sep-2021 09:22:27

179 Views

When it is required to find rows, which have ‘K’ string in matrix, the ‘enumerate’ attribute, a simple iteration and ‘append’ method is used.ExampleBelow is a demonstration of the same −my_list = [["Pyt", "fun", "python"], ["python", "rock"], ["Pyt", "for", "CS"], ["Keep", "learning"]] print("The list is :") print(my_list) K = "Pyt" my_result = [] for idx, element in enumerate(my_list):    if K in element:       my_result.append(idx) print("The result is :") print(my_result)OutputThe list is : [['Pyt', 'fun', 'python'], ['python', 'rock'], ['Pyt', 'for', 'CS'], ['Keep', 'learning']] The result is : [0, 2]ExplanationA list is defined ... Read More

Python - Merge a Matrix by the Elements of First Column

AmitDiwan
Updated on 08-Sep-2021 09:20:12

338 Views

When it is required to merge a matrix by the elements of first column, a simple iteration and list comprehension and ‘setdefault’ method is used.ExampleBelow is a demonstration of the same −my_list = [[41, "python"], [13, "pyt"], [41, "is"], [4, "always"], [3, "fun"]] print("The list is :") print(my_list) my_result = {} for key, value in my_list:    my_result.setdefault(key, []).append(value) my_result = [[key] + value for key, value in my_result.items()] print("The result is :") print(my_result)OutputThe list is : [[41, 'python'], [13, 'pyt'], [41, 'is'], [4, 'always'], [3, 'fun']] The result is : [[41, 'python', 'is'], [13, ... Read More

Python – Concatenate Strings in the Given Order

AmitDiwan
Updated on 08-Sep-2021 09:18:28

347 Views

When it is required to concatenate strings in a given order, a simple iteration is used.ExampleBelow is a demonstration of the same −my_list = ["pyt", "fun", "for", "learning"] print("The list is :") print(my_list) sort_order = [1, 0, 3, 2] my_result = '' for element in sort_order:    my_result += my_list[element] print("The result is :") print(my_result)OutputThe list is : ['pyt', 'fun', 'for', 'learning'] The result is : funpytlearningforExplanationA list is defined and displayed on the console.Another list of integers is defined, which is the order of sorting.An empty string is created.The sort order list is ... Read More

Python Program to remove elements that are less than K difference away in a list

AmitDiwan
Updated on 08-Sep-2021 09:16:17

162 Views

When it is required to remove elements that are less than K difference away in a list, a simple iteration and ‘if’ condition is used.ExampleBelow is a demonstration of the same −my_list = [13, 29, 24, 18, 40, 15] print("The list is :") print(my_list) K = 3 my_list = sorted(my_list) index = 0 while index < len(my_list) - 1:    if my_list[index] + K > my_list[index + 1]:       del my_list[index + 1]    else:       index += 1 print("The result is :") print(my_list)OutputThe list is : [13, 29, 24, ... Read More

Python – Append List every Nth index

AmitDiwan
Updated on 08-Sep-2021 09:13:57

615 Views

When it is required to append list every ‘N’th index, a simple iteration and the ‘enumerate’ attribute are used.ExampleBelow is a demonstration of the same −my_list = [13, 27, 48, 12, 21, 45, 28, 19, 63] print("The list is :") print(my_list) append_list = ['P', 'Y', 'T'] N = 3 print("The value of N is ") print(N) my_result = [] for index, element in enumerate(my_list):    if index % N == 0:       for element_in in append_list:          my_result.append(element_in)    my_result.append(element) print("The result is :") print(my_result)OutputThe list is : [13, ... Read More

Python – Filter Tuples Product greater than K

AmitDiwan
Updated on 08-Sep-2021 09:11:55

213 Views

When it is required to filter tuples product greater than K, a list comprehension is used.ExampleBelow is a demonstration of the same −def tuples_product(index):    my_result = 1    for element in index:       my_result *= element    return my_result my_list = [(14, 25, 17), (2, 3, 5), (81, 42, 21), (6, 2, 1)] print("The list is :") print(my_list) K = 15 print("The value of K is :") print(K) my_result = [index for index in my_list if tuples_product(index) > K] print("The result is :") print(my_result)OutputThe list is : [(14, 25, 17), (2, 3, ... Read More

Python – Adjacent elements in List

AmitDiwan
Updated on 08-Sep-2021 09:10:04

2K+ Views

When it is required to display adjacent elements in list, a method is defined that uses enumerate and a simple iteration to determine the result.ExampleBelow is a demonstration of the same −def find_adjacent_elements(my_list):    my_result = []    for index, ele in enumerate(my_list):       if index == 0:          my_result.append((None, my_list[index + 1]))       elif index == len(my_list) - 1:          my_result.append((my_list[index - 1], None))       else:          my_result.append((my_list[index - 1], my_list[index + 1]))    return my_result my_list = [13, 37, 58, 12, 41, ... Read More

Python – Sort by Units Digit in a List

AmitDiwan
Updated on 08-Sep-2021 09:08:08

351 Views

When it is required to sort by units digit in a list, a method is defined that takes one parameter and uses ‘str’ and negative indexing to determine the output.ExampleBelow is a demonstration of the same −def unit_sort(element):    return str(element)[-1] my_list = [716, 134, 343, 24742] print("The list is :") print(my_list) my_list.sort(key=unit_sort) print("The result is :") print(my_list)OutputThe list is : [716, 134, 343, 24742] The result is : [24742, 343, 134, 716]ExplanationA method named ‘unit_sort’ is defined that takes an element of list as a parameter, and returns the last element after converting it to ... Read More

Python – Remove rows with Numbers

AmitDiwan
Updated on 08-Sep-2021 09:05:17

309 Views

When it is required to remove rows with numbers, a list comprehension and the ‘not’ and ‘any’ operators are used.ExampleBelow is a demonstration of the same −my_list = [[14, 'Pyt', 'fun'], ['Pyt', 'is', 'best'], [23, 51], ['Pyt', 'fun']] print("The list is :") print(my_list) my_result = [index for index in my_list if not any(isinstance(element, int) for element in index)] print("The result is :") print(my_result)OutputThe list is : [[14, 'Pyt', 'fun'], ['Pyt', 'is', 'best'], [23, 51], ['Pyt', 'fun']] The result is : [['Pyt', 'is', 'best'], ['Pyt', 'fun']]ExplanationA list of list is defined and displayed on the console.A list comprehension ... Read More

Python - Sort Matrix by Maximum Row element

AmitDiwan
Updated on 08-Sep-2021 09:03:19

217 Views

When it is required to sort matrix by maximum row element, a method is defined that takes one parameter and uses ‘max’ method to determine the result.ExampleBelow is a demonstration of the same −def sort_max(row):    return max(row) my_list = [[15, 27, 18], [39, 20, 13], [13, 15, 56], [43, 13, 25]] print("The list is :") print(my_list) my_list.sort(key = sort_max, reverse = True) print("The result is :") print(my_list)OutputThe list is : [[15, 27, 18], [39, 20, 13], [13, 15, 56], [43, 13, 25]] The result is : [[13, 15, 56], [43, 13, 25], [39, 20, 13], ... Read More

Advertisements