-5

This is my code (I use python 2.x):

import itertools

def grouper(input_list, n = 2):
    for i in xrange(len(input_list) - (n - 1)):
        yield input_list[i:i+n]

def list_of_lists(num):
    nums = list(map(int,str(num)))
    for first, second, third, fourth in grouper(nums, 4):
        x = first, second, third, fourth
        xx = list(x)
        print xx

This is my input:

a = 1232331
list_of_lists(a)

This is my output:

[1, 2, 3, 2]
[2, 3, 2, 3]
[3, 2, 3, 3]
[2, 3, 3, 1]

But I want the output to be:

[[1, 2, 3, 2], [2, 3, 2, 3], [3, 2, 3, 3], [2, 3, 3, 1]]
Heptapod
  • 121
  • 6

1 Answers1

0

You can use zip method to get the combinations:

try:
    from itertools import izip as zip
except ImportError:
    pass


x = [1, 2, 3, 4, 5, 6]


def getCombin(n, l):
    if len(l) < n:
        return None
    else:
        return list(zip(*(x[i:] for i in range(n))))


print(getCombin(3,x))

Output:

[(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6)]

If you're using Python2.x you can import izip:

from itertools import izip as zip
McGrady
  • 10,869
  • 13
  • 47
  • 69