Found 10476 Articles for Python

Two Sum Less Than K in Python

Arnab Chakraborty
Updated on 28-Apr-2020 08:31:32

855 Views

Suppose we have an array A of integers and another integer K is given. We have to find the maximum S such that there exists i < j with A[i] + A[j] = S and S < K. If there is no i, j exists satisfying this equation, then return -1. So for example if A = [34, 23, 1, 24, 75, 33, 54, 8] and K = 60, then the output will be 58, as we can use 34 and 24 to sum 58, which is less than 60.To solve this, we will follow these steps −res = - ... Read More

Index Pairs of a String in Python

Arnab Chakraborty
Updated on 28-Apr-2020 08:27:27

926 Views

Suppose we have a text string and words (a list of strings), we have to find all index pairs [i, j] such that the substring text[i]...text[j] is in the list of words. So if the string is like “ababa” and words array is like [“aba”, “ab”], then the output will be [[0, 1], [0, 2], [2, 3], [2, 4]]. One thing we can notice, that the matches can overlap, the “aba” is matched in [0, 2] and [2, 4].To solve this, we will follow these steps −res := an empty listfor i in range 0 to a length of stringfor ... Read More

Fixed Point in Python

Arnab Chakraborty
Updated on 28-Apr-2020 08:24:35

1K+ Views

Suppose we have an array A of unique integers sorted in ascending order, we have to return the smallest index i that satisfies A[i] == i. Return -1 if no such i exists. So if the array is like [-10, -5, 0, 3, 7], then the output will be 3, as A[3] = 3 the output will be 3.To solve this, we will follow these steps −For i in range 0 to length of Aif i = A[i], then return ireturn -1Example(Python)Let us see the following implementation to get a better understanding − Live Democlass Solution(object):    def fixedPoint(self, A):   ... Read More

Python - Convert column to separate elements in list of lists

Pradeep Elance
Updated on 26-Feb-2020 08:11:19

305 Views

In analyzing data or processing data using python, we come across situations where the given list has to be remodeled or reshaped to get lists with different columns. We can achieve that with multiple approaches as discussed below.Using SlicingWe can slice the list at certain elements to create the columnar structure. Here we convert the given list into a new list where the elements are split form the middle. We sue two for loops. The outer one splits the elements from the zeroth element to the second element and the inner one from the second element to the last.Example Live Demox ... Read More

Python - Convert a set into dictionary

Pradeep Elance
Updated on 26-Feb-2020 08:06:33

3K+ Views

Python provides lot of flexibility to handle different types of data structures. There may be a need when you have to convert one Data Structure to another for a better use or better analysis of the data. In this article we will see how to convert a Python set to a Python dictionary.Using zip and dictThe dict() can be used to take input parameters and convert them to a dictionary. We also use the zip function to group the keys and values together which finally become the key value pair in the dictionary.Example Live Demolist_keys = {1, 2, 3, 4} list_values ... Read More

How to print double quotes with the string variable in Python?

Pradeep Elance
Updated on 26-Feb-2020 07:54:40

14K+ Views

Printing double quotes is tricky, as it itself is required as part of syntax to print the strings by surrounding them. In this article we will see how these double quotes can be printed using print statement.The below scenarios will not print the double quote. The first two lines of code will give no output while the last one will through error.Example Live Demoprint(" ") print(" " " ") print(""aString"")OutputRunning the above code gives us the following result −;print(""aString"") ^ SyntaxError: invalid syntaxBut if we surround the strings with proper quotes as shown below, then the quotes can themselves get printed. ... Read More

Bigram formation from given a Python list

Pradeep Elance
Updated on 26-Feb-2020 07:42:18

821 Views

A bigram is formed by creating a pair of words from every two consecutive words from a given sentence. In python, this technique is heavily used in text analytics. Below we see two approaches on how to achieve this.Using enumerate and splitUsing these two methods we first split the sentence into multiple words and then use the enumerate function to create a pair of words from consecutive words.Example Live Demolist = ['Stop. look left right. go'] print ("The given list is : " + str(list)) # Using enumerate() and split() for Bigram formation output = [(k, m.split()[n + 1]) for m ... Read More

Avoiding quotes while printing strings in Python

Pradeep Elance
Updated on 26-Feb-2020 07:39:41

184 Views

If we print a given list of strings as it is, we have to use quotes and fill in a pair of matching quotes appropriately. We can avoid using quotes in the print statements by following two approaches.Using join()The join method helps us in printing the output of list elements by using any separator we choose. In the below example we choose ** as separator.Example Live Demolist = ['Mon', 'Tue', 'Wed'] # The given list print("The given list is : " + str(list)) print("The formatted output is : ") print(' ** '.join(list))OutputRunning the above code gives us the following result −The ... Read More

askopenfile() function in Python Tkinter

Pradeep Elance
Updated on 26-Feb-2020 07:36:06

2K+ Views

Instead of hard coding the path to a file to be used by a python program, we can allow the user to browse the os folder structure using a GUI and let the user select the file. This is achieved using the tkinter module in which we define a canvas and put a button on it to browse the files.In the below program, we define a file opener function. We only use this function to open a text file as python can read the content of a text file and print it out in a much readable manner. We can ... Read More

Python - Get items in sorted order from given dictionary

Pradeep Elance
Updated on 18-Feb-2020 12:09:13

190 Views

The Python dictionary has key and value pairs. In some situation we will need the items of the dictionary to be sorted according to the keys. In this article we'll see the different ways to get a sorted output from the items in the dictionary.Using Operator ModuleThe Operator module has itemgetter function which can take 0 as the index of input parameter for the keys of the dictionary. We apply the sorted function on top of itemgetter and get the sorted output.Example Live Demodict = {12 : 'Mon', 21 : 'Tue', 17: 'Wed'} import operator print("Given dictionary", str(dict)) print ("sorted order ... Read More

Advertisements