Skip to main content

Posts

Showing posts from May, 2015

Python: Get the Indexes of Elements from a List (Counting Same Occurrences)

This is the "translation" of earlier JavaScript post about Counting Same Occurrences . Here, I have Python 2.7.7. The concept The main idea of this function is the same as the JavaScript version (link above - so that I don't have to re-explain it [I'm quite lazy that way] ). In this snippet, I typed a function that generates a dictionary (in JavaScript: object ) output from a list (in JavaScript: array ) input. The output consists of list element : {"index": [array of indexes], "length": array of indexes length} pairs. The "confusing" part was when I tried to initialize the list type for the non-existing dictionary key. The "solution" is using try-except block, with KeyError exception. Let's take a look def occurrence(a): result = {} if isinstance(a, list): # Check input for i, v in enumerate(a): if not isinstance(v, int) or not isinstance(v, str): v = str...

JavaScript: Counting Same Occurrences in an Array

For example We have an array: [2, "tea", 2, 1, 2, "tea", "bag", "bag", "bag", 1] And we wanna count the occurrence of every element (how many times it appears). Method There's a very long way to do that using bunch of loops. But then, I discovered a way using Object construction method. It's using just one forEach loop plus if-else conditional statement. Steps: Create an empty object. Loop through the array. While looping, for each of array element, create an object property of it (for that empty object). The value of that object property will then be filled with array of index where the element (object property) is found. Sounds hard, but not quite. Let's do it var occurrence = function (array) { "use strict"; var result = {}; if (array instanceof Array) { // Check if input is array. array.forEach(function (v, i) { if (!result[v]) { // Initial object pr...

JavaScript: Transfrom Nodelist into Array

We can get DOM nodelist from querySelectorAll or getElementsByTagName or getElementsByClassName (new) method. The nodelist can't be "fully" processed like an array can. We can loop through (read, and maybe set/write) the elements (using any of loop block methods), but there are methods which won't work for this object (it will return error code). Here's the basic idea We need to create an empty array, then put the elements from the nodelist (array-like) to the (actual) empty array. The newly created array will then be available for any of JavaScript array methods. JavaScript snippet Updated May 14, 2015 var NodeListToArray = function (a) { "use strict"; var i = 0, array = []; if (a instanceof Object) { do { array.push(a[i]); i += 1; } while (array.length Snippet usage var paragraph_nodelist = document.getElementsByTagName("p"), paragraph_array = NodeListToArray...

JavaScript: Check Whether an Object Is an Array

#1 Using instanceof operator # Example: var a = [0, 1, 1, 2], // array b = "string", // string c = 9, // number d = a instanceof Array, e = b instanceof Array, f = c instanceof Array; console.log(d); // Output: true console.log(e); // Output: false console.log(f); // Output: false Additional Checking object # This operator can also check if the input is an object, by using Object built-in constructor. As such: var the_object = {"something": 0, "something_else": 1000}, check = the_object instanceof Object; console.log(check); // Output: true Or, we can use typeof method. For example: var the_object = {"something": 0, "something_else": 1000}, check = typeof the_object === "object"; // standard form // previously: check = typeof(the_object) === "object"; console.log(check); // Output: true typeof will return "object" (string) keyword for either object or ar...