Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Programming Articles
Page 137 of 2547
Python – Fractional Frequency of elements in List
When you need to find the fractional frequency of elements in a list, you can use a dictionary to track element occurrences, combined with the Counter method from the collections module. This approach shows how many times each element has appeared out of its total occurrences as you iterate through the list. Understanding Fractional Frequency Fractional frequency represents the current occurrence count of an element divided by its total occurrence count. For example, if element 14 appears twice in a list, its fractional frequencies would be "1/2" for the first occurrence and "2/2" for the second occurrence. ...
Read MorePython – Concatenate Tuple to Dictionary Key
When it is required to concatenate tuple elements to form dictionary keys, a list comprehension and the join() method are used. This technique converts tuples into string keys while preserving their associated values. Example Below 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) Output The list is : [(('pyt', 'is', 'best'), 10), ...
Read MorePython – Restrict Elements Frequency in List
When it is required to restrict elements frequency in a list, we can use a simple iteration along with the append method and defaultdict to track element counts. Example Below 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 restrict dictionary is :") print(my_dict) my_result = [] my_def_dict = defaultdict(int) for element in my_list: my_def_dict[element] += 1 ...
Read MorePython – List Elements Grouping in Matrix
When working with matrices (list of lists), you sometimes need to group elements based on certain criteria. This example demonstrates how to group list elements in a matrix using iteration, the pop() method, list comprehension, and append() methods. Example Below is a demonstration of grouping matrix elements ? 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 check list is :") print(check_list) my_result = [] while my_list: sub_list_1 = my_list.pop() ...
Read MorePython program to convert tuple into list by adding the given string after every element
When it is required to convert a tuple into a list by adding a given string after every element, list comprehension provides an elegant solution. This technique creates a new list where each original element is followed by the specified string. Example Below 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) Output ...
Read MorePython – Cross Join every Kth segment
Cross joining every Kth segment means alternating between two lists by taking K elements from the first list, then K elements from the second list, and repeating this pattern. This creates a merged sequence that preserves segments of each original list. Using Generator Function A generator function can efficiently yield elements by alternating between lists in K-sized segments ? def cross_join_kth_segment(list1, list2, k): index1 = 0 index2 = 0 while index1 < len(list1) and index2 < len(list2): ...
Read MorePython program to find the character position of Kth word from a list of strings
When it is required to find the character position of Kth word from a list of strings, a list comprehension along with enumerate() is used to get the position of each character when all strings are concatenated. Example Below 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) Output ...
Read MorePython program to replace all Characters of a List except the given character
When it is required to replace all characters of a list except a given character, a list comprehension and the == operator are used. This technique allows you to preserve specific characters while replacing all others with a substitute character. Using List Comprehension The most Pythonic approach uses a conditional list comprehension to check each element ? characters = ['P', 'Y', 'T', 'H', 'O', 'N', 'P', 'H', 'P'] print("The list is:") print(characters) replace_char = '$' retain_char = 'P' result = [element if element == retain_char else replace_char for element in characters] print("The ...
Read MorePython program to concatenate Strings around K
String concatenation around a delimiter involves combining strings that appear before and after a specific character K. This technique is useful for merging related string elements separated by a common delimiter. Syntax The general approach uses iteration to identify patterns where strings are separated by the delimiter K ? while index < len(list): if list[index + 1] == K: combined = list[index] + K + list[index + 2] index += 2 result.append(element) ...
Read MorePython program for sum of consecutive numbers with overlapping in lists
When summing consecutive numbers with overlapping elements in lists, we create pairs of adjacent elements where each element (except the last) is paired with its next neighbor. The last element pairs with the first element to create a circular pattern. Example Below is a demonstration using list comprehension with zip ? 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) Output The list is : [41, ...
Read More