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 1648
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
Recognized Expert Moderator Specialist - 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 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
|
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}}}}
|
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.
|
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.
|
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 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...
|
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 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?
|
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...
|
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,...
|
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...
| |
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...
|
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...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
| |