7

input:

  • a sorted list, like this:[1,2,3,8,10,15,16,17,18,22,23,27,30,31]
  • a threshold, like this: max_diff = 2

expected output:

  • a list of sub lists; each sub list contains the values that the neighboring difference is smaller than max_diff, like this: [[1, 2, 3], [8, 10], [15, 16, 17, 18], [22, 23], [27], [30, 31]]

Here's how I did this, I am wondering if there is a better way to do this.

test_list = [1,2,3,8,10,15,16,17,18,22,23,27,30,31]
max_diff = 2

splited_list = []
temp_list = [test_list[0]]
for i in xrange(1,len(test_list)):
    if test_list[i] - temp_list[-1] > max_diff:
        splited_list.append(temp_list)
        temp_list = [test_list[i]]
    else:
        temp_list.append(test_list[i])        
    if i == len(test_list) -1:
        splited_list.append(temp_list)

print splited_list 
2
  • Code review codereview.stackexchange.com
    – Praveen
    Aug 25, 2015 at 8:25
  • If this code is fully working and you'd just like to improve upon it, you might be better off posting on CodeReview as Praveen suggested. But please make sure to read their How to ask page. Aug 25, 2015 at 8:28

4 Answers 4

2

You can use enumerate and zip function within a list comprehension to find the indices of the elements that value difference is larger than 2, then split your list based on index list :

>>> li =[1, 2, 3, 8, 10, 15, 16, 17, 18, 22, 23, 27, 30, 31]
>>> inds=[0]+[ind for ind,(i,j) in enumerate(zip(li,li[1:]),1) if j-i>2]+[len(li)+1]
>>> [li[i:j] for i,j in zip(inds,inds[1:])]
[[1, 2, 3], [8, 10], [15, 16, 17, 18], [22, 23], [27], [30, 31]]
0
>>> a = [1,2,3,8,10,15,16,17,18,22,23,27,30,31]
>>> b = a[1:]    #offset by 1 position
>>> b
[2, 3, 8, 10, 15, 16, 17, 18, 22, 23, 27, 30, 31]
>>> c = [(i[1] - i[0]) for i in zip(a[:-1], b)]
>>> c   #position diff
[1, 1, 5, 2, 5, 1, 1, 1, 4, 1, 4, 3, 1]
>>> d  = [i[0] for i in enumerate(c) if i[1] > 2]
>>> d    #split position
[2, 4, 8, 10, 11]
>>> e =  [-1]+d+[len(a)]
>>> e    #add start end to split position
[-1, 2, 4, 8, 10, 11, 14]
>>> [a[l[0]+1: l[1]+1] for l in zip(e, e[1:])] 
[[1, 2, 3], [8, 10], [15, 16, 17, 18], [22, 23], [27], [30, 31]]
#split result
0

Rearranging your lines leads to a more compact form:

test_list = [1,2,3,8,10,15,16,17,18,22,23,27,30,31]
max_diff = 2

splited_list = []
prev_element = float('-inf')
for element in test_list:
    if element - prev_element > max_diff:
        splited_list.append([])
    splited_list[-1].append(element)
    prev_element = element
print splited_list
0

Works on all iterables

def split_by_threshold(seq, max_diff=2):
    it = iter(seq)
    last = next(it)
    part = [last]

    for curr in it:
        if curr - last > max_diff:
            yield part
            part = []

        part.append(curr)
        last = curr

    yield part


l = [1,2,3,8,10,15,16,17,18,22,23,27,30,31]
print(list(split_by_threshold(l)))
3
  • why don't you use a for-loop?
    – Daniel
    Aug 25, 2015 at 9:37
  • You're right, it didn't occurred to me that I'm actually building a for loop.
    – pacholik
    Aug 25, 2015 at 9:44
  • Basically the solution I was going to post, and I pretty much finished it, but I was so tired I didn't completely finish it :P.
    – Cyphase
    Aug 25, 2015 at 18:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.