Found 26504 Articles for Server Side Programming

Python program to remove each y occurrence before x in List

AmitDiwan
Updated on 14-Sep-2021 10:40:55

114 Views

When it is required to remove every ‘y’ occurrence before ‘x’ in a list, a list comprehension along with the ‘index’ method are used.ExampleBelow is a demonstration of the samemy_list = [4, 45, 75, 46, 66, 77, 48, 99, 10, 40, 5, 8] print("The list is :") print(my_list) a, b = 8, 4 index_a = my_list.index(a) my_result = [ele for index, ele in enumerate(my_list) if ele != b or (ele == b and index > index_a) ] print("The resultant list is ") print(my_result)OutputThe list is : [4, 45, 75, 46, 66, 77, 48, 99, 10, ... Read More

Python Program that prints the rows of a given length from a matrix

AmitDiwan
Updated on 14-Sep-2021 10:39:05

147 Views

When it is required to print the row of a specific length from a matrix, list comprehension is used.ExampleBelow is a demonstration of the samemy_list = [[22, 4, 63, 7], [24, 4, 85], [95], [2, 55, 4, 7, 91], [5, 31, 1]] print("The list is :") print(my_list) my_key = 4 my_result = [sub for sub in my_list if len(sub) == my_key] print("The resultant list is :") print(my_result)OutputThe list is : [[22, 4, 63, 7], [24, 4, 85], [95], [2, 55, 4, 7, 91], [5, 31, 1]] The resultant list is : [[22, 4, 63, 7]]ExplanationA list ... Read More

Python Program to print a specific number of rows with Maximum Sum

AmitDiwan
Updated on 14-Sep-2021 10:37:55

202 Views

When it is required to print a specific number of rows with the maximum sum, the ‘sorted’ method, and the ‘lambda’ method are used.ExampleBelow is a demonstration of the samemy_list = [[2, 4, 6, 7], [2, 4, 8], [45], [1, 3, 5, 6], [8, 2, 1]] print("The list is :") print(my_list) my_key = 3 print("The key is") print(my_key) my_result = sorted(my_list, key=lambda row: sum(row), reverse=True)[:my_key] print("The resultant list is :") print(my_result)OutputThe list is : [[2, 4, 6, 7], [2, 4, 8], [45], [1, 3, 5, 6], [8, 2, 1]] The key is 3 The resultant list is ... Read More

Python – Sort Matrix by Palindrome count

AmitDiwan
Updated on 14-Sep-2021 10:36:50

219 Views

When it is required to sort matrix based on palindrome count, a method is defined that takes a list as parameter. It uses the list comprehension and ‘join’ method to iterate and see if an element is a palindrome or not. Based on this, results are determined and displayed.ExampleBelow is a demonstration of the samedef get_palindrome_count(row):    return len([element for element in row if''.join(list(reversed(element))) == element]) my_list = [["abcba", "hdgfue", "abc"], ["peep"], ["py", "is", "best"], ["sees", "level", "non", "noon"]] print("The list is :") print(my_list) my_list.sort(key=get_palindrome_count) print("The resultant list is :") print(my_list)OutputThe list is : [['abcba', 'hdgfue', 'abc'], ... Read More

Python – Extract rows with Complex data types

AmitDiwan
Updated on 14-Sep-2021 10:34:58

139 Views

When it is required to extract rows with complex data types, the ‘isinstance’ method and list comprehension are used.ExampleBelow is a demonstration of the samemy_list = [[13, 1, 35], [23, [44, 54], 85], [66], [75, (81, 2), 29, 7]] my_result = [row for row in my_list if any(isinstance(element, list) or isinstance(element, tuple) or isinstance(element, dict) or isinstance(element, set) for element in row)] print("The list is :") print(my_list) print("The resultant list is :") print(my_result)OutputThe list is : [[13, 1, 35], [23, [44, 54], 85], [66], [75, (81, 2), 29, 7]] The resultant list is : [[23, [44, 54], ... Read More

Python Program to return the Length of the Longest Word from the List of Words

AmitDiwan
Updated on 14-Sep-2021 10:33:19

1K+ Views

When it is required to return the length of the longest word from a list of words, a method is defined that takes a list as parameter. It checks if an element is in the list and depending on this, the output is displayed.ExampleBelow is a demonstration of the samedef find_longest_length(my_list):    max_length = len(my_list[0])    temp = my_list[0]    for element in my_list:       if(len(element) > max_length):          max_length = len(element)          temp = element return max_length my_list = ["ab", "abc", "abcd", "abcde"] print("The list ... Read More

Python Program to Sort Matrix Rows by summation of consecutive difference of elements

AmitDiwan
Updated on 14-Sep-2021 10:31:43

191 Views

ExampleBelow is a demonstration of the samedef diff_summation_elem(row): return sum([abs(row[index + 1] - row[index]) for index in range(0, len(row) - 1)]) my_list = [[97, 6, 47, 3], [6, 88, 3, 26], [71, 53, 34, 65], [15, 36, 5, 62]] print("The list is : ") print(my_list) my_list.sort(key=diff_summation_elem) print("The resultant list is :" ) print(my_list)OutputThe list is : [[97, 6, 47, 3], [6, 88, 3, 26], [71, 53, 34, 65], [15, 36, 5, 62]] The resultant list is : [[71, 53, 34, 65], [15, 36, 5, 62], [97, 6, 47, 3], [6, 88, 3, 26]]ExplanationA ... Read More

Python – Append given number with every element of the list

AmitDiwan
Updated on 14-Sep-2021 10:28:01

337 Views

When it is required to append given number with every element of the list, a list comprehension is used.ExampleBelow is a demonstration of the samemy_list = [25, 36, 75, 36, 17, 7, 8, 0] print ("The list is :") print(my_list) my_key = 6 my_result = [x + my_key for x in my_list] print ("The resultant list is :") print(my_result)OutputThe list is : [25, 36, 75, 36, 17, 7, 8, 0] The resultant list is : [31, 42, 81, 42, 23, 13, 14, 6]ExplanationA list is defined and is displayed on the console.An integer value for key ... Read More

Python – Cumulative Row Frequencies in a List

AmitDiwan
Updated on 14-Sep-2021 10:26:49

300 Views

When it is required to get the cumulative row frequencies in a list, the ‘Counter’ method, and a list comprehension are used.ExampleBelow is a demonstration of the samefrom collections import Counter my_list = [[11, 2, 32, 4, 31], [52, 52, 3, 71, 71, 3], [1, 3], [19, 19, 40, 40, 40]] print("The list is :") print(my_list) my_element_list = [19, 2, 71] my_frequency = [Counter(element) for element in my_list] my_result = [sum([freq[word] for word in my_element_list if word in freq]) for freq in my_frequency] print("The resultant matrix is :") print(my_result)OutputThe list is : [[11, 2, ... Read More

Python Program To Get Minimum Element For String Construction

AmitDiwan
Updated on 14-Sep-2021 10:19:58

129 Views

When it is required to get the minimum element to construct a string, the ‘set’ operator, the ‘combinations’ method, the ‘issubset’ method and a simple iteration is required.ExampleBelow is a demonstration of the samefrom itertools import combinations my_list = ["python", "is", "fun", "to", "learn"] print("The list is :") print(my_list) my_target_str = "onis" my_result = -1 my_set_string = set(my_target_str) complete_val = False for value in range(0, len(my_list) + 1): for sub in combinations(my_list, value): temp_set = set(ele for subl in sub for ele in subl) ... Read More

Advertisements