Answer the question
In order to leave comments, you need to log in
How to find all nearest bunches of numbers from an array?
I have an array of data of this type (let's say it's minutes, without seconds)
1
2 2 2 5 8 12 12 13 15 15 21 25
in this case 1 and 2, 12 and 13, 15 (the ideal result is 1, 12, 15)
Not necessarily python code
Answer the question
In order to leave comments, you need to log in
Thank you all, even tupanul, really just in general. In the end, I did it like this in python, MB will come in handy for someone:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
d=[1, 12, 13, 15, 15, 21, 2, 2, 2, 5, 8, 12, 25, 85.181, 86.081, 87.181, 88.182]
d.sort()
sv = 0
svtime=1 #диапазон связи
prev = None
for value in d:
if prev != None:
if((value-prev)<=svtime):
if sv == 0:
print(prev)
sv = 1
else:
sv = 0
prev = value
OUT >>> 1, 12, 15
1. sort the list in ascending order if it is not sorted
2. loop through the list and look at the difference "the current element minus the previous one".
3. if the difference is 1, then we write the "previous element" into a new list with links.
it is not clear why for the series 1 2 2 2 5 8 12 12 13 15 15 21 25 the correct option is 1, 12, 15,
since judging by the conditions, in order to be 15, there should be 15, 16 in the initial data.
nums = '1 2 2 2 5 8 12 12 13 15 15 21 25 26 32'
nums = sorted(map(int, nums.split()))
result = [x1 for x1, x2 in zip(nums[:-1], nums[1:]) if x2 - x1 == 1]
print(*result)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question