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 878 of 2109
Find Min-Max in heterogeneous list in Python
A Python list can contain both strings and numbers, creating what we call a heterogeneous list. In this article, we will explore different methods to find the minimum and maximum numeric values from such mixed-type lists. Finding Minimum Value We can use the isinstance() function to filter only numeric values and then apply the min() function to find the smallest number. Example data = [12, 'Sun', 39, 5, 'Wed', 'Thu'] # Given list print("The Given list:", data) # Filter integers and find minimum res = min(i for i in data if isinstance(i, int)) ...
Read MoreFind Maximum difference pair in Python
Data analysis often requires finding pairs of elements with specific characteristics. In this article, we will explore how to find pairs of numbers in a list that have the maximum difference between them using different Python approaches. Using nlargest() with combinations() This approach finds all possible combinations of elements, calculates their differences, and uses nlargest() from the heapq module to get multiple pairs with maximum differences. Example from itertools import combinations from heapq import nlargest numbers = [21, 14, 30, 11, 17, 18] print("Given list:", numbers) # Find top 2 pairs with ...
Read MoreFind longest consecutive letter and digit substring in Python
A given string may be a mixture of digits and letters. In this article, we will find the longest consecutive substring of letters and the longest consecutive substring of digits separately using two different approaches. Using Regular Expression Module The regular expression module can identify all continuous substrings containing only digits or only letters. We use findall() to extract these substrings and max() with key=len to find the longest ones ? Example import re def longSubstring(text): # Find all consecutive letter sequences letter = max(re.findall(r'\D+', text), ...
Read MoreFind keys with duplicate values in dictionary in Python
While dealing with dictionaries, we may come across situations when there are duplicate values in the dictionary while the keys remain unique. In this article we will see how to find keys that share the same values using different approaches. Method 1: Using Key-Value Exchange We exchange the keys with values of the dictionaries and then keep appending the keys associated with a given value. This way the duplicate values get grouped together ? Example dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3} print("Given Dictionary:", dictA) k_v_exchanged = {} for ...
Read MoreFind k longest words in given list in Python
When working with lists of words, you often need to find the k longest words. Python provides several approaches to accomplish this task efficiently using built-in functions like sorted(), enumerate(), and heapq. Using sorted() with Length as Key The simplest approach is to sort words by length in descending order and slice the first k elements ? def k_longest_words(words, k): return sorted(words, key=len, reverse=True)[:k] words = ['Earth', 'Moonshine', 'Aurora', 'Snowflakes', 'Sunshine'] k = 3 result = k_longest_words(words, k) print(f"Top {k} longest words: {result}") Top 3 longest words: ['Snowflakes', ...
Read MoreFind indices with None values in given list in Python
Many times when dealing with data analysis we may come across None values present in a list. These values cannot be used directly in mathematical operations and string operations. So we need to find their position and either convert them or use them effectively. Using range() with List Comprehension Combining the range() and len() functions we can compare the value of each element with None and capture their index positions using list comprehension ? Example days = ['Sun', 'Mon', None, 'Wed', None, None] # Given list print("Given list :", days) # Using range ...
Read MoreFind fibonacci series upto n using lambda in Python
A Fibonacci series is a widely known mathematical sequence that explains many natural phenomena. It starts with 0 and 1, then each subsequent term is the sum of the two preceding terms. In this article, we will see how to generate a given number of Fibonacci terms using lambda functions in Python. Method 1: Using map() with Lambda We use the map() function to apply a lambda function that adds the sum of the last two terms to our list. The any() function is used to execute the map operation ? Example def fibonacci(count): ...
Read MoreFind elements within range in numpy in Python
Sometimes while processing data using the numpy library, we may need to filter out certain numbers in a specific range. This can be achieved by using some in-built methods available in numpy. Using logical_and() with where() In this approach we take a numpy array then apply the logical_and() function to it. The where() clause in numpy is also used to apply the and condition. The result is an array showing the position of the elements satisfying the required range conditions ? import numpy as np A = np.array([5, 9, 11, 4, 31, 27, 8]) ...
Read MoreFind depth of a dictionary in Python
A Python dictionary can be nested, meaning there are dictionaries within dictionaries. In this article, we will see how to calculate the level of nesting in a dictionary when there are nested dictionaries. Using String Conversion In this approach, we convert the entire dictionary into a string and count the number of opening braces { to determine the nesting level ? Example dictA = {1: 'Sun', 2: {3: {4: 'Mon'}}} dictStr = str(dictA) cnt = 0 for i in dictStr: if i == "{": ...
Read MoreFind common elements in three sorted arrays by dictionary intersection in Python
When working with Python data manipulation, you may need to find elements that are common among multiple arrays. This can be efficiently achieved by converting arrays into dictionaries using the Counter class from the collections module. The approach involves using Counter to count occurrences of each element, then finding the intersection using the & operator. This method preserves the minimum count of common elements across all arrays. Using Counter and Dictionary Intersection Here's how to find common elements using dictionary intersection ? from collections import Counter arrayA = ['Sun', 12, 14, 11, 34] arrayB ...
Read More