Found 10476 Articles for Python

Find same contacts in a list of contacts in Python

Arnab Chakraborty
Updated on 19-Aug-2020 11:16:45

258 Views

Suppose we have a list of contacts holding username, email and phone number in any order, we have to find the same contacts (When same person have many different contacts) and return the same contacts together. We have to keep in mind that −A contact can store username, email and phone fields according to any order.Two contacts are same if they have either same username or same email or same phone number.So, if the input is like Contacts = [{"Amal", "amal@gmail.com", "+915264"}, { "Bimal", "bimal321@yahoo.com", "+1234567"}, { "Amal123", "+1234567", "amal_new@gmail.com"}, { "AmalAnother", "+962547", "amal_new@gmail.com"}], then the output will be [0, ... Read More

Find multiplication of sums of data of leaves at same levels in Python

Arnab Chakraborty
Updated on 19-Aug-2020 11:12:34

130 Views

Suppose we have a Binary Tree. We have to perform following operations −For each level, find sum of all leaves if there are leaves at this level. Otherwise ignore it.Find multiplication of all sums and return it.So, if the input is likethen the output will be 270. First two levels have no leaves. Third level has single leaf 9. Last level has four leaves 2, 12, 5 and 11. So result is 9 * (2 + 12 + 5 + 11) = 270To solve this, we will follow these steps −if root is null, thenreturn 0res := 1que := a ... Read More

Find pairs with given sum such that pair elements lie in different BSTs in Python

Arnab Chakraborty
Updated on 19-Aug-2020 11:03:13

133 Views

Suppose we have two given Binary Search Trees and another sum is given; we have to find pairs with respect of given sum so that each pair elements must be in different BSTs.So, if the input is like sum = 12then the output will be [(6, 6), (7, 5), (9, 3)]To solve this, we will follow these steps −Define a function solve() . This will take trav1, trav2, Sumleft := 0right := size of trav2 - 1res := a new listwhile left < size of trav1 and right >= 0, doif trav1[left] + trav2[right] is same as Sum, theninsert (trav1[left], ... Read More

Find pairs with given sum such that elements of pair are in different rows in Python

Arnab Chakraborty
Updated on 19-Aug-2020 11:00:13

182 Views

Suppose we have a matrix of unique elements and a sum; we have to find all the pairs from the matrix whose sum is equal to given sum. Here, each element of pair will be taken from different rows.So, if the input is like −24356987101114121311516sum = 13, then the output will be [(2, 11), (4, 9), (3, 10), (5, 8), (12, 1)]To solve this, we will follow these steps −res := a new listn := size of matrixfor i in range 0 to n, dosort the list matrix[i]for i in range 0 to n - 1, dofor j in range ... Read More

Python - Write multiple files data to master file

Nizamuddin Siddiqui
Updated on 08-Aug-2020 08:27:16

560 Views

File handling is an important part of any web application.Python has several functions for creating, reading, updating, and deleting files.To write to an existing file, you must add a parameter to the open()function −"a" − Append − will append to the end of the file"w" − Write − will overwrite any existing contentExampleimport os # list the files in directory lis = os.listdir('D:\python' '\data_files\data_files') print(lis) tgt = os.listdir('D:\python' '\data_files\target_file')   file_dir ='D:\python\data_files\data_files' out_file = r'D:\python\data_files\target_file\master.txt' ct = 0   print('target file :', tgt) try:    # check for if file exists    # if yes delete the file    # otherwise ... Read More

Python - Working with PNG Images using Matplotlib

Nizamuddin Siddiqui
Updated on 08-Aug-2020 08:23:24

905 Views

Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack.Example#applying pseudocolor # importing pyplot and image from matplotlib import matplotlib.pyplot as plt import matplotlib.image as img   # reading png image im = img.imread('imR.png')   # applying pseudocolor # default value of colormap is used. lum = im[:, :, 0]   # show image plt.imshow(lum) #colorbar # importing pyplot and image from matplotlib import matplotlib.pyplot as plt import matplotlib.image as img # reading png image im = ... Read More

Python - Working with Pandas and XlsxWriter

Nizamuddin Siddiqui
Updated on 08-Aug-2020 08:21:39

332 Views

Python Pandas is a data analysis library. It can read, filter and re-arrange small and large datasets and output them in a range of formats including Excel.Pandas writes Excel files using the XlsxWriter modules.XlsxWriter is a Python module for writing files in the XLSX file format. It can be used to write text, numbers, and formulas to multiple worksheets. Also, it supports features such as formatting, images, charts, page setup, auto filters, conditional formatting and many others.Example# import pandas as pd import pandas as pd # Create some Pandas dataframes from some data. df1 = pd.DataFrame({'Data': [11, 12, 13, 14]}) df2 = pd.DataFrame({'Data': [21, ... Read More

Python - Working with .docx module

Nizamuddin Siddiqui
Updated on 08-Aug-2020 08:16:34

9K+ Views

Word documents contain formatted text wrapped within three object levels. Lowest level- Run objects, Middle level- Paragraph objects and Highest level- Document object.So, we cannot work with these documents using normal text editors. But we can manipulate these word documents in python using the python-docx module.The first step is to install this third-party module python-docx. You can use pip “pip install python-docx”After installation import “docx” NOT “python-docx”.Use “docx.Document” class to start working with the word document.Example# import docx NOT python-docx import docx # create an instance of a word document doc = docx.Document() # add a heading of level 0 ... Read More

Python - Word Embedding using Word2Vec

Nizamuddin Siddiqui
Updated on 08-Aug-2020 08:15:04

584 Views

Word Embedding is a language modeling technique used for mapping words to vectors of real numbers. It represents words or phrases in vector space with several dimensions. Word embeddings can be generated using various methods like neural networks, co-occurrence matrix, probabilistic models, etc.Word2Vec consists of models for generating word embedding. These models are shallow two-layer neural networks having one input layer, one hidden layer and one output layer.Example# importing all necessary modules from nltk.tokenize import sent_tokenize, word_tokenize import warnings warnings.filterwarnings(action = 'ignore') import gensim from gensim.models import Word2Vec   #  Reads ‘alice.txt’ file sample = open("C:\Users\Vishesh\Desktop\alice.txt", "r") s = sample.read()   # ... Read More

Python - Window size Adjustment in Kivy

Nizamuddin Siddiqui
Updated on 08-Aug-2020 08:13:23

742 Views

Kivy is a platform independent GUI tool in Python. As it can be run on Android, IOS, linux and Windows etc. Kivy provides you the functionality to write the code for once and run it on different platforms. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktops applications.Kivy is that platform where the size does not matter a lot as it is self-adjusting accordingly but What if we want to fix the size to some extent whether its hight wise or width wise or free from boundation depends ... Read More

Advertisements