Python: Checking to which bin a value belongs

Probably too late, but for future reference, numpy has a function that does just that: http://docs.scipy.org/doc/numpy/reference/generated/numpy.digitize.html >>> my_list = [3,2,56,4,32,4,7,88,4,3,4] >>> bins = [0,20,40,60,80,100] >>> np.digitize(my_list,bins) array([1, 1, 3, 1, 2, 1, 1, 5, 1, 1, 1]) The result is an array of indexes corresponding to the bin from bins that each element from my_list … Read more

group by range in mysql

Here is general code to group by range since doing a case statement gets pretty cumbersome. The function ‘floor’ can be used to find the bottom of the range (not ’round’ as Bohemian used), and add the amount (19 in the example below) to find the top of the range. Remember to not overlap the … Read more

Does range() really create lists?

In Python 2.x, range returns a list, but in Python 3.x range returns an immutable sequence, of type range. Python 2.x: >>> type(range(10)) <type ‘list’> >>> type(xrange(10)) <type ‘xrange’> Python 3.x: >>> type(range(10)) <class ‘range’> In Python 2.x, if you want to get an iterable object, like in Python 3.x, you can use xrange function, … Read more

php switch case statement to handle ranges

Well, you can have ranges in switch statement like: //just an example, though $t = “2000”; switch (true) { case ($t < “1000”): alert(“t is less than 1000”); break case ($t < “1801”): alert(“t is less than 1801”); break default: alert(“t is greater than 1800”) } //OR switch(true) { case in_array($t, range(0,20)): //the range from … Read more