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
Python Articles
Page 60 of 855
How to Check if a given Matrix is a Markov Matrix using Python?
In this article we are going to learn how to check whether the input matrix is a Markov Matrix using nested for loops and sum() function. Before that let us understand what is Markov Matrix with an example. The matrix is said to be a Markov matrix if the sum of each row is equal to 1. What is a Markov Matrix? A Markov matrix (also called a stochastic matrix) is a square matrix where each row sums to 1. This property ensures that the matrix can represent probability transitions between states. Example matrix ...
Read MoreHow to check Idempotent Matrix in Python?
An idempotent matrix is a square matrix that produces the same result when multiplied by itself. Mathematically, a matrix M is idempotent if and only if M × M = M. Understanding Idempotent Matrix Consider this example matrix: M = [1 0 0] [0 1 0] [0 0 0] When we multiply M by itself: [1 0 0] [1 0 0] [1 0 0] [0 1 0] × [0 1 0] = [0 1 0] [0 0 0] ...
Read MoreCheck Horizontal and Vertical Symmetry in the Binary Matrix using Python
A binary matrix is a rectangular grid in which each element is either 0 or 1, indicating true or false states. It is widely employed to represent relationships, connectivity, and patterns across various disciplines. Assume we have taken a 2D binary input matrix containing N rows and M columns. We will now check whether the input matrix is horizontal or vertically symmetric or both using the below method. If the first row matches the last row, the second row matches the second last row, and so on, the matrix is said to be horizontally symmetric. If the ...
Read MorePython - K difference index Pairing in List
K difference index pairing creates pairs of elements from a list where each element is paired with another element that is exactly K positions ahead. For example, with K=2, element at index 0 pairs with element at index 2, index 1 with index 3, and so on. Given the list ["A", "B", "C", "D", "E", "F"] and K=2, the result would be ["AC", "BD", "CE", "DF"]. Using map() with operator.concat The map() function applies operator.concat to corresponding elements from two sliced lists ? import operator # Initialize list my_list = ["T", "U", "T", "O", ...
Read MoreCheck if the Sum of Digits of a Number N Divides It
A number is divisible by the sum of its digits when the remainder of dividing the number by its digit sum equals zero. For example, if N = 36, the digit sum is 3 + 6 = 9, and since 36 % 9 = 0, the number 36 is divisible by its digit sum. Algorithm The algorithm follows these steps ? Extract each digit of the number using modulo operator (% 10) Add all digits to get the sum Check if the original number is divisible by this sum using modulo operator Return true if remainder ...
Read MoreHow to Sort a Pandas DataFrame by Date?
Sorting a Pandas DataFrame by date is a common operation in data analysis. Pandas provides several methods to accomplish this, with sort_values() being the most efficient. Before sorting, ensure your date column is in proper datetime format using to_datetime(). Basic Date Sorting with sort_values() The most straightforward method is using sort_values() after converting string dates to datetime format ? import pandas as pd # Create sample DataFrame with date strings data = { 'Date': ['2023-06-26', '2023-06-24', '2023-06-28', '2023-06-25'], 'Sales': [100, 200, 300, 150] } df ...
Read MorePython - Filter Tuples with All Even Elements
Filtering tuples with all even elements is a common operation in Python data processing. This involves checking if every element in each tuple is divisible by 2 and keeping only those tuples that satisfy this condition. Using List Comprehension with all() The most Pythonic approach combines list comprehension with the all() function to filter tuples where every element is even ? tuple_list = [(6, 4, 2, 8), (5, 6, 7, 6), (8, 0, 2), (7, ), (2, 4, 6)] result = [tup for tup in tuple_list if all(ele % 2 == 0 for ele in ...
Read MoreCheck if a Word is Present in a Sentence
In Python, checking if a word is present in a sentence is a common task in text processing and string manipulation. Python provides multiple approaches including the in operator, find() method, and split() method for word-by-word matching. Using the in Operator The simplest way to check if a word exists in a sentence is using Python's in operator ? sentence = "The bestseller book is amazing" word = "book" if word in sentence: print("The given word is present in the sentence.") else: print("The given word is not ...
Read MoreCheck if a number with even number of digits is palindrome or not
A palindrome is a number that reads the same forwards and backwards. This article shows how to check if a number with an even number of digits is a palindrome in Python. Examples include 2662, 4224, 44, 1001, etc. Using String Comparison The simplest approach converts the number to a string and compares it with its reverse ? def is_even_digit_palindrome(num): # Convert number to string num_str = str(num) # Check if even number of digits ...
Read MorePython - K List Dictionary Mesh
The term dictionary mesh refers to creating a nested dictionary structure where each element from the first list becomes a key, and each element from the second list becomes a nested key with empty lists as values. This creates a mesh-like structure that can hold organized data. Understanding Dictionary Mesh Given two input lists ? list1 = [10, 20, 30] list2 = [40, 50] The dictionary mesh creates this structure ? {10: {40: [], 50: []}, 20: {40: [], 50: []}, 30: {40: [], 50: []}} Method 1: Using Nested ...
Read More