Server Side Programming Articles

Page 315 of 2109

Python - Group contiguous strings in List

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 286 Views

When working with mixed lists containing both strings and numbers, you might need to group contiguous string elements together while keeping non-string elements separate. Python's itertools.groupby() function combined with a custom key function provides an elegant solution. Understanding the Problem Given a list with mixed data types, we want to group consecutive string elements into sublists while leaving non-string elements as individual items ? Solution Using groupby() We'll create a helper function to identify strings and use groupby() to group contiguous elements ? from itertools import groupby def string_check(elem): ...

Read More

Python - Add a zero column to Pandas DataFrame

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 2K+ Views

To add a zero column to a Pandas DataFrame, you can use square bracket notation to create a new column and assign 0 to all rows. This is useful for initializing placeholder columns or creating default values. Creating the DataFrame First, let's create a sample DataFrame with student data ? import pandas as pd # Create DataFrame dataFrame = pd.DataFrame({ "Student": ['Jack', 'Robin', 'Ted', 'Marc', 'Scarlett', 'Kat', 'John'], "Result": ['Pass', 'Fail', 'Pass', 'Fail', 'Pass', 'Pass', 'Pass'], "Roll Number": [5, 10, 3, 8, 2, ...

Read More

Python - Element frequencies in percent range

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 383 Views

When finding elements whose frequencies fall within a specific percentage range, we can use Python's Counter class along with percentage calculations to filter elements based on their occurrence rates. Example Below is a demonstration of finding elements with frequencies between 13% and 60% − from collections import Counter my_list = [56, 34, 78, 90, 11, 23, 6, 56, 79, 90] print("The list is :") print(my_list) start, end = 13, 60 my_freq = dict(Counter(my_list)) print("Element frequencies:", my_freq) my_result = [] for element in set(my_list): percent = (my_freq[element] / len(my_list)) * 100 print(f"Element {element}: {percent}%") if percent >= start and percent

Read More

Python Program to Get K initial powers of N

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 377 Views

When it is required to get the specific number of powers of a number, the ** operator is used along with list comprehension to generate the first K powers of N. Syntax The basic syntax for calculating powers in Python ? # Using ** operator result = base ** exponent # Using list comprehension for multiple powers powers = [base ** i for i in range(k)] Example Below is a demonstration of getting K initial powers of N ? n = 4 k = 5 print("The value n is:", ...

Read More

Python Program to find Duplicate sets in list of sets

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 429 Views

When working with lists of sets, you may need to identify which sets appear more than once. Python provides an efficient solution using Counter from the collections module and frozenset to handle duplicate sets. Why Use frozenset? Sets are mutable and unhashable, so they cannot be directly counted by Counter. frozenset creates an immutable, hashable version of a set that can be used as a dictionary key or counted. Example Below is a demonstration of finding duplicate sets ? from collections import Counter my_list = [{4, 8, 6, 1}, {6, 4, 1, 8}, ...

Read More

Add custom borders to a matrix in Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 364 Views

When it is required to add custom borders to the matrix, a simple list iteration can be used to add the required borders to the matrix. This technique is useful for displaying matrices in a more readable format with visual boundaries. Example Below is a demonstration of the same ? my_list = [[2, 5, 5], [2, 7, 5], [4, 5, 1], [1, 6, 6]] print("The list is :") print(my_list) print("The resultant matrix is :") border = "|" for sub in my_list: my_temp = border + " " ...

Read More

How to append a list as a row to a Pandas DataFrame in Python?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 2K+ Views

To append a list as a row to a Pandas DataFrame in Python, we can use the concat() method (recommended) or the loc[] indexer. Let's first import the required library − import pandas as pd Following is the data in the form of lists of team rankings − Team = [['India', 1, 100], ['Australia', 2, 85], ['England', 3, 75], ['New Zealand', 4 , 65], ['South Africa', 5, 50]] Creating a DataFrame with the above data and adding columns − import pandas as pd Team = [['India', 1, 100], ['Australia', ...

Read More

Python program to remove words that are common in two Strings

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 983 Views

When working with text processing, you may need to remove words that are common between two strings and keep only the unique words. This can be achieved by counting word frequencies and filtering words that appear only once across both strings. Example Below is a demonstration of removing common words from two strings − def common_words_filter(string_1, string_2): word_count = {} # Count words from first string for word in string_1.split(): word_count[word] = word_count.get(word, 0) + ...

Read More

Python Program to Extract Elements from a List in a Set

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 454 Views

When it is required to extract elements from a list that exist in a set, a simple 'for' loop with membership checking can be used. This technique filters the list to keep only elements that are present in the target set. Method 1: Using For Loop Below is a demonstration using a basic for loop with conditional checking − my_list = [5, 7, 2, 7, 2, 4, 9, 8, 8] print("The list is:") print(my_list) search_set = {6, 2, 8} print("The search set is:") print(search_set) my_result = [] for element in my_list: ...

Read More

Python Pandas - Filling missing column values with median

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 3K+ Views

The median is a statistical measure that separates the higher half from the lower half of a dataset. In Pandas, you can fill missing values (NaN) in a DataFrame column with the median using the fillna() method combined with median(). Importing Required Libraries First, import Pandas and NumPy with their standard aliases ? import pandas as pd import numpy as np Creating DataFrame with Missing Values Create a DataFrame containing NaN values using np.NaN ? import pandas as pd import numpy as np # Create DataFrame with missing values dataFrame ...

Read More
Showing 3141–3150 of 21,090 articles
« Prev 1 313 314 315 316 317 2109 Next »
Advertisements