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
Python Articles
Page 116 of 852
Find all possible substrings after deleting k characters in Python
we are given a string. The required task is to take out one letter from the string and print the remaining letters in the string. And this we have to so for each letter of the string.With loops and rangeThis is a basic programming approach in which we first list out the parameters required like declare the string, create variables for start and end positions and create a temporary placeholder for each of the letters. The we create a function that will iterate through each of the letters and create a string of remaining letters.Examplelist = [] def letterCombinations(s, ...
Read MoreFind all triplets in a list with given sum in Python
In a list of numbers we want to find out which three elements can join to give a certain sum. We call it a triplet. And in the list there can be many such triplets. For example, the sum 10 can be generated form numbers 1, 6, 3 as well as 1, 5, 4. In this article we will see how to find out all such triplets from a given list of numbers.Using range and temp variablesThis is the traditional approach in which we will create temporary variables. These variables will hold the elements from the list and check if ...
Read MoreFind depth of a dictionary in Python
A python dictionary can be nested i.e., there are dictionaries within a dictionary. In this article we will see how to calculated the level of nesting in a dictionary when there is a nested dictionary.With string conversionIn this approach we convert the entire dictionary into a string. Then we count the number of left { that indicate to which level the dictionaries are nested.ExampledictA = {1: 'Sun', 2: {3: {4:'Mon'}}} dictStr = str(dictA) cnt = 0 for i in dictStr : if i == "{": cnt += 1 print("The depth of dictionary: ", cnt)OutputRunning the above ...
Read MoreFind fibonacci series upto n using lambda in Python
A fibinacci series is a widley known mathematical series that explains many natural phenomenon. It starts with 0 and 1 and then goes on adding a term to its previous term to get the next term. In this article we will see how to generate a given number of elements of the Fibonacci series by using the lambda function in python.With sum and mapWe use the map function to apply the lambda function to each element of the list. We design a list slicing mechanism to get sum of previous two terms and use range to keep count of how ...
Read MoreFind indices with None values in given list in Python
Manytime when dealing with data analysis we may come across the None values present in a list. These values can not be used directly in mathematical operations and string operations etc. So we need to find their position and either convert them or use them effectively.With range()Combining the range and len function we can compare the value of each element with None and capture their index positions. Of course we use a for loop design to achieve this.ExamplelistA = ['Sun', 'Mon', None, 'Wed', None, None] # Given list print("Given list : ", listA) # Using range positions = ...
Read MoreFind k longest words in given list in Python
We have a scenario where we have to pick the top n longest word from a list containing many words of varying length. In this article we will see various approaches to achieve that.With count() and sorted()We first sort the elements of the list in the reverse order so that the longest words are available at the beginning of the list. Then find the length of each word and add the result of the count to a variable. Finally take a slice of the required number of longest words we need.Examplefrom itertools import count def longwords(l, x): c ...
Read MoreFind keys with duplicate values in dictionary in Python
While dealing with dictionaries, we may come across situation when there are duplicate values in the dictionary while obviously the keys remain unique. In this article we will see how we can achieve thi.Exchanging keys and valuesWe exchange the keys with values of the dictionaries and then keep appending the values associated with a given key. This way the duplicate values get clubbed and we can see them in the resulting new dictionary.ExampledictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3} print("Given Dictionary :", dictA) k_v_exchanged = {} for key, value in dictA.items(): if value ...
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 are required to find the biggest substring that has letters and digits together.with re moduleThe regular expression module can be used to find all the continuous substring having digits or letters. Then we apply the max function to pick only those continuous substrings of letters and digits which have maximum length among all the substrings found. The findall function is also use to identify and get the required substrings.Exampleimport re def longSubstring(str): letter = max(re.findall(r'\D+', str), key=len) digit = max(re.findall(r'\d+', str), key=len) ...
Read MoreFind Maximum difference pair in Python
Data analysis can throw a variety of challenges. In this article we will take a list with numbers as its elements. Then we will find such pairs of elements in the list which has maximum difference in value between them.With nlargestThe approach here is to first find out all possible combinations of elements and then subtract the second element from the first. Finally apply the nlargest function form heapq module to get those pairs where the difference is maximum.Examplefrom itertools import combinations from heapq import nlargest listA = [21, 14, 30, 11, 17, 18] # Given list print("Given ...
Read MoreFind Min-Max in heterogeneous list in Python
A python list can contain both strings and numbers. We call it a heterogeneous list. In this article we will take such a list and find the minimum and maximum number present in the list.Finding MinimumIn this approach we will take the isinstance function to find only the integers present in the list and then apply the min function to get the minimum value out of it.ExamplelistA = [12, 'Sun', 39, 5, 'Wed', 'Thus'] # Given list print("The Given list : ", listA) res = min(i for i in listA if isinstance(i, int)) # Result print("The minimum ...
Read More