Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Articles by Pradeep Elance
Page 10 of 31
Python Generate successive element difference list
In this as its elements article we will see how to find the difference between the two successive elements for each pair of elements in a given list. The list has only numbers as its elements.With IndexUsing the index of the elements along with the for loop, we can find the difference between the successive pair of elements.Example Live DemolistA = [12, 14, 78, 24, 24] # Given list print("Given list : ", listA) # Using Index positions res = [listA[i + 1] - listA[i] for i in range(len(listA) - 1)] # printing result print ("List with successive difference in elements ...
Read MorePython Generate random string of given length
In this article we will see how how to generate a random string with a given length. This will be useful in creating random passwords or other programs where randomness is required.With random.choicesThe choices function in random module can produce strings which can then be joined to create a string of given length.Example Live Demoimport string import random # Length of string needed N = 5 # With random.choices() res = ''.join(random.choices(string.ascii_letters+ string.digits, k=N)) # Result print("Random string : ", res)OutputRunning the above code gives us the following result −Random string : nw1r8With secretsThe secrets module also has choice method which ...
Read MorePython Generate random numbers within a given range and store in a list
In this article we will see how to generate Random numbers between a pair of numbers and finally store those values into list.We use a function called randint. First let's have a look at its syntax.Syntaxrandint(start, end) Both start and end should be integers. Start should be less than end.In this example we use the range function in a for lowith help of append we generate and add these random numbers to the new empty list.Example Live Demoimport random def randinrange(start, end, n): res = [] for j in range(n): res.append(random.randint(start, end)) return res # ...
Read MorePython Categorize the given list by string size
Let's consider a list containing many strings of different lengths. In this article we will see how to club those elements into groups where the strings are of equal length in each group.With for loopWe design a for loop which will iterate through every element of the list and happened it only to the list where its length matches with the length of existing element.Example Live DemolistA = ['Monday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] # Given list print("Given list : ", listA) # Categorize by string size len_comp = lambda x, y: len(x) == len(y) res = [] for sub_list in listA: ...
Read MoreConverting an image to ASCII image in Python
In this article we we want to convert a given images into a text bases image also called ASCII image.Below is the Python program which will take an input imagee and various functions to convert them into grayscale picture and then apply the ASCII characters to create different patterns insert the image. Finally the emails comes text based image this is a series of plane ASCII characters.Examplefrom PIL import Image import os ASCII_CHARS = [ '#', '?', '%', '.', 'S', '+', '.', '*', ':', ', ', '@'] def resize_image(image, new_width=25): (input__width, input__height) = image.size aspect_ratio = input__height/float(input__width) ...
Read MoreBoolean list initialization in Python
There are scenarios when we need to get a list containing only the Boolean values like true and false. In this article how to create list containing only Boolean values.With rangeWe use range function giving it which is the number of values we want. Using a for loop we assign true or false today list as required.Example Live Demores = [True for i in range(6)] # Result print("The list with binary elements is : " ,res)OutputRunning the above code gives us the following result −The list with binary elements is : [True, True, True, True, True, True]With * operatorThe * operator ...
Read MoreBinary element list grouping in Python
Suppose we have a list of lists in which each sublist has two elements. One element of each sublist is common across many other subjects of the list. We need to create a final list which will show sublists grouped by common elements.With set and mapIn the given list the first element is a string and the second element is a number. So we create a temp list which will hold the second element of each sublist. Then we compare is sublist with each of the element in the temp list and designer follow to group them.Example Live DemolistA = [['Mon', ...
Read MoreAdd the occurrence of each number as sublists in Python
We have a list whose elements are numeric. Many elements are present multiple times. We want to create sub list so the frequency of each element along with the elements itself.With for and appendIn this approach we will compare each element in the list with every other elements after it. If there is a match then count will be incremented and both the element and the count will be made into a subsist. List will be made which should contain subsists showing every element and its frequency.Example Live Demodef occurrences(list_in): for i in range(0, len(listA)): a = ...
Read MoreAdd only numeric values present in a list in Python
We have a Python list which contains both string and numbers. In this article we will see how to sum up the numbers present in such list by ignoring the strings.With filter and isinstanceThe isinstance function can be used to filter out only the numbers from the elements in the list. Then we apply and the sum function and get the final result.Example Live DemolistA = [1, 14, 'Mon', 'Tue', 23, 'Wed', 14, -4] #Given dlist print("Given list: ", listA) # Add the numeric values res = sum(filter(lambda i: isinstance(i, int), listA)) print ("Sum of numbers in listA: ", res)OutputRunning the ...
Read MoreAccessing index and value in a Python list
When we use a Python list, will be required to access its elements at different positions. In this article we will see how to get the index of specific elements in a list.With list.IndexThe below program sources the index value of different elements in given list. We supply the value of the element as a parameter and the index function returns the index position of that element.Example Live DemolistA = [11, 45, 27, 8, 43] # Print index of '45' print("Index of 45: ", listA.index(45)) listB = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'] # Print index of 'Wed' print("Index of ...
Read More