Found 26504 Articles for Server Side Programming

Python – Fractional Frequency of elements in List

AmitDiwan
Updated on 08-Sep-2021 08:24:52

446 Views

When it is required to find the fractional frequency of elements in a list, a dictionary comprehension, a simple iteration and the ‘Counter’ method is used.ExampleBelow is a demonstration of the same −from collections import Counter my_list = [14, 15, 42, 60, 75, 50, 45, 55, 14, 60, 48, 65] print("The list is :") print(my_list) my_num = {index : 0 for index in set(my_list)} my_denominator = Counter(my_list) my_result = [] for element in my_list: my_num[element] += 1 my_result.append(str(my_num[element]) + '/' + str(my_denominator[element])) print("The result is :") print(my_result)OutputThe ... Read More

Python – Concatenate Tuple to Dictionary Key

AmitDiwan
Updated on 08-Sep-2021 08:23:41

384 Views

When it is required to concatenate tuple to a dictionary key, a list comprehension and the ‘join’ attribute are used.ExampleBelow is a demonstration of the same −my_list = [(("pyt", "is", "best"), 10), (("pyt", "cool"), 1), (("pyt", "is", "fun"), 15)] print("The list is :") print(my_list) my_result = {} for sub_list in my_list: my_result[" ".join(sub_list[0])] = sub_list[1] print("The result is :") print(my_result)OutputThe list is : [(('pyt', 'is', 'best'), 10), (('pyt', 'cool'), 1), (('pyt', 'is', 'fun'), 15)] The result is : {'pyt is best': 10, 'pyt cool': 1, 'pyt is fun': 15}ExplanationA list of tuple is ... Read More

Python – Restrict Elements Frequency in List

AmitDiwan
Updated on 08-Sep-2021 08:22:12

377 Views

When it is required to restrict elements frequency in a list, a simple iteration is used along with the ‘append’ method.ExampleBelow is a demonstration of the same −from collections import defaultdict my_list = [11, 14, 15, 14, 11, 14, 14, 15, 15, 16] print("The list is :") print(my_list) my_dict = {14 : 3, 11 : 1, 16 : 1, 15 : 2} print("The dictionary is :") print(my_dict) my_result = [] my_def_dict = defaultdict(int) for element in my_list: my_def_dict[element] += 1 if my_def_dict[element] > my_dict[element]: ... Read More

Python – List Elements Grouping in Matrix

AmitDiwan
Updated on 08-Sep-2021 08:20:37

305 Views

When it is required to list elements grouping in a matrix, a simple iteration, the ‘pop’ method, list comprehension and ‘append’ methods are used.ExampleBelow is a demonstration of the same −my_list = [[14, 62], [51, 23], [12, 62], [78, 87], [41, 14]] print("The list is :") print(my_list) check_list = [14, 12, 41, 62] print("The list is :") print(check_list) my_result = [] while my_list: sub_list_1 = my_list.pop() sub_list_2 = [element for element in check_list if element not in sub_list_1] try: ... Read More

Python program to convert tuple into list by adding the given string after every element

AmitDiwan
Updated on 08-Sep-2021 08:17:52

253 Views

When it is required to convert tuple into list by adding the given string after every element, the list comprehension is used.ExampleBelow is a demonstration of the same −my_tuple = ((15, 16), (71), 42, 99) print("The tuple is :") print(my_tuple) K = "Pyt" print("The value of K is :") print(K) my_result = [element for sub in my_tuple for element in (sub, K)] print("The result is :") print(my_result)OutputThe tuple is : ((15, 16), 71, 42, 99) The value of K is : Pyt The result is : [(15, 16), 'Pyt', 71, 'Pyt', 42, 'Pyt', 99, 'Pyt']ExplanationA tuple ... Read More

Python – Cross Join every Kth segment

AmitDiwan
Updated on 08-Sep-2021 07:32:15

227 Views

When it is required to cross join every ‘K’th element, a method is defined that uses iteration and fetches the index as output.ExampleBelow is a demonstration of the same −def merge_pair_elem(my_list_1, my_list_2, K): index_1 = 0 index_2 = 0 while(index_1 < len(my_list_1)): for i in range(K): yield my_list_1[index_1] index_1 += 1 for i in range(K): ... Read More

Python program to find the character position of Kth word from a list of strings

AmitDiwan
Updated on 08-Sep-2021 07:19:06

354 Views

When it is required to find the character position of ‘K’th word from a list of strings, a list comprehension along with enumerate is used.ExampleBelow is a demonstration of the same −my_list = ["python", "is", "fun", "to", "learn"] print("The list is :") print(my_list) K = 15 print("The value of K is :") print(K) my_result = [element[0] for sub in enumerate(my_list) for element in enumerate(sub[1])] my_result = my_result[K] print("The result is :") print(my_result)OutputThe list is : ['python', 'is', 'fun', 'to', 'learn'] The value of K is : 15 The result is : 2ExplanationA list of strings ... Read More

Python program to replace all Characters of a List except the given character

AmitDiwan
Updated on 08-Sep-2021 07:17:39

466 Views

When it is required to replace all characters of a list except a given character, a list comprehension, and the ‘==’ operator are used.ExampleBelow is a demonstration of the same −my_list = ['P', 'Y', 'T', 'H', 'O', 'N', 'P', 'H', 'P'] print("The list is :") print(my_list) replace_char = '$' retain_char = 'P' my_result = [element if element == retain_char else replace_char for element in my_list] print("The result is :") print(my_result)OutputThe list is : ['P', 'Y', 'T', 'H', 'O', 'N', 'P', 'H', 'P'] The result is : ['P', '$', '$', '$', '$', '$', 'P', '$', 'P']ExplanationA ... Read More

Python program to concatenate Strings around K

AmitDiwan
Updated on 08-Sep-2021 07:16:04

172 Views

When it is required to concatenate strings around ‘K’, a simple iteration and the ‘append’ method is used.ExampleBelow is a demonstration of the same −my_list = ["python", "+", 'is', 'fun', "+", 'to', 'learn'] print("The list is :") print(my_list) K = "+" print("The value of K is :") print(K) my_result = [] index = 0 while index < len(my_list): element = my_list[index] if (index < len(my_list) - 1) and my_list[index + 1] == K: element = element + K + my_list[index + 2] ... Read More

Python program for sum of consecutive numbers with overlapping in lists

AmitDiwan
Updated on 08-Sep-2021 07:13:31

643 Views

When it is required to sum the consecutive numbers with overlapping elements in lists, a list comprehension, list slicing, concatenation operator and ‘zip’ methods are used.ExampleBelow is a demonstration of the same −my_list = [41, 27, 53, 12, 29, 32, 16] print("The list is :") print(my_list) my_result = [a + b for a, b in zip(my_list, my_list[1:] + [my_list[0]])] print("The result is :") print(my_result)OutputThe list is : [41, 27, 53, 12, 29, 32, 16] The result is : [68, 80, 65, 41, 61, 48, 57]ExplanationA list of integers is defined and is displayed on the console.A list ... Read More

Advertisements