Are you familiar with JavaScript and looking for how to do something equivalent within Python?
(or vice versa?)
Python and JavaScript have many similar functionalities, but they are often called different things and have different consequences.
We're hoping to collect common types of requests and putting them here.
(Please note this is still in progress, pull requests welcome)
A range is a 1D array that progresses from a start number to an end number
print range(5)
# 0, 1, 2, 3, 4
JavaScript doesn't have an equivalent and one is required to be made.
Note that number libraries like stdlib also can provide similar functionality
const 1dRange = (count) => Array.from(new Array(count), (x,i) => i));
idRange(5)
// [0,1,2,3,4]
def foo():
yield 1
yield 2
yield 3
function *foo() {
yield 1;
yield 2;
yield 3;
}
lambda a: a * 2
a => a * 2
Note that Python allows for automatic destructuring using tuples.
status, data = getResult()
JavaScript allows destructuring for both object maps and arrays.
// getResult(): [number, number]
var [status, data] = getResult();
console.log(`status:${status}, data:${data}`)
or
// getObject(): {first, last}
var {first, last} = getObject();
console.log(`first:${first}, last:${last}`);
search_db(**parameters)
searchDb(...parameters);
def fibonacci():
pre, cur = 0, 1
while True:
pre, cur = cur, pre + cur
yield cur
for x in fibonacci():
if (x > 1000):
break
print x,
See MDN regarding iterators and generators
var fibonacci = {
[Symbol.iterator]: function*() {
var pre = 0, cur = 1;
for (;;) {
var temp = pre;
pre = cur;
cur += temp;
yield cur;
}
}
}
for (var n of fibonacci) {
if (n > 1000)
break;
console.log(n);
}
(Python has builtin support for multiple inheritance)
class SpiderMan(Human, SuperHero):
def __init__(self, age):
super(SpiderMan, self).__init__(age)
self.age = age
def attack(self):
print 'launch web'
class SpiderMan extends SuperHero {
constructor(age) {
super();
this.age = age;
}
attack() {
console.log('launch web')
}
}
names = [c.name for c in customers if c.admin]
They were proposed within JavaScript within EcmaScript v7, but were removed:
Quoting MDN:
The array comprehensions syntax is non-standard and removed starting with Firefox 58. For future-facing usages, consider using Array.prototype.map, Array.prototype.filter, arrow functions, and spread syntax.
(Experimental in Babel)
var names = [for (c of customers) if (c.admin) c.name];
Now using maps:
var names = customers.map(c => c.admin ? c.name : null);
double = lambda: x => x * 2
map(double, [1,2,3,4])
# [2,4,6,8]
const double = (x) => x * 2;
[1,2,3,4].map(double)
// [2,4,6,8]
len([1,2,3,4])
# 4
[1,2,3,4].length
# 4
(In order of more common recommendations)
- Data Analysis Library
- Python
- Javascript - stack overflow
- DataFrame
- Python
- JavaScript - stack overflow
What is it?
What is it useful for?
When is it not a good fit?
Considerations
If you're interested in this, try:
What is it?
Built by the team at Tensorflow, one of the main goals of Danfo.js is to bring data processing, machine learning and AI tools to JavaScript developers.
Just like Pandas
is built on top of Numpy
, danfo-js
is built on tensorflow-js
Danfo.js is heavily inspired by the Pandas library and provides a similar interface and API. This means users familiar with the Pandas API can easily use Danfo.js.
- Danfo.js is fast. It is built on Tensorflow.js, and supports tensors out of the box. This means you can convert danfo data structure to Tensors.
- Easy handling of missing data (represented as NaN) in floating point as well as non-floating point data
- Size mutability: columns can be inserted/deleted from DataFrame
- Automatic and explicit alignment: objects can be explicitly aligned to a set of labels, or the user can simply ignore the labels and let Series, DataFrame, etc. automatically align the data for you in computations
- Powerful, flexible groupby functionality to perform split-apply-combine operations on data sets, for both aggregating and transforming data
- Make it easy to convert Arrays, JSONs, List or Objects, Tensors and differently-indexed data structures into DataFrame objects
- Intelligent label-based slicing, fancy indexing, and querying of large data sets
- Intuitive merging and joining data sets
- Robust IO tools for loading data from flat-files (CSV and delimited) and JSON data format.
- Powerful, flexible and intutive API for plotting DataFrames and Series interactively.
- Timeseries-specific functionality: date range generation and date and time properties.
- Robust data preprocessing functions like OneHotEncoders, LabelEncoders, and scalers like StandardScaler and MinMaxScaler are supported on DataFrame and Series
What is it useful for?
When is it not a good fit?
Considerations
If you're interested in this, try:
Not Recommended - appears abandoned
What is it?
What is it useful for?
When is it not a good fit?
Considerations
What is it?
What is it useful for?
When is it not a good fit?
Considerations
If you're interested in this, try:
What is it?
What is it useful for?
When is it not a good fit?
Considerations
If you're interested in this, try:
What is it?
What is it useful for?
When is it not a good fit?
Considerations
If you're interested in this, try:
What is it?
What is it useful for?
When is it not a good fit?
Considerations
If you're interested in this, try:
What is it?
What is it useful for?
When is it not a good fit?
Considerations
If you're interested in this, try:
What is it?
What is it useful for?
When is it not a good fit?
Considerations
If you're interested in this, try:
What is it?
What is it useful for?
When is it not a good fit?
Considerations
If you're interested in this, try:
What is it?
What is it useful for?
When is it not a good fit?
Considerations
If you're interested in this, try:
What is it?
What is it useful for?
When is it not a good fit?
Considerations
If you're interested in this, try:
What is it?
What is it useful for?
When is it not a good fit?
Considerations
If you're interested in this, try:
What is it?
What is it useful for?
When is it not a good fit?
Considerations
If you're interested in this, try:
What is it?
What is it useful for?
When is it not a good fit?
Considerations
If you're interested in this, try:
What is it?
What is it useful for?
When is it not a good fit?
Considerations
If you're interested in this, try:
What is it?
What is it useful for?
When is it not a good fit?
Considerations
If you're interested in this, try:
What is it?
What is it useful for?
When is it not a good fit?
Considerations
If you're interested in this, try:
What is it?
What is it useful for?
When is it not a good fit?
Considerations
If you're interested in this, try:
What is it?
What is it useful for?
When is it not a good fit?
Considerations
If you're interested in this, try:
What is it?
What is it useful for?
When is it not a good fit?
Considerations
If you're interested in this, try:
What is it?
What is it useful for?
When is it not a good fit?
Considerations
If you're interested in this, try:
What is it?
What is it useful for?
When is it not a good fit?
Considerations
If you're interested in this, try: