473,327 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

dividing list of numbers in to groups

Hello Everyone!

I need help with dividing a list of numbers in to groups of numbers that have constant difference between them. This constant number does not change it remains the same . e.g. 1000 in the following example:

Thank you in advance.

The list is:

List = [ 40000, 41000, 42000, 43000, 54000, 55000, 56000, 90000]

the routine i need should give me:

grp1 = [40000, 41000, 42000, 43000]
grp2 = [54000, 55000, 56000]
grp3 = [90000]
Feb 24 '08 #1
2 1635
Below code might work...but may not be a natural one...

Expand|Select|Wrap|Line Numbers
  1. import os
  2. import sys
  3.  
  4. aList = [10,12,14,16]
  5.  
  6. const = 7
  7.  
  8. aList.sort()
  9.  
  10. minVal = min(aList)
  11. maxVal = max(aList)
  12.  
  13. agroupDict = {}
  14. count = 1
  15. agroupDict[count] = [minVal]
  16. aList.remove(minVal)
  17. value = minVal + const
  18.  
  19.  
  20.  
  21. while value <= maxVal:
  22.     if value in aList:
  23.         agroupDict[count].append(value)
  24.         aList.remove(value)
  25.         value = value + const
  26.     else:
  27.         count = count + 1
  28.         agroupDict[count] = []
  29.         value = min(aList)
  30.  
  31. if value > maxVal:
  32.     for tempVal in aList:
  33.         count = count + 1
  34.         agroupDict[count] = [tempVal]        
  35. print agroupDict
  36.  
SKN
Feb 25 '08 #2
bvdet
2,851 Expert Mod 2GB
Expand|Select|Wrap|Line Numbers
  1. def group_nums(numlist, diff=1000):
  2.     resultDict = {}
  3.     idx = 0
  4.     for i, num in enumerate(numlist):
  5.         if i:
  6.             if num - numlist[i-1] != diff:
  7.                 idx += 1
  8.             resultDict.setdefault(idx, []).append(num)
  9.         else:
  10.             resultDict[idx] = [num, ]
  11.     return resultDict
  12.  
  13. numlist = [40000, 41000, 42000, 43000, 54000, 55000, 56000, 90000]
  14. dd = group_nums(numlist)
  15. print dd.values()
  16. dd = group_nums(numlist, 11000)
  17. print dd.values()
Output:
>>> [[40000, 41000, 42000, 43000], [54000, 55000, 56000], [90000]]
[[40000], [41000], [42000], [43000, 54000], [55000], [56000], [90000]]
>>>
Feb 25 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Jack Higgs | last post by:
I'm building a maths program for year 6 children. Problem with division - I generate a random number between say 1 and 1000. I want to generate another random number which when divided by the...
10
by: bearophile | last post by:
This is my first Python program (it's an improvement of a recursive version by Luther Blissett). Given a list like this: , ]]] It produces the "flatted" version: I think this operation is...
41
by: Xah Lee | last post by:
here's another interesting algorithmic exercise, again from part of a larger program in the previous series. Here's the original Perl documentation: =pod merge($pairings) takes a list of...
4
by: temp | last post by:
Hi All, I wonder could someone help me with this? What I want to do is search through a list of letters and look for adjacent groups of letters that form sequences, not in the usual way of...
6
by: Christian Roth | last post by:
Hello, how do I offset the numbering of a list in XHTML Strict (+CSS) in current browsers? What I want is something like: 5. Item a 6. Item b 7. Item c
4
by: August1 | last post by:
A handful of articles have been posted requesting information on how to use these functions in addition to the time() function as the seed to generate unique groups (sets) of numbers - each group...
5
by: M. Akkerman | last post by:
Hi, I've been working on this problem for a while now but I just can't seem to find a good solution for it. Here is the situation. I have the following class class TBigNumber {
3
by: mphillips | last post by:
I have an ASP form that users enter a number into. The form then posts the number with a hidden login and password to a web address which opens in a new window. However, I want make sure that the...
2
by: Lysander | last post by:
I have not seen this feature documented before, so I thought I would share it with you, as I will be using it in a later article. For a combo or list box, the source data is normally a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.