0

In python 3, I have been trying to find a way to extract subsets from a list given a number that specifies how elements I want, and also in the correct order. For instance: for the list L = [7, 6, 0, 3, 1, 1, 2, 5], say I want subsets with 7 elements, there would only be two subsets which are [7, 6, 0, 3, 1, 1, 2] and [6, 0, 3, 1, 1, 2, 5]. Is there a way for me to perform this operation? Thank you in Advance!

miradulo
  • 28,857
  • 6
  • 80
  • 93
Leslie
  • 21
  • 2

1 Answers1

-2

Will this work?

def subsets(chunks,list1):
    d=(len(L)+1-chunks)
    return [L[i:i + chunks] for i in range(0,d,1)]

print(subsets(7,L))

output:

[[7, 6, 0, 3, 1, 1, 2], [6, 0, 3, 1, 1, 2, 5]]
Aaditya Ura
  • 12,007
  • 7
  • 50
  • 88