Found 10476 Articles for Python

Python - cmp() Method

Pradeep Elance
Updated on 03-Mar-2020 06:30:00

4K+ Views

The cmp() is part of the python standard library which compares two integers. The result of comparison is -1 if the first integer is smaller than second and 1 if the first integer is greater than the second. If both are equal the result of cmp() is zero.Below example illustrates different scenario showing the use of cmp() method.Example Live Demodef cmp(x, y):    return (x > y) - (x < y) #x>y x = 5 y = 3 print("The cmp value for x>y is : ",cmp(x, y),"") #xy is : 1 The cmp value for x

Python - Clearing list as dictionary value

Pradeep Elance
Updated on 03-Mar-2020 06:27:16

169 Views

In this article we consider a dictionary where the values are presented as lists. Then we consider clearing those values from the lists. We have two approaches here. One is to use the clear methods and another is to designate empty values to each key using list comprehension.Example Live Demox1 = {"Apple" : [4, 6, 9, 2], "Grape" : [7, 8, 2, 1], "Orange" : [3, 6, 2, 4]} x2 = {"mango" : [4, 6, 9, 2], "pineapple" : [7, 8, 2, 1], "cherry" : [3, 6, 2, 4]} print("The given input is : " + str(x1)) # using loop + ... Read More

Python - Check if frequencies of all characters of a string are different

Pradeep Elance
Updated on 03-Mar-2020 06:21:19

210 Views

In this article we will see how to find the frequency of each character in a given string. Then see if two or more characters have the same frequency in the given string or not. We will accomplish this in two steps. In the first program we will just find out the frequency of each character.Frequency of each characterHere we find the frequency of each character in the given input screen. We declare a empty dictionary and then add each character as a string. We also assign keys to each of the character to create the key-value pair needed by ... Read More

Python - Check if dictionary is empty

Pradeep Elance
Updated on 03-Mar-2020 06:17:58

6K+ Views

During analysis of data sets we may come across situations where we have to deal with empty dictionaries. In tis article we will see how to check if a dictionary is empty or not.Using ifThe if condition evaluates to true if the dictionary has elements. Otherwise it evaluates to false. So in the below program we will just check the emptiness of a dictionary using only the if condition.Example Live Demodict1 = {1:"Mon", 2:"Tue", 3:"Wed"} dict2 = {} # Given dictionaries print("The original dictionary : " ,(dict1)) print("The original dictionary : " ,(dict2)) # Check if dictionary is empty if dict1: ... Read More

Python - Check if all the values in a list are less than a given value

Pradeep Elance
Updated on 03-Mar-2020 06:12:29

1K+ Views

In python data analysis, we sometime face a situation where we need to compare a given number with a list containing many values. In this article we need to fins if a given number is less than each of the values present in a given list. We are going to achieve it using the following two ways.Using for loopWe iterate through the given list and compare the given value with each of the values in the list. Once all values from the list are compared and the comparison condition holds good in each of the step, we print out the ... Read More

Possible Words using given characters in Python

Pradeep Elance
Updated on 03-Mar-2020 06:11:04

850 Views

In this article we are going to see a python program that will give output of possible words from a given set of characters. Here we are taking a list as an input which will contain the set of reference words and another list containing the characters from which the words are made up of.In the below program, we define two functions. One to take the letters from the second list and make up words. Another function to match the words formed with the words present in the given list of words.Example Live Demodef Possible_Words(character):    x = {}    for ... Read More

Accessing all elements at given Python list of indexes

Pradeep Elance
Updated on 03-Mar-2020 05:31:23

343 Views

We can access the individual elements of a list using the [] brackets and the index number. But when we need to access some of the indices then we cannot apply this method. We need the below approaches to tackle this.Using two listsIn this method, along with the original list, we take the indices as another list. Then we use a for loop to iterate through the indices and supply those values to the main list for retrieving the values.Example Live Demogiven_list = ["Mon", "Tue", "Wed", "Thu", "Fri"] index_list = [1, 3, 4] # printing the lists print("Given list : ... Read More

Find K-Length Substrings With No Repeated Characters in Python

Arnab Chakraborty
Updated on 29-Apr-2020 08:31:57

530 Views

Suppose we have a string S, we have to find the number of substrings of length K where no characters are repeated. So if S = “heyfriendshowareyou” and K is 5, then output will be 15, as the strings are [heyfr, eyfri, yfrie, frien, riend, iends, endsh, ndsho, dshow, showa, howar, oware, warey, areyo, reyou]To solve this, we will follow these steps −create one empty map m, and left := 0 and right := -1 and ans := 0while right < length of string – 1if right – left + 1 = k, thenincrease ans by 1decrease m[str[left]] by 1increase ... Read More

Keys and Rooms in Python

Arnab Chakraborty
Updated on 29-Apr-2020 08:29:03

526 Views

Suppose we have N rooms and we start in room 0. In each room there exists a distinct number in 0, 1, 2, ..., N-1, and each room may have some keys to access the next room. So in other words, each room i has a list of keys rooms[i], and each key rooms[i][j] is an integer in [0, 1, ..., N-1] where N = number of rooms. A key rooms[i][j] = v, this opens the room with number v So if the input is [[1], [2], [3], []]. then output will be true. There are few more points that ... Read More

Maximum Swap in Python

Arnab Chakraborty
Updated on 29-Apr-2020 06:32:59

1K+ Views

Suppose we have a non-negative integer; we could swap two digits at most once to get the maximum valued number. We have to return the maximum valued number we can get. So if the input is like 2736 then the output will be 7236. So here we are swapping 2 and 7.To solve this, we will follow these steps −num := cut each digit from the number, and make a listnum1 := sort the num in reverse orderindex := 0while index < length of numif num1[index] is not same as num[index]a := subarray of num from index (index + 1) ... Read More

Advertisements