Programming Articles

Page 323 of 2547

Python regex to find sequences of one upper case letter followed by lower case letters

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 960 Views

When working with text processing, you often need to find sequences of one uppercase letter followed by lowercase letters. Python's regex module provides an efficient way to match such patterns using regular expressions. Understanding the Pattern The regex pattern [A-Z][a-z]+ matches one uppercase letter followed by one or more lowercase letters. Let's break it down ? [A-Z] − matches exactly one uppercase letter [a-z]+ − matches one or more lowercase letters $ − ensures the pattern matches to the end of string Basic Pattern Matching Here's a simple function to check if a ...

Read More

Python - Remove all characters except letters and numbers

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

When it is required to remove all characters except for letters and numbers, regular expressions are used. A regular expression is defined, and the string is subjected to follow this expression. Using Regular Expressions with re.sub() The re.sub() function replaces all non-alphanumeric characters with an empty string ? import re my_string = "python123:, .@! abc" print("The string is:") print(my_string) result = re.sub('[\W_]+', '', my_string) print("The expected string is:") print(result) The output of the above code is ? The string is: python123:, .@! abc The expected string is: python123abc ...

Read More

Python - How to group DataFrame rows into list in Pandas?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 407 Views

When working with Pandas DataFrames, you may need to group rows and collect values into lists. This is commonly done using the groupby() method combined with apply(list). Basic Grouping with apply(list) The simplest approach uses groupby() with apply(list) to collect values ? import pandas as pd # Create DataFrame dataFrame = pd.DataFrame({ "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] }) print("Original DataFrame:") print(dataFrame) # Group by Car and collect Units into lists grouped = dataFrame.groupby('Car')['Units'].apply(list) print("Grouped DataFrame:") print(grouped) ...

Read More

Python Program to Group Strings by K length Using Suffix

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 305 Views

When it is required to group strings by K length using a suffix, a simple iteration and the 'try' and 'except' blocks are used. This technique extracts the last K characters from each string and groups strings with the same suffix together. Example Below is a demonstration of the same − my_list = ['peek', "leak", 'creek', "weak", "good", 'week', "wood", "sneek"] print("The list is :") print(my_list) K = 3 print("The value of K is") print(K) my_result = {} for element in my_list: suff = element[-K : ] ...

Read More

Python – Replacing by Greatest Neighbors in a List

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 250 Views

When it is required to replace elements of a list by their greatest neighbors, a simple iteration along with the 'if' and 'else' condition is used. Each element (except the first and last) gets replaced by whichever neighbor has the greater value. Syntax The general approach involves iterating through the list and comparing adjacent elements − for index in range(1, len(list) - 1): list[index] = list[index - 1] if list[index - 1] > list[index + 1] else list[index + 1] Example Below is a demonstration of replacing elements by ...

Read More

Python – Filter dictionaries with ordered values

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 300 Views

When filtering dictionaries to find those with values in ascending order, we can use the sorted() method along with list comprehension to compare the original values with their sorted version. Syntax [dict for dict in list_of_dicts if sorted(dict.values()) == list(dict.values())] Example Below is a demonstration of filtering dictionaries with ordered values − my_list = [{'python': 2, 'is': 8, 'fun': 10}, {'python': 1, 'for': 10, 'coding': 9}, {'cool': 3, 'python': 4}] ...

Read More

Python Pandas – Filter DataFrame between two dates

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

To filter DataFrame between two dates, use the dataframe.loc method. This allows you to select rows based on date conditions using boolean indexing. Creating a DataFrame with Date Records At first, import the required library and create a DataFrame with date columns − import pandas as pd # Dictionary of lists with date records d = {'Car': ['BMW', 'Lexus', 'Audi', 'Mercedes', 'Jaguar', 'Bentley'], 'Date_of_Purchase': ['2021-07-10', '2021-08-12', '2021-06-17', ...

Read More

Python – Replace value by Kth index value in Dictionary List

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 683 Views

Sometimes you need to replace list values in dictionaries with specific elements from those lists. This technique uses isinstance() to identify list values and replaces them with the element at the Kth index. Example Below is a demonstration of replacing list values with their Kth index elements − my_list = [{'python': [5, 7, 9, 1], 'is': 8, 'good': 10}, {'python': 1, 'for': 10, 'fun': 9}, {'cool': 3, 'python': [7, 3, 9, 1]}] print("The ...

Read More

Python – Summation of consecutive elements power

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 230 Views

When it is required to add the consecutive elements power, an 'if' condition and a simple iteration along with the '**' operator are used. This technique calculates the sum of each element raised to the power of its consecutive frequency count. Example Below is a demonstration of the same ? my_list = [21, 21, 23, 23, 45, 45, 45, 56, 56, 67] print("The list is :") print(my_list) my_freq = 1 my_result = 0 for index in range(0, len(my_list) - 1): if my_list[index] != my_list[index + 1]: ...

Read More

Python program to find the group sum till each K in a list

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 238 Views

When you need to find the cumulative sum of elements in groups separated by a specific key value K, you can use a simple iteration approach. This technique sums elements between occurrences of K and includes K itself in the result. Example Below is a demonstration of finding group sums separated by key value K ? my_list = [21, 4, 37, 46, 7, 56, 7, 69, 2, 86, 1] print("The list is :") print(my_list) my_key = 46 print("The key is") print(my_key) my_sum = 0 my_result = [] for ele in my_list: ...

Read More
Showing 3221–3230 of 25,466 articles
« Prev 1 321 322 323 324 325 2547 Next »
Advertisements