IntroductionMatplotlib allows to add more than one plot in the same graph. In this tutorial, I will show you how to present data in the same plot, on two different axes.How to do it..1. Install matplotlib by opening up the python command prompt and firing pip install matplotlib.import matplotlib.pyplot as plt2. Prepare the data to be displayed.import matplotlib.pyplot as plt # data prep (I made up data no accuracy in these stats) mobile = ['Iphone', 'Galaxy', 'Pixel'] # Data for the mobile units sold for 4 Quaters in Million units_sold = (('2016', 12, 8, 6), ('2017', 14, 10, ... Read More
Problem..Assume you need to check the start or end of a string for a specific text patterns. The common patterns might be filename extensions but can also be anything. I will show you few methods on how you can do this.Startswith() methodA simple way to check the beginning of a string is by using startswith() method.Exampletext = "Is USA colder than Australia?" print(f"output {text.startswith('Is')}")OutputTrueExamplefilename = "Hello_world.txt" print(f"output {filename.startswith('Hello')}")OutputTrueExamplesite_url = 'https://www.something.com' print(f"output {site_url.startswith('http:')}")OutputFalseExampleprint(f"output {site_url.startswith('https:')}")OutputTrueendswith() Method.A simple way to check the ending of a string is by using endswith() method.Outputtext = "Is USA colder than Australia?" print(f"output ... Read More
ProblemYou want to search for and replace a text pattern in a string.If we have a very simple literal patterns, using the str.replace() method is an optimal solution.Exampledef sample(): yield 'Is' yield 'USA' yield 'Colder' yield 'Than' yield 'Canada?' text = ' '.join(sample()) print(f"Output {text}")OutputIs USA Colder Than Canada?Let us first see how to search a text.# search for exact text print(f"Output {text == 'USA'}")OutputFalseWe can search for the text using the basic string methods, such as str.find(), str.endswith(), str.startswith().# text start with print(f"Output {text.startswith('Is')}")OutputTrue# text ends with print(f"Output {text.startswith('Is')}")OutputTrue# search text with find print(f"Output ... Read More
Introduction...The queue module provides a first-in, first-out (FIFO), Last-in, First out (LIFO) data structure suitable for multi-threaded programming. Queues can be used to pass data or any wide range of information e.g. session details, paths, variables, .. between creator and consumer threads safely. Locking is generally handled for the caller.Note : This discussion assumes you already understand the general nature of a queue. If you do not, you may want to read some of the references before continuing.1. Let us implement a basic FIFO Queue.import queue fifo = queue.Queue() # put numbers into queue for i in range(5): fifo.put(i) ... Read More
This article is aimed at developers who want to find the largest or smallest items with Python. I will show a few methods touse and will conclude the best method for you.Method – 1: Slice approach on a ListIf you are simply trying to find the single smallest or largest item i.e N = 1, it is faster to use min() and max().Let us begin by generating some random integers.import random # Create a random list of integers random_list = random.sample(range(1, 10), 9) random_listOutput[2, 4, 5, 1, 7, 9, 6, 8, 3] FINDING THE SMALLEST & LARGEST ITEM (N=1) # ... Read More
ProblemYou need to identify the most frequently occurring items in a sequence.SolutionWe can use counter to keep track of the items in a sequence.What is a Counter ?The “Counter” is a mapping that holds an integer count for each key. Updating an existing key adds to its count. This Objectis used for counting the instances of hashable objects or as a multiset.The “Counter” is one of your best friends when you are performing data analysis.This object has been present in Python for quite some time, and so for a lot of you, this will be a quick review.We will start ... Read More
For this, use Object.keys() along with reduce(). To display the result, we will also use concat().ExampleFollowing is the code −var details = { name: ["John", "David"], age1: "21", age2: "23" }, output = Object .keys(details) .reduce((obj, tempKey) => (obj[tempKey] = [].concat(details[tempKey]), obj), {}) console.log(output) To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo302.js.OutputThis will produce the following output on console −PS C:\Users\Amit\javascript-code> node demo302.js { name: [ 'John', 'David' ], age1: [ '21' ], age2: [ '23' ] }
Let’s say we have marks records and we need to sum them. The records are displayed in Radio Button −Marks1 75 { if (evnt.checked) { total = total + parseInt(evnt.value); return; } }); secondMark.forEach((evnt) => { if (evnt.checked) { total = total + parseInt(evnt.value); return; } }); ... Read More
Let’s say the following is our array −var values = [7,5,3,8,9,'/',9,5,8,2,'/',3,4,8];To merge specific elements, use map along with split().ExampleFollowing is the code −var values = [7,5,3,8,9,'/',9,5,8,2,'/',3,4,8]; var afterMerge = values.join('') .split(/(\d+)/). filter(Boolean). map(v => isNaN(v) ? v : +v); console.log(afterMerge);To run the above program, you need to use the following command −node fileName.js. Here, my file name is demo301.js.OutputThis will produce the following output on console −PS C:\Users\Amit\javascript-code> node demo301.js [ 75389, '/', 9582, '/', 348 ]
For this, you can use ternary operator based on some condition.ExampleFollowing is the code −const yearRangeValue = 18; const getCorrectCentury = dateValues => { var [date, month, year] = dateValues.split("-"); var originalYear = +year > yearRangeValue ? "20" + year : "18" + year; return new Date(date + "-" + month + "-" + originalYear).toLocaleDateString('en-GB') }; console.log(getCorrectCentury('10-JAN-19'));To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo300.js.OutputThis will produce the following output on console −PS C:\Users\Amit\javascript-code> node demo300.js 1/10/2019