0

I have a list like this

['1133', '1300', '1418', '1443', '1473', '1600', '1601', '1988', '1990', '1991', '1992', '1993', '1994', '1995', '1996', '1997', '1998', '2000', '2003', '2004', '2005', '2006', '2007', '2008', '2009', '2010', '2012', '2013', '2014', '2015', '2153', '2600', '3000', '3714', '3785', '3896', '3995', '4001', '4436', '5094', '5346', '8000']

How can I find subgroups that contain consecutive number-looking strings (for example: '2012', '2013', '2014', '2015')?

Thank you!

Nemo
  • 1,124
  • 2
  • 16
  • 39
  • 2
    can you post something you've tried – smac89 Sep 15 '19 at 06:12
  • Hi, @smac89, I stared at the list for a whilst without knowing where to start so I couldn't think of any possible solutions for trial and error testing! – Nemo Sep 15 '19 at 06:20

3 Answers3

3

Try something like this:

l = ['1133', '1300', '1418', '1443', '1473', '1600', '1601', '1988', '1990', '1991', '1992', '1993', '1994', '1995', '1996', '1997', '1998', '2000', '2003', '2004', '2005', '2006', '2007', '2008', '2009', '2010', '2012', '2013', '2014', '2015', '2153', '2600', '3000', '3714', '3785', '3896', '3995', '4001', '4436', '5094', '5346', '8000']

size = len(l)
i = 0
while i < size:
   j = i
   while j + 1 < size and int(l[j]) + 1 == int(l[j + 1]):
      j += 1
   if j != i:
       print (l[i:j+1])
   i = j + 1

Output:

['1600', '1601']
['1990', '1991', '1992', '1993', '1994', '1995', '1996', '1997', '1998']
['2003', '2004', '2005', '2006', '2007', '2008', '2009', '2010']
['2012', '2013', '2014', '2015']
smac89
  • 39,374
  • 15
  • 132
  • 179
2

This is one way to do it, if you're fine with all the elements you specified being on the same result list

a = ['1133', '1300', '1418', '1443', '1473', '1600', '1601', '1988', '1990', '1991', '1992', '1993', '1994', '1995',
     '1996', '1997', '1998', '2000', '2003', '2004', '2005', '2006', '2007', '2008', '2009', '2010', '2012', '2013',
     '2014', '2015', '2153', '2600', '3000', '3714', '3785', '3896', '3995', '4001', '4436', '5094', '5346', '8000']
result = []
for i, x in enumerate(a):
    previous_e = -9999 if i == 0 else int(a[i - 1])
    next_e = -9999 if i == len(a) - 1 else int(a[i + 1])
    if int(x) == previous_e + 1 or int(x) == next_e - 1:
        result.append(x)

print(result)

['1600', '1601', '1991', '1992', '1993', '1994', '1995', '1996', '1997', '1998', '2004', '2005', '2006', '2007', '2008', '2009', '2010', '2013', '2014', '2015']

Ofer Sadan
  • 11,391
  • 5
  • 38
  • 62
  • Thanks for your suggestion, Ofer. Could you please explain where `-9999` came from and what was used for? – Nemo Sep 15 '19 at 06:40
  • 1
    @Nemo -9999 is just some random value I chose that is nowhere near the values in the list, only when there wasn't a "previous" or "next" value to put there. I could have chosen any number that isn't near the values of the list, or even `None` – Ofer Sadan Sep 15 '19 at 06:43
  • @Nemo added a small fix as well, missed one character – Ofer Sadan Sep 15 '19 at 06:46
  • 1
    Not sure because I examined your code _after_ @smac89 (when my brain has become accustomed to the logic of finding consecutive numbers) and due to your additional explanation of `-9999`, it took me much lesser time to comprehend and appreciate your code. Thanks again! – Nemo Sep 15 '19 at 11:52
1

If you convert your list to a list of int.

mylist = list(map(int, mylist))

Then you can apply the answer written here. But if you are using python 3, do not forget to convert it to python 3 syntax.

mulaixi
  • 183
  • 2
  • 11
  • Thanks, mulaixi. I think your solution is easier for someone with little programming background like me follow. However, it's not as self contained as smac89's. – Nemo Sep 15 '19 at 06:52