Dec-03-2019, 07:02 PM
I am looking for a way to group a list of numbers into group of intervals and a count.
For example:
List = [1, 2, 3, 3.5, 3.6, 4, 5, 6, 7, 8, 9, 10, 14]
Interval = 2
To give the result of;
NewList = [1, 3, 5, 7, 9, 11, 13] #This will form the values in x scale of a graph
Counted_items = [2, 4, 2, 2, 2, 0, 1] #This will form the y axis as a count of each within that interval.
(2 = because there this is the count of the numbers between 1 and 3 within the list. 4 is returned because there are 4 numbers between 3 and the next number 5 in the list. Hoping that makes sense.
The result doesn't necessary be in the above format, i'm just looking to count numbers that exist based on a given interval in order to input later into a graph.
I am thinking I need to get the min number from the NewList and add the interval of 2 to this, keep looping over the values until the number is less than the next interval of 3. Output this to a new list. Seem to having difficulty on how to tackle this one. Going between a defined function or either using intertools or even pandas. Be grateful if someone can point me in the right direction.
For example:
List = [1, 2, 3, 3.5, 3.6, 4, 5, 6, 7, 8, 9, 10, 14]
Interval = 2
To give the result of;
NewList = [1, 3, 5, 7, 9, 11, 13] #This will form the values in x scale of a graph
Counted_items = [2, 4, 2, 2, 2, 0, 1] #This will form the y axis as a count of each within that interval.
(2 = because there this is the count of the numbers between 1 and 3 within the list. 4 is returned because there are 4 numbers between 3 and the next number 5 in the list. Hoping that makes sense.
The result doesn't necessary be in the above format, i'm just looking to count numbers that exist based on a given interval in order to input later into a graph.
I am thinking I need to get the min number from the NewList and add the interval of 2 to this, keep looping over the values until the number is less than the next interval of 3. Output this to a new list. Seem to having difficulty on how to tackle this one. Going between a defined function or either using intertools or even pandas. Be grateful if someone can point me in the right direction.