Cripto
Cripto

Reputation: 3751

Grouping list of integers in a range into chunks

Given a set or a list (assume its ordered)

myset = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]

I want to find out how many numbers appear in a range. say my range is 10. Then given the list above, I have two sets of 10.

I want the function to return [10,10]

if my range was 15. Then I should get [15,5]

The range will change. Here is what I came up with

myRange = 10
start = 1
current = start
next = current + myRange
count = 0
setTotal = []
for i in myset:
    if i >= current and i < next :
        count = count + 1
        print str(i)+" in "+str(len(setTotal)+1)
    else:
        current = current + myRange
        next = myRange + current
        if next >= myset[-1]:
            next = myset[-1]
        setTotal.append(count)
        count = 0

print setTotal

Output

1 in 1
2 in 1
3 in 1
4 in 1
5 in 1
6 in 1
7 in 1
8 in 1
9 in 1
10 in 1
12 in 2
13 in 2
14 in 2
15 in 2
16 in 2
17 in 2
18 in 2
19 in 2
[10, 8]

notice 11 and 20 where skipped. I also played around with the condition and got wired results.

EDIT: Range defines a range that every value in the range should be counted into one chuck.

think of a range as from current value to currentvalue+range as one chunk.

EDIT:

Wanted output:

1 in 1
2 in 1
3 in 1
4 in 1
5 in 1
6 in 1
7 in 1
8 in 1
9 in 1
10 in 1
11 in 2
12 in 2
13 in 2
14 in 2
15 in 2
16 in 2
17 in 2
18 in 2
19 in 2
[10, 10]

Upvotes: 0

Views: 390

Answers (3)

martineau
martineau

Reputation: 123491

With the right key function, thegroupbymethod in the itertoolsmodule makes doing this fairly simple:

from itertools import groupby

def ranger(values, range_size):
    def keyfunc(n):
        key = n/(range_size+1) + 1
        print '{} in {}'.format(n, key)
        return key

    return [len(list(g)) for k, g in groupby(values, key=keyfunc)]

myset = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
print ranger(myset, 10)
print ranger(myset, 15)

Upvotes: 2

Martijn Pieters
Martijn Pieters

Reputation: 1123410

You want to use simple division and the remainder; the divmod() function gives you both:

def chunks(lst, size):
    count, remainder = divmod(len(lst), size)
    return [size] * count + ([remainder] if remainder else [])

To create your desired output, then use the output of chunks():

lst = range(1, 21)
size = 10

start = 0
for count, chunk in enumerate(chunks(lst, size), 1):
    for i in lst[start:start + chunk]:
        print '{} in {}'.format(i, count)
    start += chunk

count is the number of the current chunk (starting at 1; python uses 0-based indexing normally).

This prints:

1 in 1
2 in 1
3 in 1
4 in 1
5 in 1
6 in 1
7 in 1
8 in 1
9 in 1
10 in 1
11 in 2
12 in 2
13 in 2
14 in 2
15 in 2
16 in 2
17 in 2
18 in 2
19 in 2
20 in 2

Upvotes: 1

Blender
Blender

Reputation: 298364

If you don't care about what numbers are in a given chunk, you can calculate the size easily:

def chunk_sizes(lst, size):
    complete = len(lst) // size  # Number of `size`-sized chunks
    partial = len(lst) % size    # Last chunk

    if partial:  # Sometimes the last chunk is empty
        return [size] * complete + [partial]
    else:
        return [size] * complete

Upvotes: -1

Related Questions