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
Articles on Trending Technologies
Technical articles with clear explanations and examples
Remove all duplicates from a given string in Python
To remove all duplicates from a string in Python, we need to first split the string by spaces so that we have each word in an array. Then there are multiple ways to remove duplicates from the resulting list. We can remove duplicates by first converting all words to lowercase, then sorting them and finally picking only the unique ones. Let's explore different approaches ? Using Manual Loop with Sorting This approach converts words to lowercase, sorts them, and uses a loop to extract unique words ? sent = "Hi my name is John Doe ...
Read MoreTokenize text using NLTK in python
Tokenization is the process of breaking down text into individual pieces called tokens. In NLTK and Python, tokenization converts a string into a list of tokens, making it easier to process text word by word instead of character by character. For example, given the input string ? Hi man, how have you been? We should get the output ? ['Hi', 'man', ', ', 'how', 'have', 'you', 'been', '?'] Basic Word Tokenization NLTK provides the word_tokenize() function to split text into words and punctuation marks ? from nltk.tokenize import ...
Read MorePython program to sort out words of the sentence in ascending order
In order to sort the words of a sentence in ascending order, we first need to split the sentence into words using space as the splitting point. For simplicity, we'll only be splitting on space and let the punctuation be there. We can use replace or regex to remove that as well. Once we split the sentence, we can sort the words lexicographically (like in a language dictionary) using either sort() or sorted() methods depending on whether we want to sort the array in place or create a new sorted array. Using sort() Method (In-Place Sorting) When ...
Read MoreFind all the patterns of \"10+1\" in a given string using Python Regex
The term "10+1" is a specific pattern in a binary string that starts with a digit '1' followed by at least one or more '0' and ending with a '1'. In regular expressions, this pattern is represented as 10+1. Understanding the Pattern The regex pattern 10+1 breaks down as follows ? 1 − matches the literal character '1' 0+ − matches one or more occurrences of '0' 1 − matches the literal character '1' Using re.findall() The re.findall() method accepts a pattern and a string as parameters, finds the given pattern in the ...
Read MoreHow can I remove the same element in the list by Python
A list is a built-in Python data structure that stores an ordered collection of items. Lists often contain duplicate elements, which can cause data inaccuracies. In this article, we will explore several approaches to remove duplicate elements from a list while preserving the original order. Using set() Function The set() function accepts an iterable (like a list, tuple, or string) as a parameter and creates a set object. Since a set object does not accept duplicate values, converting a list to a set automatically removes duplicates. Example The following example removes duplicate elements using the set() ...
Read MoreCan someone help me fix this Python Program?
This Python program demonstrates fixing common syntax and logical errors in a conversational script. The original code had indentation problems and undefined variable issues that prevented it from running correctly. Common Issues in the Original Code The main problems were: Indentation errors − Missing indentation inside if blocks Undefined variables − Variable 'name' used before assignment Python 2 print syntax − Using print statements instead of print() function Logic flow − Variable defined in one branch but used in another Corrected Version Here's the fixed code with proper indentation, variable handling, and Python ...
Read MoreReply to user text using Python
You can create an interactive text-based response system using Python's if-elif-else statements and while loops. This approach ensures the program continues asking for input until the user provides a valid option. The key is to use int() to convert user input to an integer and validate it against the available options. Example Here's a complete interactive response system − print("Come-on in. Need help with any bags?") while True: # Loop continues until valid option is selected try: bag ...
Read MorePython - How to convert this while loop to for loop?
Converting a while loop to a for loop in Python can be achieved using itertools.count() which creates an infinite iterator. This is particularly useful when you need to iterate indefinitely until a specific condition is met. Understanding itertools.count() The count() function from the itertools module generates an iterator of evenly spaced values. It takes two optional parameters: start − Starting value (default is 0) step − Step size between values (default is 1) Using default parameters creates an infinite iterator, so you must use break to terminate the loop when needed. Example: Converting ...
Read MoreHow to get signal names from numbers in Python?
Python's signal module allows you to work with system signals, but getting signal names from their numeric values requires some introspection. You can create a mapping dictionary by examining the signal module's attributes and filtering for signal constants. Creating a Signal Number to Name Mapping The signal module contains all signal constants as attributes. We can iterate through these attributes to build a mapping ? import signal # Get all signal module attributes and create mapping sig_items = reversed(sorted(signal.__dict__.items())) signal_map = dict((k, v) for v, k in sig_items if v.startswith('SIG') and not v.startswith('SIG_')) print("Signal ...
Read MoreHow to print Narcissistic(Armstrong) Numbers with Python?
A narcissistic number (also known as an Armstrong number) is a number that equals the sum of its digits, each raised to the power of the number of digits. For example, 370 = 33 + 73 + 03 = 27 + 343 + 0 = 370. Algorithm Steps The algorithm to check for an Armstrong number follows these steps ? Determine the number of digits for the mentioned number. Extract each digit and calculate the power of that digit with the exponent equal to the number of digits. ...
Read More