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]
2 1635
Below code might work...but may not be a natural one... -
import os
-
import sys
-
-
aList = [10,12,14,16]
-
-
const = 7
-
-
aList.sort()
-
-
minVal = min(aList)
-
maxVal = max(aList)
-
-
agroupDict = {}
-
count = 1
-
agroupDict[count] = [minVal]
-
aList.remove(minVal)
-
value = minVal + const
-
-
-
-
while value <= maxVal:
-
if value in aList:
-
agroupDict[count].append(value)
-
aList.remove(value)
-
value = value + const
-
else:
-
count = count + 1
-
agroupDict[count] = []
-
value = min(aList)
-
-
if value > maxVal:
-
for tempVal in aList:
-
count = count + 1
-
agroupDict[count] = [tempVal]
-
print agroupDict
-
SKN
bvdet 2,851
Expert Mod 2GB - def group_nums(numlist, diff=1000):
-
resultDict = {}
-
idx = 0
-
for i, num in enumerate(numlist):
-
if i:
-
if num - numlist[i-1] != diff:
-
idx += 1
-
resultDict.setdefault(idx, []).append(num)
-
else:
-
resultDict[idx] = [num, ]
-
return resultDict
-
-
numlist = [40000, 41000, 42000, 43000, 54000, 55000, 56000, 90000]
-
dd = group_nums(numlist)
-
print dd.values()
-
dd = group_nums(numlist, 11000)
-
print dd.values()
Output:
>>> [[40000, 41000, 42000, 43000], [54000, 55000, 56000], [90000]]
[[40000], [41000], [42000], [43000, 54000], [55000], [56000], [90000]]
>>>
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
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 {
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
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...
| |