Help Grouping by Intervals on list - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Help Grouping by Intervals on list (/thread-22927.html) |
Help Grouping by Intervals on list - paul41 - Dec-03-2019 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. RE: Help Grouping by Intervals on list - michael1789 - Dec-03-2019 I don't know if it can be of use to you, but if I'm right: interval = abs(list[1] - list[2]) if interval == required_interval: New_list.append(list[2])Loop this with the index numbers + 1 each time and I think you can work along the list like that. |