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 899 of 2547
Python - Filter out integers from float numpy array
When working with NumPy arrays containing both floats and integers, you may need to filter out integer values for data cleansing purposes. This article demonstrates two effective methods to remove integers from a float NumPy array using astype comparison and modulo operations. Method 1: Using astype Comparison The astype function converts array elements to integers. By comparing the original array with its integer-converted version, we can identify which elements are not integers ? Example import numpy as np # Create array with mixed floats and integers data_array = np.array([3.2, 5.5, 2.0, 4.1, 5]) ...
Read MorePython - Filter dictionary key based on the values in selective list
Sometimes in a Python dictionary we may need to filter out certain keys based on a selective list. This allows us to extract specific key-value pairs from a larger dictionary. In this article we will see how to filter dictionary keys using different approaches. Using List Comprehension with 'in' Operator In this approach we put the keys to be filtered in a list. Then iterate through each element of the list and check for its presence in the given dictionary. We create a resulting dictionary containing only these filtered key-value pairs ? Example # Original ...
Read MorePython - end parameter in print()
The print() function in Python automatically adds a newline character () at the end of each print statement. However, you can customize this behavior using the end parameter to specify different ending characters or strings. Syntax print(value1, value2, ..., end='character_or_string') Default Behavior By default, print() ends with a newline character ? print("Welcome to") print("Tutorialspoint") Welcome to Tutorialspoint Using Space as End Character You can replace the newline with a space to print on the same line ? print("Welcome to", end=' ') print("Tutorialspoint") ...
Read MorePython - Difference in keys of two dictionaries
Two Python dictionaries may contain some common keys between them. In this article we will find how to get the difference in the keys present in two given dictionaries. Using Set Difference Here we take two dictionaries and apply set function to them. Then we subtract the two sets to get the difference. We do it both ways, by subtracting second dictionary from first and next subtracting first dictionary from second. Those keys which are not common are listed in the result set ? Example dictA = {'1': 'Mon', '2': 'Tue', '3': 'Wed'} print("1st Dictionary:", ...
Read MorePython - Create a dictionary using list with none values
Sometimes you need to create a dictionary from a list where each list element becomes a key with a None value. This is useful for creating placeholder dictionaries or initializing data structures. Python provides several methods to achieve this conversion. Using dict.fromkeys() The dict.fromkeys() method creates a dictionary with keys from an iterable and assigns the same value to all keys. By default, it assigns None ? days = ["Mon", "Tue", "Wed", "Thu", "Fri"] print("Given list:") print(days) result = dict.fromkeys(days) print("Dictionary with None values:") print(result) Given list: ['Mon', 'Tue', 'Wed', 'Thu', ...
Read MorePython - Convert given list into nested list
Sometimes we need to convert list elements into individual sublists, creating a nested list structure. Python provides several approaches to achieve this transformation depending on your specific requirements. Using List Comprehension The simplest approach is to wrap each element in a list using list comprehension ? days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'] print("Given list:", days) # Convert each element to a sublist nested_list = [[item] for item in days] print("Nested list:", nested_list) Given list: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'] Nested list: [['Mon'], ['Tue'], ['Wed'], ['Thu'], ['Fri']] Using map() Function ...
Read MoreBroadcasting with NumPy Arrays in Python
Broadcasting is a NumPy feature that allows arithmetic operations between arrays of different shapes without explicitly reshaping them. When arrays have unequal dimensions, NumPy automatically adjusts the smaller array's shape by prepending dimensions of size 1, enabling element-wise operations. Rules of Array Broadcasting NumPy follows these rules when broadcasting arrays ? Arrays with smaller ndim are prepended with dimensions of size 1 in their shape. The output shape in each dimension is the maximum of the input sizes in that dimension. An array can be used in calculation if its size in a particular dimension matches ...
Read MoreAvoiding class data shared among the instances in Python
When we create multiple instances of a class in Python, class variables are shared among all instances. This can lead to unexpected behavior when modifying mutable objects like lists or dictionaries. In this article, we will explore the problem and two effective solutions to avoid shared class data. The Problem: Shared Class Variables In the below example, we demonstrate how class variables are shared across all instances, leading to unintended data sharing ? class MyClass: listA = [] # This is a class variable, shared by all instances # Instantiate ...
Read MoreAverage of each n-length consecutive segment in a Python list
We have a list containing only numbers. We plan to get the average of a set of sequential numbers from the list which keeps rolling from the first number to next number and then to next number and so on. Example The below example simplifies the requirement of finding the average of each 4-length consecutive elements of the list ? Given list: [10, 12, 14, 16, 18, 20, 22, 24, 26] Average of every segment of 4 consecutive numbers: [13.0, 15.0, 17.0, 19.0, 21.0, 23.0] Using sum() and range() We use the ...
Read Moreasksaveasfile() function in Python Tkinter
Tkinter is Python's built-in GUI toolkit. The asksaveasfile() function from tkinter.filedialog opens a save dialog that allows users to specify where to save a file and returns a file object for writing. Syntax asksaveasfile(mode='w', **options) Parameters Common parameters include: mode − File opening mode (default: 'w' for write) filetypes − List of file type tuples defaultextension − Default file extension initialdir − Initial directory to open title − Dialog window title Basic Example Here's how to create a simple save file dialog ? import tkinter as tk ...
Read More