Found 10476 Articles for Python

Longest Valid Parentheses in Python

Arnab Chakraborty
Updated on 26-May-2020 11:41:17

1K+ Views

Suppose we have a string, with opening and closing parentheses. We have to find the longest length of the valid (well-formed) parentheses. So if the input is like “))(())())”, then the result will be 6, as the valid string is “(())()”.To solve this, we will follow these steps −Make a stack, and insert -1., set ans := 0for i in range 0 to length of stack – 1if s[i] is opening parentheses, then insert i into stackotherwiseif stack is not empty and top of stack is not -1 and s[stack top] is opening parentheses, thentop element from stackans := max ... Read More

Merge k Sorted Lists in Python

Arnab Chakraborty
Updated on 26-May-2020 11:30:07

946 Views

Suppose we have some lists, these are sorted. We have to merge these lists into one list. To solve this, we will use the heap data structure. So if the lists are [1, 4, 5], [1, 3, 4], [2, 6], then the final list will be [1, 1, 2, 3, 4, 4, 5, 6].To solve this, we will follow these steps −make one heapfor each linked list l in lists −if is in not 0, then insert I into a heapres := null and res_next := nullDo one infinite loop −temp := min of heapif heap has no element, then ... Read More

Regular Expression Matching in Python

Arnab Chakraborty
Updated on 26-May-2020 11:20:39

509 Views

Suppose we have an input string s and another input string p. Here s is the main string and p is the pattern. We have to define one method, that can match patterns in the string. So we have to implement this for a regular expression, that supports ‘.’ And ‘*’.Dot ‘.’ Matches any single characterStar ‘*’ Matches zero or more of the preceding element.So for example, if the input is like s = “aa” and p = “a.”, then it will be true, for the same input string, if the patter is “.*”, then it will be true.To solve ... Read More

Convert set into a list in Python

Pradeep Elance
Updated on 20-May-2020 11:12:20

343 Views

As part of data analysis using Python we may be required to to convert the data container from set to list. In this article we'll see e how to solve this requirement.With listThis is a straightforward approach in which we directly apply the list function on the given set. The elements get converted into elements of a list.Example Live DemosetA = {'Mon', 'day', '7pm'} # Given Set print("Given set : ", setA) res = (list(setA) ) # Result print("Final list: ", res)OutputRunning the above code gives us the following result −Given set : ['7pm', 'Mon', 'day'] Final list: ['7pm', 'Mon', 'day']With ... Read More

Convert number to list of integers in Python

Pradeep Elance
Updated on 20-May-2020 11:10:01

11K+ Views

As part of data manipulation in Python we may sometimes need to convert a given number into a list which contains the digits from that number. In this article we'll see the approaches to achieve this.With list comprehensionIn the below approach we apply the str function to the given number and then convert into integer through identity function. Finally we wrap the result into a list.Example Live DemonumA = 1342 # Given number print("Given number : ", numA) res = [int(x) for x in str(numA)] # Result print("List of number: ", res)OutputRunning the above code gives us the following result −Given ... Read More

Convert list of tuples to list of strings in Python

Pradeep Elance
Updated on 20-May-2020 11:08:40

818 Views

During data processing using python, we may come across a list whose elements are tuples. And then we may further need to convert the tuples into a list of strings.With joinThe join() returns a string in which the elements of sequence have been joined by str separator. We will supply the list elements as parameter to this function and put the result into a list.Example Live DemolistA = [('M', 'o', 'n'), ('d', 'a', 'y'), ('7', 'pm')] # Given list print("Given list : ", listA) res = [''.join(i) for i in listA] # Result print("Final list: ", res)OutputRunning the above code gives ... Read More

Convert list of tuples to list of list in Python

Pradeep Elance
Updated on 20-May-2020 11:06:54

414 Views

Sometimes we may be given a python list whose elements are tuples. Then we may have a data processing requirement which will need these tuples to be converted to lists for further processing. In this article, we will see how to convert a list of tuples to a list of lists.With list comprehensionIt is a straight forward approach where we create a for loop to loop through each element and apply the list function to create a list of lists.Example Live DemolistA = [('Mon', 3), ('Wed', 4), ('Fri', 7, 'pm')] # Given list print("Given list : ", listA) res = [list(ele) ... Read More

Convert list of tuples into list in Python

Pradeep Elance
Updated on 20-May-2020 11:04:17

838 Views

We may come across a lists whose elements are tuples. But for further data processing we may need to convert the tuples to the normal elements of a list. In this article we will see the approaches to achieve this.With list comprehensionIn this approach we design nested for loops to iterate through each tuple and produce the final list of elements.Example Live DemolistA = [('Mon', 3), ('Wed', 4), ('Fri', 7, 'pm')] # Given list print("Given list : ", listA) res = [item for t in listA for item in t] # Result print("Final list: ", res)OutputRunning the above code gives us ... Read More

Convert list of tuples into digits in Python

Pradeep Elance
Updated on 20-May-2020 11:02:52

194 Views

Python has a wide variety of data manipulation capabilities. We have a scenario in which we are given a list which has elements which are pair of numbers as tuples. In this article we will see how to extract the unique digits from the elements of a list which are tuples.With re and setWe can use regular expression module and its function called sub. It is used to replace a string that matches a regular expression instead of perfect match. So we design a regular expression to convert the tuples into normal strings and then apply the set function to ... Read More

Convert list of strings to list of tuples in Python

Pradeep Elance
Updated on 20-May-2020 10:59:26

353 Views

While manipulating data with python, we may come across a list which has numbers as strings. Further we may want to convert the list of strings into tuples. Of course the given strings are in a particular format of numbers.With map and evalWe will use the map function to apply eval on every element of the list. Then store the final element as a list.Example Live DemolistA = ['21, 3', '13, 4', '15, 7'] # Given list print("Given list : ", listA) # Use eval res = list(map(eval, listA)) # Result print("List of tuples: ", res)OutputRunning the above code gives us ... Read More

Advertisements