Python Forum
Help Grouping by Intervals on list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help Grouping by Intervals on list
#1
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.
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Grouping Data based on 30% bracket purnima1 4 1,191 Mar-10-2023, 07:38 PM
Last Post: deanhystad
  Grouping and sum of a list of objects Otbredbaron 1 3,196 Oct-23-2021, 01:42 PM
Last Post: Gribouillis
  time intervals project_science 7 2,933 Feb-06-2021, 11:49 PM
Last Post: project_science
  Plotting “simple” diagram with gridlines at specified intervals schniefen 1 2,409 Dec-03-2020, 05:54 PM
Last Post: schniefen
  Grouping and summing of dataset jef 0 1,635 Oct-04-2020, 11:03 PM
Last Post: jef
  Setting Tick Intervals In Sub Plots JoeDainton123 1 1,789 Oct-03-2020, 11:52 PM
Last Post: scidam
  Grouping algorithm riccardoob 7 2,994 May-19-2020, 01:22 PM
Last Post: deanhystad
  Grouping a list of various time into intervals paul41 1 3,775 Nov-24-2019, 01:47 PM
Last Post: buran
  resample grouping pr0blem olufemig 1 1,951 Nov-06-2019, 10:45 PM
Last Post: Larz60+
  Splitting lines ang grouping three at once samsonite 5 2,752 Jun-21-2019, 05:19 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020