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
-
Economics & Finance
Server Side Programming Articles
Page 876 of 2109
Python - Combine two lists by maintaining duplicates in first list
In data analysis using Python, we often need to combine two lists while handling duplicate elements carefully. This article shows how to combine two lists by maintaining all elements from the first list (including duplicates) and adding only unique elements from the second list. Using extend() with Generator Expression This approach preserves the original first list and uses a generator expression to filter unique elements from the second list ? # Given lists first_list = ['A', 'B', 'B', 'X'] second_list = ['B', 'X', 'Z', 'P'] # Create result list starting with first list result = ...
Read MorePython - Convert 1D list to 2D list of variable length
A list in Python is normally a 1D list where the elements are listed one after another. But in a 2D list we have lists nested inside the outer list. In this article we will see how to create a 2D list from a given 1D list with variable-length sublists based on specified lengths. Using Slicing with Index Tracking In this approach we will create a for loop to iterate through each required length and use slicing to extract the appropriate elements from the 1D list. We keep track of the current index position and increment it by ...
Read MorePython - Convert a list of lists into tree-like dict
Given a nested list, we want to convert it to a dictionary whose elements represent a tree data structure. In this article, we will explore two approaches to transform a list of lists into a hierarchical dictionary structure. Using Simple Iteration and Slicing We reverse the items in each list using slicing and then build the tree structure by checking and adding keys at each level ? Example def create_tree(nested_list): new_tree = {} for list_item in nested_list: current_tree = ...
Read MorePython - Check if k occurs atleast n times in a list
When analyzing data in Python lists, you often need to check if a specific element appears at least N times. For example, determining if the number 5 appears at least 3 times in a list. This article demonstrates three efficient approaches to solve this problem. Using Manual Counting The most straightforward approach is to iterate through the list and count occurrences of the target element ? data = [1, 3, 5, 5, 4, 5] print("Given list:", data) # Element to be checked elem = 5 # Minimum required occurrences n = 3 count = ...
Read MorePygorithm module in Python
The Pygorithm module is an educational Python library containing implementations of various algorithms and data structures. Its primary purpose is to provide ready-to-use algorithm code for learning and practical applications in data processing. Installation Install Pygorithm using pip ? pip install pygorithm Finding Available Data Structures After installation, you can explore the various data structures included in the package ? from pygorithm import data_structures help(data_structures) The output shows all available data structures ? Help on package pygorithm.data_structures in pygorithm: NAME pygorithm.data_structures - Collection ...
Read MoreOracle Database Connection in Python
Python can connect to Oracle using a python package called cx_Oracle. Oracle is one of the famous and widely used databases and Python's data processing features are leveraged well using this connectivity. In this article we will see how we can connect to Oracle database and query the DB. Installing cx_Oracle We can use the below command to install the python package which can be used for establishing the connectivity ? pip install cx_Oracle Connecting to Oracle Using this module we can connect to an Oracle database which is accessible through the Oracle ...
Read MoreThe netrc file processing using Python
The netrc class in Python is used to read data from the .netrc file present in Unix systems in the user's home directory. These are hidden files containing user's login credential details. This is helpful for tools like ftp, curl etc. to successfully read the .netrc file and use it for authentication. What is a .netrc File? A .netrc file stores login credentials for remote hosts in a standardized format. Each entry contains a machine name, login username, and password. The file format looks like ? machine example.com login myusername password mypassword machine ftp.example.com login ...
Read MoreEncode and decode XDR data using Python xdrlib
The External Data Representation (XDR) is a standard data serialization format used for transporting data between different systems. Python's xdrlib module provides encoders and decoders for XDR format, making it useful for creating and transferring complex data structures across networks. XDR provides a service associated with the OSI Presentation Layer and ensures consistent data representation regardless of the underlying system architecture. Basic XDR Packing and Unpacking The following example demonstrates how to pack and unpack data using the xdrlib module ? import xdrlib # Create a packer object packer = xdrlib.Packer() print("Packer type:", type(packer)) ...
Read MoreEncode and decode uuencode files using Python
The uuencode module in Python provides functions to encode and decode files using the Unix uuencoding format. This encoding method converts binary data into ASCII text, making it safe for transmission over text-based protocols. Understanding Uuencoding Uuencoding (Unix-to-Unix encoding) transforms binary files into printable ASCII characters. The encoded output includes a header with file permissions and name, followed by encoded data lines. Encoding a File The uu.encode() function converts a binary file into uuencoded format ? Syntax uu.encode(in_file, out_file, name=None, mode=None) Example import uu import io # Create ...
Read MoreEncode and decode MIME quoted-printable data using Python
When working with emails and web data, we often encounter non-ASCII characters that need special encoding. MIME quoted-printable encoding helps transmit such data safely over protocols that only support ASCII characters. Python provides built-in modules to handle this encoding and decoding efficiently. Using the email Package The email package includes modules for handling MIME encoding. Here's how to create a quoted-printable encoded message ? import email.mime.nonmultipart import email.charset # Create MIME message with UTF-8 charset msg = email.mime.nonmultipart.MIMENonMultipart('text', 'plain', charset='utf-8') # Construct charset with quoted-printable encoding cs = email.charset.Charset('utf-8') cs.body_encoding = email.charset.QP # ...
Read More