0

I'm trying to find consecutive integers in a list, similar to the solution to this question: Detecting consecutive integers in a list

However, that question was answered in python 2, and running the same code sample in python 3 results in the following

from itertools import groupby
from operator import itemgetter
data = [ 1, 4,5,6, 10, 15,16,17,18, 22, 25,26,27,28]
for k, g in groupby(enumerate(data), lambda (i, x): i-x):
    print(map(itemgetter(1), g))
File "temp.py", line 4
  for k, g in groupby(enumerate(data), lambda (i, x): i-x):
                                                ^
SyntaxError: invalid syntax

I can't seem to see where the syntax would have changed between python versions, and I'm guessing I'm missing an easy fix.

lsterzinger
  • 687
  • 5
  • 22
  • You're close. Try the groupby on `zip(data, data[1:])` – inspectorG4dget Sep 03 '20 at 17:19
  • 3
    I think a lambda can only take one argument, and that argument may be a tuple. Try `lambda t: t[0] - t[1]` – Green Cloak Guy Sep 03 '20 at 17:21
  • @inspectorG4dget unfortunately that produces the same error: `File "temp.py", line 4 for k, g in groupby(zip(data, data[1:]), lambda (i, x): i-x): ^ SyntaxError: invalid syntax ` – lsterzinger Sep 03 '20 at 17:22
  • @GreenCloakGuy I think that's the issue, did `lambda` change between python 2 and 3? I can confirm that the above code works on python 2 – lsterzinger Sep 03 '20 at 17:25
  • @GreenCloakGuy your solution worked, and is very similar to the solution I mistakenly overlooked in the original post. Thanks! – lsterzinger Sep 03 '20 at 17:28
  • You can still have multiple arguments to a lambda function in python 3 (minus the parenthesis, something like `lambda i, x: i - x`). The issue is that `groupby` passes only one argument to the function. – Gustavo Kawamoto Sep 03 '20 at 17:28

3 Answers3

0

Turns out I didn't read the original post enough, a solution for python 3 was posted in a comment to the solution:

"Change the lambda to lambda ix : ix[0] - ix[1] and it works in both Python 3 and Python 2 (well, not counting the print statement). – Kevin May 20 '15 at 4:17"

lsterzinger
  • 687
  • 5
  • 22
0
In [16]: data = [ 1, 4,5,6, 10, 15,16,17,18, 22, 25,26,27,28]                                                                           

In [17]: data                                                                                                                           
Out[17]: [1, 4, 5, 6, 10, 15, 16, 17, 18, 22, 25, 26, 27, 28]

In [18]: def groups(L): 
    ...:     temp = [L[0]] 
    ...:     for num in L[1:]: 
    ...:         if num != temp[-1]+1: 
    ...:             yield temp 
    ...:             temp = [] 
    ...:         temp.append(num) 
    ...:     yield temp 
    ...:                                                                                                                                

In [19]: for run in groups(data): print(run)                                                                                            
[1]
[4, 5, 6]
[10]
[15, 16, 17, 18]
[22]
[25, 26, 27, 28]
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
-1

You can code like this;

from itertools import groupby
data = [1, 4, 5, 6, 10, 15, 16, 17, 18, 22, 25, 26, 27, 28]
for k, g in groupby(enumerate(data), lambda x : x[0] - x[1]):
    print(list(dict(g).values()))
Ahmed Mamdouh
  • 696
  • 5
  • 12