Found 26504 Articles for Server Side Programming

Python – Maximum of K element in other list

AmitDiwan
Updated on 13-Sep-2021 11:17:28

195 Views

When it is required to get the maximum of K elements based on another list, a simple iteration, the ‘append’ method and the ‘max’ methods are used.ExampleBelow is a demonstration of the same −my_list_1 = [62, 25, 32, 98, 75, 12, 46, 53] my_list_2 = [91, 42, 48, 76, 23, 17, 42, 83] print("The first list is : " ) print(my_list_1) print("The first list after sorting is : " ) my_list_1.sort() print(my_list_1) print("The second list is : " ) print(my_list_2) print("The first list after sorting is : " ) my_list_2.sort() print(my_list_2) K = 42 print("The ... Read More

Python – Split Numeric String into K digit integers

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

363 Views

When it is required to split a numeric string into K digit integers, a simple iteration, the ‘int’ method and the ‘append’ methods are used.ExampleBelow is a demonstration of the same −my_string = '69426874124863145' print("The string is : " ) print(my_string) K = 4 print("The value of K is ") print(K) my_result = [] for index in range(0, len(my_string), K): my_result.append(int(my_string[index : index + K])) print("The resultant list is : ") print(my_result) print("The resultant list after sorting is : ") my_result.sort() print(my_result)OutputThe string is : 69426874124863145 The value of K is 4 ... Read More

Python – Check if elements in a specific index are equal for list elements

AmitDiwan
Updated on 13-Sep-2021 11:13:09

796 Views

When it is required to check if the elements in a specific index are equal to another list of elements, a simple iteration and a Boolean value are used.ExampleBelow is a demonstration of the same −my_list_1 = [69, 96, 23, 57, 13, 75, 13] my_list_2 = [68, 21, 69, 23, 49, 35, 73] print("The first list is : " ) print(my_list_1) print("The first list after sorting is :") my_list_1.sort() print(my_list_1) print("The second list is : " ) print(my_list_2) print("The first list after sorting is :") my_list_2.sort() print(my_list_2) check_list = [66, 89, 69] print("The second list ... Read More

Python – Strings with all given List characters

AmitDiwan
Updated on 13-Sep-2021 11:08:40

372 Views

When it is required to find the strings which have all the given characters present in a list, a method is defined that takes the string as a parameter and iterates through the string, and adds the index value to it.ExampleBelow is a demonstration of the same −print("Method definition begins...") def convert_to_my_string(my_string):    my_result = ""    for index in my_string:       my_result += index    return my_result print("Method definition ends...") my_string = ['L', 'e', 'a', 'r', 'n', 'P', 'y', 't', 'h', 'o', 'n', 'c', 'o', 'o', 'l', 'f', 'u', 'n'] print("The list is : " ... Read More

Python – Filter consecutive elements Tuples

AmitDiwan
Updated on 13-Sep-2021 11:06:53

281 Views

When it is required to filter consecutive elements from a list of tuple, a method is defined that takes a list of tuple as a parameter and checks the index of every tuple, and returns a Boolean value depending on the index.ExampleBelow is a demonstration of the same −print("Method definition begins...") def check_consec_tuple_elem(my_tuple):    for idx in range(len(my_tuple) - 1):       if my_tuple[idx + 1] != my_tuple[idx] + 1:          return False    return True print("Method definition ends...") my_tuple = [(23, 24, 25, 26), (65, 66, 78, 29), (11, 28, 39), (60, 61, 62, 63)] ... Read More

Python – Remove Rows for similar Kth column element

AmitDiwan
Updated on 13-Sep-2021 11:04:30

120 Views

When it is required to remove rows for a similar ‘K’th column element, a simple iteration, and the ‘append’ method are used.ExampleBelow is a demonstration of the same −my_list = [[45, 95, 26], [70, 35, 74], [87, 65, 23], [70, 35, 74], [67, 85, 12], [45, 65, 0]] print("The list is : " ) print(my_list) K = 1 print("The value of K is ") print(K) my_result = [] my_mem = [] for index in my_list:    if not index[K] in my_mem:       my_result.append(index)       my_mem.append(index[K]) print("The resultant list is : ") ... Read More

Python – Count frequency of sublist in given list

AmitDiwan
Updated on 13-Sep-2021 11:01:44

460 Views

When it is required to count the frequency of a sub-list in a given list, a list comprehension and the ‘len’ method along with the ‘if’ condition are used.ExampleBelow is a demonstration of the same −my_list = [23, 33, 45, 67, 54 , 43, 33, 45, 67, 83, 33, 45, 67, 90, 0] print("The list is : " ) print(my_list) sub_list = [33, 45, 67, 90] print("The sub-list is : " ) print(sub_list) my_result = len([sub_list for index in range(len(my_list)) if my_list[index : index + len(sub_list)] == sub_list]) print("The resultant list is : ") print(my_result)OutputThe list ... Read More

Python – Segregate elements by delimiter

AmitDiwan
Updated on 13-Sep-2021 10:59:06

191 Views

When it is required to segregate elements based on a delimiter, ExampleBelow is a demonstration of the same −my_list = ["89@21", "58@51", "19@61", "11@10", "32@65", "34@45", "87@90", "32@21", '1@345'] print("The list is : " ) print(my_list) print("The list after sorting is :") my_list.sort() print(my_list) my_delimiter = "@" print("The delimiter is :") print(my_delimiter) result_before_delim, result_after_delim = [ele.split(my_delimiter)[0] for ele in my_list], [ele.split(my_delimiter)[1] for ele in my_list] print("The result containing elements before delimiter is : ") print(result_before_delim) print("The result containing elements after delimiter is : ") print(result_after_delim)OutputThe list is : ['89@21', '58@51', '19@61', '11@10', '32@65', '34@45', '87@90', ... Read More

Python – Extract elements in between multiple specific range of index

AmitDiwan
Updated on 13-Sep-2021 10:48:40

720 Views

When it is required to extract elements that are in between multiple specific range of index, the ‘extend’ method, a simple iteration and indexing are used.ExampleBelow is a demonstration of the same −my_list = [13, 21, 81, 10, 13, 17, 22, 18, 11, 90, 0] print("The list is : ") print(my_list) print("The list after sorting is : " ) my_list.sort() print(my_list) range_list = [(2, 4), (7, 8), (1, 2), (2, 7)] my_result = [] for element in range_list: my_result.extend(my_list[element[0] : element[1] + 1]) print("The resultant list is : ") print(my_result) print("The result ... Read More

Python – Sort List items on basis of their Digits

AmitDiwan
Updated on 13-Sep-2021 10:45:23

366 Views

When it is required to sort the elements of a list based on the digits, the ‘max’, ‘max’ method are used. We will also use the ‘sorted’ method, the ‘lambda’ function and ‘ljust’.ExampleBelow is a demonstration of the same −my_list = [4344, 2611, 122, 541, 33, 892, 48292, 460, 390, 120, 10, 2909, 11239, 1] print("The list is : " ) print(my_list) print("The list after sorting is : " ) my_list.sort() print(my_list) my_temp_val = map(str, my_list) my_max_length = max(map(len, my_temp_val)) my_result = sorted(my_list, key = lambda index : (str(index).ljust(my_max_length, 'a'))) print("The resultant list is ... Read More

Advertisements