Found 10783 Articles for Python

Find elements within range in numpy in Python

Pradeep Elance
Updated on 26-Aug-2020 07:41:23

2K+ Views

Sometime 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.With and operatorIn this approach we take an 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]) # printing initial array print("Given Array : ", A) ... Read More

Find depth of a dictionary in Python

Pradeep Elance
Updated on 26-Aug-2020 07:40:38

672 Views

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.Example Live DemodictA = {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 ... Read More

Find common elements in three sorted arrays by dictionary intersection in Python

Pradeep Elance
Updated on 26-Aug-2020 07:38:40

242 Views

While manipulating data using python we may come across situation where we need to find the elements which are common among the multiple arrays. This can be achieved by converting the arrayd into dictionaries as shown below.In the below example we take the arrays and apply the Counter container from collections module. It will hold the count of each of the elements present in the container. Then we convert them to a dictionary by applying dict() and using the & operator to identify only the common elements among the arrays. Finally we loop through the items of the newly created ... Read More

Find all triplets in a list with given sum in Python

Pradeep Elance
Updated on 26-Aug-2020 07:37:24

1K+ Views

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 More

Find all possible substrings after deleting k characters in Python

Pradeep Elance
Updated on 26-Aug-2020 07:33:55

137 Views

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.Example Live Demolist = [] def ... Read More

Find all elements count in list in Python

Pradeep Elance
Updated on 26-Aug-2020 07:31:51

242 Views

Many times we need to count the elements present in a list for some data processing. But there may be cases of nested lists and counting may not be straight forward. In this article we will see how to handle these complexities of counting number of elements in a list.With For loopIn this approach we use two for loops to go through the nesting structure of list. In the below program we have nested list where inner elements have different number of elements inside them. We also apply the len() function to calculate the length of the flattened list.Example Live DemolistA ... Read More

Find all distinct pairs with difference equal to k in Python

Pradeep Elance
Updated on 26-Aug-2020 07:30:23

286 Views

In this article we are going to see how to count the numbers of pairs of numbers which have an exact difference equal to k. The given numbers are in form of a list and we supply the value of k to the program.Using for LoopIn this approach we design two for loops, one inside another. The outer for loop keeps track of visiting each element of the given list. The inner for loop keeps comparing each of the remaining elements with the element of the outer loop and increase the value of the count variable if it matches the ... Read More

Extracting rows using Pandas .iloc[] in Python

Pradeep Elance
Updated on 26-Aug-2020 07:27:29

502 Views

Pandas is a famous python library that Is extensively used for data processing and analysis in python. In this article we will see how to use the .iloc method which is used for reading selective data from python by filtering both rows and columns from the dataframe.iloc method processes data by using integer based indexes which may or may not be part of the original data set. The first row is assigned index 0 and second and index 1 and so on. Similarly, the first column is index 0 and second is index 1 and so on.The Data SetBelow is ... Read More

Duplicate substring removal from list in Python

Pradeep Elance
Updated on 26-Aug-2020 07:18:09

278 Views

Sometimes we may have a need to refine a given list by eliminating the duplicate elements in it. This can be achieved by using a combination of various methods available in python standard library.with set and splitThe split method can be used to segregate the elements for duplicate checking and the set method is used to store the unique elements from the segregated list elements.Example# initializing list listA = [ 'xy-xy', 'pq-qr', 'xp-xp-xp', 'dd-ee'] print("Given list : ", listA) # using set() and split() res = [set(sub.split('-')) for sub in listA] # Result print("List after duplicate removal ... Read More

dir() Method in Python

Pradeep Elance
Updated on 26-Aug-2020 07:04:03

417 Views

The dir() function returns list of the attributes and methods of any object like functions , modules, strings, lists, dictionaries etc. In this article we will see how to use the dir() in different ways in a program and for different requirements.Only dir()When we print the value of the dir() without importing any other module into the program, we get the list of methods and attributes that are available as part of the standard library that is available when a python program is initialized.ExamplePrint(dir())OutputRunning the above code gives us the following result −['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', ... Read More

Advertisements