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 321 of 2547
Python - Generate all possible permutations of words in a Sentence
When it is required to generate all possible permutations of words in a sentence, Python's itertools.permutations() function provides an efficient solution. This function takes a list of words and returns all possible arrangements. Using itertools.permutations() The permutations() function from the itertools module generates all possible orderings of the input elements ? from itertools import permutations def calculate_permutations(my_string): word_list = list(my_string.split()) permutes = permutations(word_list) for perm in permutes: permute_list = list(perm) ...
Read MorePython program to extract rows from Matrix that has distinct data types
When working with matrices containing mixed data types, you may need to extract rows where each element has a distinct data type. This can be achieved by comparing the number of unique types in a row with the total number of elements. Example Below is a demonstration of extracting rows with distinct data types − my_list = [[4, 2, 6], ["python", 2, {6: 2}], [3, 1, "fun"], [9, (4, 3)]] print("The list is:") print(my_list) my_result = [] for sub in my_list: # Get unique data types in the current row ...
Read MorePython - Split list into all possible tuple pairs
When working with lists, you might need to generate all possible ways to split the list into tuple pairs. This involves creating partitions where elements can either remain as individual items or be grouped into tuples with other elements. Example Below is a demonstration of splitting a list into all possible tuple pair combinations ? def determine_pairings(my_list): if len(my_list)
Read MorePython program to Reverse a range in list
When it is required to reverse a specific range of elements in a list, Python provides several approaches. We can use slicing with the [::-1] operator to reverse a portion of the list while keeping other elements in their original positions. Method 1: Using Slicing to Reverse a Range We can reverse elements between specific indices using slicing ? my_list = [1, 2, 3, 4, 5, 6, 7, 8] print("Original list:") print(my_list) # Reverse elements from index 2 to 5 start, end = 2, 5 my_list[start:end+1] = my_list[start:end+1][::-1] print("List after reversing range [2:6]:") print(my_list) ...
Read MorePython - Cumulative Mean of Dictionary keys
When working with a list of dictionaries, you may need to calculate the cumulative mean of values for each key that appears across all dictionaries. This involves collecting all values for each unique key and computing their average. Example Below is a demonstration of calculating cumulative mean across multiple dictionaries ? from statistics import mean my_list = [{'hi': 24, 'there': 81, 'how': 11}, {'hi': 16, 'how': 78, 'doing': 63}] print("The list is:") print(my_list) my_result = dict() for sub in my_list: ...
Read MorePython - How to fill NAN values with mean in Pandas?
When working with datasets in Pandas, missing values (NaN) are common. You can fill these NaN values with the mean of the column using mean() and fillna() functions. Creating a DataFrame with NaN Values Let us first import the required libraries and create a sample DataFrame ? import pandas as pd import numpy as np # Create DataFrame with NaN values dataFrame = pd.DataFrame({ "Car": ['BMW', 'Lexus', 'Lexus', 'Mustang', 'Bentley', 'Mustang'], "Units": [100, 150, np.NaN, 80, np.NaN, np.NaN] }) print("Original DataFrame:") print(dataFrame) Original ...
Read MorePython - Given a list of integers that represents a decimal value, increment the last element by 1
When working with a list of integers that represents a decimal value, we sometimes need to increment it by 1. This is similar to adding 1 to a number, but we need to handle carry operations when digits are 9. Problem Understanding Given a list like [1, 2, 3] representing the number 123, we want to increment it to get [1, 2, 4] representing 124. If the last digit is 9, we need to handle carries properly. Using Recursive Approach Here's a recursive solution that handles the carry operation when incrementing ? def increment_num(digits, ...
Read MorePython - Given an integer 'n', check if it is a power of 4, and return True, otherwise False.
When it is required to check if a given variable is a power of 4, we can use different approaches. A power of 4 means the number can be expressed as 4k where k is a non-negative integer (1, 4, 16, 64, 256, etc.). Method 1: Using Division and Modulus This method repeatedly divides the number by 4 and checks if there's any remainder ? def check_power_of_4(num): if num == 0: return False while num != 1: ...
Read MorePython - Check if two strings are isomorphic in nature
Two strings are isomorphic if there exists a one-to-one mapping between characters of both strings. This means each character in the first string can be replaced to get the second string, and no two characters map to the same character. What are Isomorphic Strings? Strings are isomorphic when: They have the same length Each character in string1 maps to exactly one character in string2 No two different characters in string1 map to the same character in string2 For example: "aab" and "xxy" are isomorphic (a→x, b→y), but "aab" and "xyz" are not (both 'a' characters ...
Read MorePython - Check if a number and its double exists in an array
When it is required to check if a number and its double exists in an array, we can iterate through the array and check if double of each element exists. Python provides multiple approaches to solve this problem efficiently. Method 1: Using Nested Loops This approach iterates through each element and checks if its double exists in the remaining elements ? def check_double_exists(my_list): for i in range(len(my_list)): for j in (my_list[:i] + my_list[i+1:]): ...
Read More