473,671 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

dividing list of numbers in to groups

2 New Member
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 1648
hidrkannan
32 New Member
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 Recognized Expert Moderator Specialist
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
6368
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 first number gives a whole number answer. Any Idea how I could do this? Obviously generating prime numbers is a bit of a problem as well! Cheers, Jack
10
4137
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 quite important, and it's similar to the built-in Mathematica Flatten function: l = {{"a", 2, {}, {3, 5, {4}}}}
41
3539
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 pairs, each pair indicates the sameness of the two indexes. Returns a partitioned list of same indexes.
4
2311
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 matching say abc to another appearance later on in the list but to look for transposed patterns. The groups of letters can be made up of between 2 to 4 letters.
6
7390
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
10746
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 consisting of 6 numbers - with a total of 50 groups of numbers. A well-known girl that some publishing companies use to provide introductory level textbooks to various Junior Colleges in the U.S., not surprisngly, asks for this same exact...
5
2437
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
1133
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 number they enter is not one of the numbers I have in an Access database table that contains a list of numbers they are not allowed to enter. How can I do this?
2
6228
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 Table/Query. Or it could be a value list, a static list of data. But what if your information is not held in a table, and it is not a static list. Examples I have used have been, “next 100 prime numbers after this one”, “Every third Friday from start of...
0
8392
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8912
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8819
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8669
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7428
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5692
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4222
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4403
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2809
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.