Is there a foreach loop in Go?

A “for” statement with a “range” clause iterates through all entries of an array, slice, string or map, or values received on a channel. For each entry it assigns iteration values to corresponding iteration variables and then executes the block. As an example: If you don’t care about the index, you can use _: The … Read more

What is the difference between String.slice and String.substring?

slice() works like substring() with a few different behaviors. What they have in common: If start equals stop: returns an empty string If stop is omitted: extracts characters to the end of the string If either argument is greater than the string’s length, the string’s length will be used instead. Distinctions of substring(): If start > stop, then substring will swap those 2 arguments. If either argument … Read more

Fastest way to duplicate an array in JavaScript – slice vs. ‘for’ loop

There are at least 6 (!) ways to clone an array: loop slice Array.from() concat spread operator (FASTEST) map A.map(function(e){return e;}); There has been a huuuge BENCHMARKS thread, providing following information: for blink browsers slice() is the fastest method, concat() is a bit slower, and while loop is 2.4x slower. for other browsers while loop is the fastest method, since those browsers don’t have internal optimizations for slice and concat. This remains … Read more

Understanding slice notation

It’s pretty simple really: There is also the step value, which can be used with any of the above: The key point to remember is that the :stop value represents the first value that is not in the selected slice. So, the difference between stop and start is the number of elements selected (if step is 1, the default). The other feature is that start or stop may be a negative number, which … Read more

How to take column-slices of dataframe in pandas

2017 Answer – pandas 0.20: .ix is deprecated. Use .loc See the deprecation in the docs .loc uses label based indexing to select both rows and columns. The labels being the values of the index or the columns. Slicing with .loc includes the last element. Let’s assume we have a DataFrame with the following columns:foo, bar, quz, ant, cat, sat, dat. .loc accepts the same slice … Read more