Return Rows with Element at Specified Index in Python

AmitDiwan
Updated on 15-Sep-2021 11:44:08

202 Views

When it is required to return rows that have an element at a specified index, a simple iteration and the ‘append’ function can be used.ExampleBelow is a demonstration of the samemy_list_1 = [[21, 81, 35], [91, 14, 0], [64, 61, 42]] my_list_2 = [[21, 92, 63], [80, 19, 65], [54, 65, 36]] print("The first list is :") print(my_list_1) print("The second list is :") print(my_list_2) my_key = 0 my_result = [] for index in range(len(my_list_1)): if my_list_1[index][my_key] == my_list_2[index][my_key]: my_result.append(my_list_1[index]) my_result.append(my_list_1[index]) print("The ... Read More

Find Next Nearest Element in a Matrix Using Python

AmitDiwan
Updated on 15-Sep-2021 11:42:27

188 Views

When it is required to find the next nearest element in a matrix, a method is defined tat iterates through the list and places a specific condition. This method is called and the results are displayed.ExampleBelow is a demonstration of the samedef get_nearest_elem(my_list, x, y, my_key):    for index, row in enumerate(my_list[x:]):       for j, elem in enumerate(row):          if elem == my_key and j > y: return index + x, j return -1, -1 my_list = ... Read More

Find Redundancy Rates for Each Row of a Matrix in Python

AmitDiwan
Updated on 15-Sep-2021 11:40:56

331 Views

When it is required to find the redundancy rates for every row of a matrix, a simple iteration and the ‘append’ method can be used.ExampleBelow is a demonstration of the samemy_list = [[91, 52, 12, 29, 33], [54, 54, 54, 54, 54], [11, 22, 33, 59, 95]] print("The list is :") print(my_list) my_result = [] for sub in my_list:    my_result.append(1 - len(set(sub)) / len(sub)) print("The result is :") print(my_result)OutputThe list is : [[91, 52, 12, 29, 33], [54, 54, 54, 54, 54], [11, 22, 33, 59, 95]] The result is : [0, 1, 0]ExplanationA list of ... Read More

Change Signs of Elements in Tuples of a List in Python

AmitDiwan
Updated on 15-Sep-2021 11:36:50

373 Views

When it is required to change the signs of the elements in a list of tuple, a simple iteration, the ‘abs’ method and the ‘append’ method can be used.ExampleBelow is a demonstration of the samemy_list = [(51, -11), (-24, -24), (11, 42), (-12, 45), (-45, 26), (-97, -4)] print("The list is :") print(my_list) my_result = [] for sub in my_list: my_result.append((abs(sub[0]), -abs(sub[1]))) print("The result is :") print(my_result)OutputThe list is : [(51, -11), (-24, -24), (11, 42), (-12, 45), (-45, 26), (-97, -4)] The result is : [(51, -11), (24, -24), (11, -42), (12, -45), (45, ... Read More

Convert List to Set Based on Common Element in Python

AmitDiwan
Updated on 15-Sep-2021 11:35:21

141 Views

When it is required to convert a list to a set based on a specific common element, a method can be defined that iterates through the set using ‘enumerate’ and places a specific condition on the elements. The ‘union’ method and the ‘map’ methods are used.ExampleBelow is a demonstration of the samedef common_elem_set(my_set):    for index, val in enumerate(my_set):       for j, k in enumerate(my_set[index + 1:], index + 1): if val & k: my_set[index] = ... Read More

Convert List into List of Lists Using Step Value in Python

AmitDiwan
Updated on 15-Sep-2021 11:33:00

213 Views

When it is required to convert a list into a list of lists using a step value, a method is defined that uses a simple iteration, the ‘split’ method and the ‘append’ method.ExampleBelow is a demonstration of the samedef convert_my_list(my_list): my_result = [] for el in my_list: sub = el.split(', ') my_result.append(sub) return(my_result) my_list = ['peter', 'king', 'charlie'] print("The list is :") print(my_list) print("The resultant list is :") print(convert_my_list(my_list))OutputThe list is : ['peter', 'king', 'charlie'] The ... Read More

Find the Cube of Each List Element in Python

AmitDiwan
Updated on 15-Sep-2021 11:30:55

1K+ Views

When it is required to find the cube of each list element, a simple iteration and the ‘append’ method are used.ExampleBelow is a demonstration of the samemy_list = [45, 31, 22, 48, 59, 99, 0] print("The list is :") print(my_list) my_result = [] for i in my_list: my_result.append(i*i*i) print("The resultant list is :") print(my_result)OutputThe list is : [45, 31, 22, 48, 59, 99, 0] The resultant list is : [91125, 29791, 10648, 110592, 205379, 970299, 0]ExplanationA list is defined and is displayed on the console.An empty list is defined.The original list is iterated over.Every element ... Read More

Print All Words Occurring in a Sentence Exactly K Times

AmitDiwan
Updated on 15-Sep-2021 11:25:55

399 Views

When it is required to print all the words occurring in a sentence exactly K times, a method is defined that uses the ‘split’ method, ‘remove’ method and the ‘count’ methods. The method is called by passing the required parameters and output is displayed.ExampleBelow is a demonstration of the samedef key_freq_words(my_string, K):    my_list = list(my_string.split(" "))    for i in my_list:       if my_list.count(i) == K:          print(i)          my_list.remove(i) my_string = "hi there how are you, how are u" K = 2 print("The string is :") print(my_string) print"The repeated ... Read More

Custom Space Size Padding in Python Strings List

AmitDiwan
Updated on 15-Sep-2021 11:15:59

381 Views

When it is required to customize the space size padding in a list of strings, an empty list, an iteration and the ‘append’ method is used.ExampleBelow is a demonstration of the samemy_list = ["Python", "is", "great"] print("The list is :") print(my_list) lead_size = 3 trail_size = 2 my_result = [] for elem in my_list: my_result.append((lead_size * ' ') + elem + (trail_size * ' ')) print("The result is :") print(my_result)OutputThe list is : ['Python', 'is', 'great'] The result is : [' Python ', ' is ', ' great ']ExplanationA list is defined and ... Read More

First Occurrence of One List in Another in Python

AmitDiwan
Updated on 15-Sep-2021 11:13:46

378 Views

When it is required to find the first occurrence of one list in another list, the ‘set’ attribute and the ‘next’ method is used.ExampleBelow is a demonstration of the samemy_list_1 = [23, 64, 34, 77, 89, 9, 21] my_list_2 = [64, 10, 18, 11, 0, 21] print("The first list is :") print(my_list_1) print("The second list is :") print(my_list_2) my_list_2 = set(my_list_2) my_result = next((ele for ele in my_list_1 if ele in my_list_2), None) print("The result is :") print(my_result)OutputThe first list is : [23, 64, 34, 77, 89, 9, 21] The second list is : [64, 10, 18, ... Read More

Advertisements