Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I would like to receive a dataset of N elements and group it every 59 elements, considering that the first element would have the index 0. The problem in this script is that in the sample.append(data [i]) part, it groups all the data again in a single list, I would like to divide it as follows. [[60 elements], [60 elementps], ...]. Any suggestions for correction? 

Script

@staticmethod        

def sampleDivision(data):

    parts = int(len(data) / 60)

    collections = []

    i = 1

    while i <= parts:

        i * 60

        collections.append(i * 59)

        i += 1

    print(collections)

    i = 1

    sample = []

    for position in collections:

        while i <= int(position):

            sample.append(data[i])

            i += 1

    return sample output:

[68, 69, 68, 69, 70, 71, 75, 72, 73, 72, 72, 73, 72, 72, 73, 70, 71, 73, 72, 72, 71, 69, 68, 69, 68, 69, 68, 69, 68, 68, 68, 68, 69, 68, 69, 70, 71, 75, 72, 69, 68, 68, 68, 69, 68, 69, 70, 71, 75, 72, 69, 68, 69, 68, 69, 68, 69, 68, 68, 60, 60, 61, 60, 61, 65, 69, 69, 72, 73, 72, 72, 73, 72, 72, 73, 70, 71, 73, 75, 78, 80, 82, 84, 87, 84, 84, 83, 82, 79, 78, 76, 74, 73, 72, 72, 72, 71, 75, 72, 69, 68, 68, 68, 69, 68, 69, 70, 71, 75, 72, 69, 68, 69, 68, 69, 68, 69, 68]

1 Answer

0 votes
by (36.8k points)

The range function has an increment argument that is useful in this case.

Here is a sample function that does what I think you are looking for:

def group_every_n(elements, n):

  L = len(elements) 

  return [elements[i: i+n] for i in range(0, L, n)]

# Get every 60 elements

grouped_by_60 = group_every_n(elements, 60)

In this example, L is the length of our list, and range(0, L, n) creates a generator object (basically, a list that doesn't get created until we ask for it) that spits out every nth element at every iteration of the loop. Depending on how long your original list is, the last group will likely have less than 60 elements.

Improve your knowledge in data science from scratch using Data science online courses

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...