Found 26504 Articles for Server Side Programming

Python – Mapping Matrix with Dictionary

AmitDiwan
Updated on 08-Sep-2021 10:35:14

984 Views

When it is required to map the matrix to a dictionary, a simple iteration is used.ExampleBelow is a demonstration of the same −my_list = [[2, 4, 3], [4, 1, 3], [2, 1, 3, 4]] print("The list :") print(my_list) map_dict = {2 : "Python", 1: "fun", 3 : "to", 4 : "learn"} my_result = [] for index in my_list:    temp = []    for element in index:       temp.append(map_dict[element])    my_result.append(temp) print("The result is :") print(my_result)OutputThe list : [[2, 4, 3], [4, 1, 3], [2, 1, 3, 4]] The result is : [['Python', 'learn', ... Read More

Python – Convert Integer Matrix to String Matrix

AmitDiwan
Updated on 08-Sep-2021 10:33:42

344 Views

When it is required to convert an integer matrix to a string matrix, a list comprehension is used.ExampleBelow is a demonstration of the same −my_list = [[14, 25, 17], [40, 28, 13], [59, 44, 66], [29, 33, 16]] print("The list is :") print(my_list) my_result = [[str(element) for element in index]for index in my_list] print("The reuslt is :") print(my_result)OutputThe list is : [[14, 25, 17], [40, 28, 13], [59, 44, 66], [29, 33, 16]] The reuslt is : [['14', '25', '17'], ['40', '28', '13'], ['59', '44', '66'], ['29', '33', '16']]ExplanationA list is defined and displayed on the console.A ... Read More

Python program to extract characters in given range from a string list

AmitDiwan
Updated on 08-Sep-2021 10:24:21

479 Views

When it is required to extract characters in given range from a string list, a list comprehension and list slicing is used.ExampleBelow is a demonstration of the same −my_list = ["python", "is", "fun", "to", "learn"] print("The list is :") print(my_list) start, end = 11, 25 my_result = ''.join([element for element in my_list])[start : end] print("The result is :") print(my_result)OutputThe list is : ['python', 'is', 'fun', 'to', 'learn'] The result is : tolearnExplanationA list is defined and displayed on the console.The value for ‘start’ and ‘end’ are defined.A list comprehension is used to iterate over the list, ... Read More

Python – Sort a List by Factor count

AmitDiwan
Updated on 08-Sep-2021 10:22:49

415 Views

When it is required to sort a list by factor count, a method is defined that uses list comprehension and modulus operator along with ‘len’ method to determine the output.ExampleBelow is a demonstration of the same −def factor_count(element):    return len([element for index in range(1, element) if element % index == 0]) my_list = [121, 1120, 13540, 221, 1400] print("The list is :") print(my_list) my_list.sort(key=factor_count) print("The result is :") print(my_list)OutputThe list is : [121, 1120, 13540, 221, 1400] The result is : [121, 221, 13540, 1120, 1400]ExplanationA method named ‘factor_count’ is defined that takes element of ... Read More

Python – Random range in a List

AmitDiwan
Updated on 08-Sep-2021 10:21:24

935 Views

When it is required to find the random range in a list, a list comprehension and the ‘randrange’ method present in ‘random’ package is used.ExampleBelow is a demonstration of the same −import random my_result = [random.randrange(1, 100, 1) for i in range(10)] print ("The result is :") print(my_result)OutputThe result is : [40, 73, 58, 45, 68, 19, 86, 6, 15, 71]ExplanationThe required packages are imported into the environment.A list comprehension is used to iterate over the list, and 'randrange' method is used to generate random number in the given range.This result is assigned to a variable.This is the ... Read More

Python – Extract Sorted Strings

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

192 Views

When it is required to extract sorted strings, a list comprehension and the ‘sorted’ method is used.ExampleBelow is a demonstration of the same −my_list = ["pyt", "Fdf", "Fun"] print("The list is :") print(my_list) my_result = [element for element in my_list if ''.join(sorted(element)) == element] print("The result is :") print(my_result)OutputThe list is : ['pyt', 'Fdf', 'Fun'] The result is : ['Fdf']ExplanationA list is defined and displayed on the console.A list comprehension is used to iterate over the list, and every element is sorted ad checked to see if it is equal to the current element, and then the ... Read More

Python – Rows with K string in Matrix

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

163 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

320 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

327 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

145 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

Advertisements