Found 10784 Articles for Python

Python - Convert column to separate elements in list of lists

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

231 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

13K+ 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

716 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

106 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

132 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

Collapsible Pane in Tkinter Python

Pradeep Elance
Updated on 18-Feb-2020 12:03:15

826 Views

Tkinter is the GUI building library of python. In this article we will see how we can create a collapsible pane. They are usefult when we have some large amount of data to be displayed over a GUI canvas but we do not want to be displayed always. It is made collapsible so that it can be displayed as and when needed.The below program creates the collapsible pane where we see the result both after expanding and contracting the arrow. The code comments indicate the approach we take at each step.Examplefrom tkinter import * import tkinter as tk from tkinter ... Read More

Binning method for data smoothing in Python

Pradeep Elance
Updated on 18-Feb-2020 11:57:55

914 Views

Many times we use a method called data smoothing to make the data proper and qualitative for statistical analysis. During the smoking process we define a range also called bin and any data value within the range is made to fit into the bin. This is called the binning method. Below is an example of binning. Then we will see how we can achieve the binning method using a Python program.Binning ExampleLet’s take a series of numbers. Find the maximum and minimum values. Decide on the number of bins we need depending on how many data points the analysis needs. ... Read More

ASCII art using Python pyfiglet module

Pradeep Elance
Updated on 18-Feb-2020 11:51:29

2K+ Views

The ASCII text can be used to display many stylish texts by using the module pyfiglet. After installing this module we can use it to control the font that can be used to display the result. In the below program we see various results by choosing various font types.Example# import pyfiglet module import pyfiglet #Text in default font out = pyfiglet.figlet_format("Point") print(out)OutputRunning the above code gives us the following result −Example# import pyfiglet module import pyfiglet #Text in slant font out = pyfiglet.figlet_format("Point", font="slant") print(out)OutputRunning the above code gives us the following result −Example# import pyfiglet module import pyfiglet #Text ... Read More

Advertisements