Found 10476 Articles for Python

Count and display vowels in a string in Python

Pradeep Elance
Updated on 04-Jun-2020 11:19:57

1K+ Views

Given a string of characters let's analyse how many of the characters are vowels.With setWe first find out all the individual and unique characters and then test if they are present in the string representing the vowels.Example Live DemostringA = "Tutorialspoint is best" print("Given String: ", stringA) vowels = "AaEeIiOoUu" # Get vowels res = set([each for each in stringA if each in vowels]) print("The vlowels present in the string: ", res)OutputRunning the above code gives us the following result −Given String: Tutorialspoint is best The vlowels present in the string: {'e', 'i', 'a', 'o', 'u'}with fromkeysThis function enables to extract ... Read More

Converting list string to dictionary in Python

Pradeep Elance
Updated on 04-Jun-2020 11:18:14

1K+ Views

Here we have a scenario where if string is presented which has elements in it making it a list. But those elements can also represent a key-value pair making it dictionary. In this article we will see how to take such a list string and make it a dictionary.With split and slicingIn this approach we use the split function to separate the elements as key value pair and also use slicing to convert the key value pairs into a dictionary format.Example Live DemostringA = '[Mon:3, Tue:5, Fri:11]' # Given string print("Given string : ", stringA) # Type check print(type(stringA)) # using ... Read More

Converting all strings in list to integers in Python

Pradeep Elance
Updated on 04-Jun-2020 11:16:45

2K+ Views

Sometimes we can have a list containing strings but the strings themselves are numbers and closing quotes. In such a list we want to convert the string elements into actual integers.With int()The int function takes in parameters and converts it to integers if it is already a number. So we design a for loop to go through each element of the list and apply the in function. We store the final result into a new list.Example Live DemolistA = ['5', '2', '-43', '23'] # Given list print("Given list with strings : ", listA) # using int res = [int(i) for i ... Read More

Parsing A Boolean Expression in Python

Arnab Chakraborty
Updated on 04-Jun-2020 11:18:09

1K+ Views

Suppose we have a boolean expression, we have to find the result after evaluating that expression.An expression can either be −"t", evaluating to True;"f", evaluating to False;"!(expression)", evaluating to the logical NOT of the inner expression;"&(expr1, expr2, ...)", evaluating to the logical AND of 2 or more inner expressions;"|(expr1, expr2, ...)", evaluating to the logical OR of 2 or more inner expressions;So, if the input is like "|(!(t), &(t, f, t))", then the output will be fasle, this is because !(t) is false, then &(t, f, t) is also false, so the OR of all false values will be false.To ... Read More

Convert two lists into a dictionary in Python

Pradeep Elance
Updated on 04-Jun-2020 11:15:54

5K+ Views

While a Python list contains a series of values a dictionary on the other hand contains a pair of values which are called key-value pairs. In this article we will take two lists and mark them together to create a Python dictionary.With for and removeWe create two nested for loops. In the inner loop will assign one of the list as key for the dictionary while keep removing the values from the list which is outer for loop.Example Live DemolistK = ["Mon", "Tue", "Wed"] listV = [3, 6, 5] # Given lists print("List of K : ", listK) print("list of V ... Read More

Convert string to DateTime and vice-versa in Python

Pradeep Elance
Updated on 04-Jun-2020 11:13:41

251 Views

Python has extensive date and time manipulation capabilities.In this article we'll see in how is string with proper format can we converted to a datetime and the vice versa.With strptimeThis strptime function from datetime module can do the conversion from string to datetime by taking appropriate format specifiers.Example Live Demoimport datetime dt_str = 'September 19 2019 21:02:23 PM' #Given date time print("Given date time: ", dt_str) #Type check print("Data Type: ", type(dt_str)) #Format dtformat = '%B %d %Y %H:%M:%S %p' datetime_val = datetime.datetime.strptime(dt_str, dtformat) print("After converting to date time: ", datetime_val) #Type check print("Data type: ", type(datetime_val)) # Reverting to string ... Read More

Convert string enclosed list to list in Python

Pradeep Elance
Updated on 04-Jun-2020 11:11:47

630 Views

We may sometime get data which contains strings but the structure of the data inside the stream is a Python list. In this article we will convert string enclosed list to an actual Python list which can be further used in data manipulation.With evalWe know the eval function will give us the actual result which is supplied to it as parameter. So so we supplied the given string to the eval function and get back the Python list.Example Live DemostringA = "['Mon', 2, 'Tue', 4, 'Wed', 3]" # Given string print("Given string : ", stringA) # Type check print(type(stringA)) # using ... Read More

Escape a Large Maze Python

Arnab Chakraborty
Updated on 04-Jun-2020 11:00:48

423 Views

Suppose we have a grid, there are 1 million rows and 1 million columns, we also have one list of blocked cells. Now we will start at the source square and want to reach the target square. In each move, we can walk to a up, down, left, right adjacent square in the grid that isn't in the given list of blocked cells.We have to check whether it is possible to reach the target square through a sequence of moves or not.So, if the input is like blocked = [[0, 1], [1, 0]], source = [0, 0], target = [0, ... Read More

Grid Illumination in Python

Arnab Chakraborty
Updated on 04-Jun-2020 10:49:36

452 Views

Suppose we have a N x N grid of cells, in each cell (x, y) there is a lamp. Initially, some of the lamps are on. The lamps[i] is the location of the i-th lamp that is on. Each lamp that is on glows every square on its x-axis, y-axis, and both diagonals. Now for the i-th query i.e. queries[i] = (x, y), the answer to the query is 1 if the cell (x, y) is glowed, otherwise 0. After each query (x, y), we turn off any lamps that are at cell (x, y) or are adjacent 8-directionally. Return ... Read More

Smallest Good Base in Python

Arnab Chakraborty
Updated on 01-Jun-2020 11:23:44

351 Views

Suppose we have an integer n, we call k >= 2 as a good base of n, when all digits of n base k are 1. So if the number n is given as string, we have to return smallest good base of n as string. So if the number is say 121, then the answer will be 3, as 121 in base 3 is 11111.To solve this, we will follow these steps −Define a method called getSum(), this will take x and lengthset mainSum := 0 and temp := 1for i in range 0 to length – 1 −mainSum ... Read More

Advertisements