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
Server Side Programming Articles
Page 314 of 2109
Python Program to find out the sum of values in hyperrectangle cells
A hyperrectangle is a multi-dimensional rectangle with k dimensions. Each dimension has a length denoted as n1, n2, n3, ..., nm. The hyperrectangle's cells are addressed as (p, q, r, ...) and contain a value equivalent to the GCD (Greatest Common Divisor) of their coordinates. Our task is to find the sum of all cell values gcd(p, q, r, ...) where 1 ≤ p ≤ n1, 1 ≤ q ≤ n2, and so on. Problem Understanding Given input_arr = [[2, 2], [5, 5]], we need to calculate the sum for two test cases ? First instance [2, ...
Read MoreHow to get the input from the Tkinter Text Widget?
In Tkinter, we can create text widgets using the Text class. When building GUI applications, we often need to retrieve user input from these text widgets for processing or validation. We can get the input from a text widget using the .get() method. This method requires specifying an input range − typically from "1.0" to "end", where "1.0" represents the first character and "end" represents the last character in the widget. Basic Text Widget Input Example Here's how to create a text widget and retrieve its contents ? import tkinter as tk # Create ...
Read MoreHow to encode multiple strings that have the same length using Tensorflow and Python?
Multiple strings of same length can be encoded using tf.Tensor as an input value. When encoding multiple strings of varying lengths, a tf.RaggedTensor should be used as an input. If a tensor contains multiple strings in padded/sparse format, it needs to be converted to a tf.RaggedTensor before calling unicode_encode. Read More: What is TensorFlow and how Keras work with TensorFlow to create Neural Networks? Let us understand how to represent Unicode strings using Python, and manipulate those using Unicode equivalents. We separate the Unicode strings into tokens based on script detection with the help of the Unicode equivalents ...
Read MoreHow can Tensorflow be used to create a pair using a file path for the flower dataset?
TensorFlow can process image datasets by creating (image, label) pairs from file paths. The flowers dataset contains thousands of flower images organized in subdirectories, where each subdirectory represents a different flower class. Read More: What is TensorFlow and how Keras work with TensorFlow to create Neural Networks? We are using Google Colaboratory to run the below code. Google Colab helps run Python code over the browser and requires zero configuration with free access to GPUs. Setting Up the Dataset First, let's set up the basic variables and import required libraries ? import tensorflow as ...
Read MorePython – Stacking a single-level column with Pandas stack()?
The Pandas stack() method transforms a DataFrame by stacking column levels into row levels, creating a hierarchical index. This operation pivots columns into a multi-level index, converting wide data to long format. Syntax DataFrame.stack(level=-1, dropna=True) Creating a DataFrame with Single-Level Columns First, let's create a simple DataFrame with single-level columns ? import pandas as pd # Create DataFrame with single-level columns dataFrame = pd.DataFrame([[10, 15], [20, 25], [30, 35], [40, 45]], ...
Read MorePython - Create nested list containing values as the count of list items
When it is required to create a nested list containing values as the count of list elements, a simple iteration and list comprehension can be used. This technique replaces each element with a list containing repeated values based on the element's position. Example Below is a demonstration of creating nested lists where each position contains a list with repeated values ? my_list = [11, 25, 36, 24] print("The original list is:") print(my_list) for element in range(len(my_list)): my_list[element] = [element + 1 for j in range(element + 1)] print("The resultant nested ...
Read MorePython - Count the frequency of matrix row length
When working with matrices (lists of lists), you may need to count how many rows have the same length. Python provides several approaches to count the frequency of matrix row lengths using dictionaries or the Counter class. Using Dictionary with Manual Counting The traditional approach iterates through the matrix and manually tracks row length frequencies ? matrix = [[42, 24, 11], [67, 18], [20], [54, 10, 25], [45, 99]] print("The matrix is:") print(matrix) frequency = {} for row in matrix: length = len(row) if length not ...
Read MorePython - How to rename multiple column headers in a Pandas DataFrame with Dictionary?
In Pandas, you can rename multiple column headers simultaneously using the rename() method with a dictionary. The dictionary maps old column names (keys) to new column names (values). Syntax DataFrame.rename(columns=dictionary, inplace=True) Where dictionary contains old_name: new_name pairs, and inplace=True modifies the original DataFrame. Creating a Sample DataFrame Let's start by creating a DataFrame with car data ? import pandas as pd dataFrame = pd.DataFrame({ "Car": ['BMW', 'Mustang', 'Tesla', 'Mustang', 'Mercedes', 'Tesla', 'Audi'], "Cubic Capacity": [2000, 1800, 1500, 2500, 2200, 3000, 2000], ...
Read MorePython - Select columns with specific datatypes
To select columns with specific datatypes in Pandas, use the select_dtypes() method with the include parameter. This method allows you to filter DataFrame columns based on their data types such as object, int64, float64, etc. Syntax DataFrame.select_dtypes(include=None, exclude=None) Parameters include − List of data types to include exclude − List of data types to exclude Creating a Sample DataFrame Let's start by creating a DataFrame with different data types ? import pandas as pd # Create DataFrame with multiple data types dataFrame = pd.DataFrame( ...
Read MorePython - Character repetition string combinations
When generating string combinations with character repetition, we need to create all possible arrangements where each position can contain any character from the original string. This is essentially generating permutations with repetition using recursive backtracking. Example Below is a demonstration of generating all permutations with repetition − def to_string(my_list): return ''.join(my_list) def lex_recurrence(my_string, my_data, last_val, index_val): length = len(my_string) for i in range(length): my_data[index_val] = my_string[i] ...
Read More