-5

This is the Code I am looking at https://github.com/Katee/quietnet

In this project, there is a file named quitenet.py

In this file, there it is.

def chunks(l, n):
    for i in xrange(0, len(l), n):
        yield l[i:i+n]

I have no idea how I can understand the meaning l[i:i+n] .

levi
  • 22,001
  • 7
  • 73
  • 74

7 Answers7

0

: is a slicing operator for lists.

From this answer

a[start:end] # items start through end-1
a[start:]    # items start through the rest of the array
a[:end]      # items from the beginning through end-1
a[:]         # a copy of the whole array

So, there is an example

l = [1,2,3,4,5] 
print l[1:3]
>> [2, 3]
Community
  • 1
  • 1
levi
  • 22,001
  • 7
  • 73
  • 74
0

It's slicing. So, in that part, it slices the list l from i to i+n-1 (includes element in index i, until index i+n-1, but exclude element in index i+n)

ismailsunni
  • 1,458
  • 1
  • 24
  • 32
0

l[i:i+n] is slicing syntax, and it gets a subsequence from index i (inclusive) to index i+n (exclusive). Unlike indexing, it never raises exceptions; if the slice goes outside the sequence, the slice is just shorter than you'd expect.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
0

The expression l[i:i+n] returns a slice of array l, specifically the elements from i to i+n-1. This is a generator expression and each call will return the next n elements from the array since the last time it was called.

casey
  • 6,855
  • 1
  • 24
  • 37
0

This is a form of string formatting called slicing. It involves sectioning a part of the string using slicing notation, :. Here is how it works:

[starting_point:ending_point]
>>>l = [1,2,3,4,5]
>>>a = l[1:3]
>>>print a
[2,3,4]

Once you know this the code becomes easy to read. You are slicing the string from the section i to whatever n is supposed to be. n is also apparently the interval that the range jumps with each iteration.

You can read more on slicing here: https://pythonhosted.org/bitstring/slicing.html

Joseph Farah
  • 2,463
  • 2
  • 25
  • 36
0

The function is a generator that returns incremental slices of l that are n elements long.

Consider the following:

def chunks(l, n):
    for i in xrange(0, len(l), n):
        yield l[i:i+n]

a_list = [1,2,3,4,5,6,7,9,10,11,12,13,14]

for slice in chunks(a_list, 4):
    print(slice)

Produces:

[1, 2, 3, 4]
[5, 6, 7, 9]
[10, 11, 12, 13]
[14]

Which are non-overlapping slices. The trick to this is that the slice notation extracts elements from the starting index to the (ending index -1).

John Percival Hackworth
  • 11,395
  • 2
  • 29
  • 38
0

Easiest way is to try it on some data. For example, let's make a list:

>>> l = list("abcdefghijklmnopqrstuvwxyz")
>>> l
['a', 'b', 'c', 'd', 'e', 'f', ... 'x', 'y', 'z']

I shortened the list, but it's all the letters you expect. Now try it:

>>> list(chunks(l, 3))
[['a', 'b', 'c'],
 ['d', 'e', 'f'],
 ['g', 'h', 'i'],
 ['j', 'k', 'l'],
 ['m', 'n', 'o'],
 ['p', 'q', 'r'],
 ['s', 't', 'u'],
 ['v', 'w', 'x'],
 ['y', 'z']]

So what's happening here is that as i increments through the loop (skipping n elements at a time), the routine yields out the next n elements of the original list. This return strategy is called a "generator," and it's signaled by the yield statement. Yield is like return, but if the routine is called again, it will "keep going from where it last left off."

Another way to exercise this is in a loop:

>>> for c in chunks(l, 4):
>>>     print c
['a', 'b', 'c', 'd']
['e', 'f', 'g', 'h']
['i', 'j', 'k', 'l']
['m', 'n', 'o', 'p']
['q', 'r', 's', 't']
['u', 'v', 'w', 'x']
['y', 'z']

Every time through the loop, c becomes the next "chunk" of the original iterable. What's yielded from chunks to the loop is a slice of the original list l[i:i+n]. In this case, I asked for n=4 so you can see the difference.

As you learn how to use enumerate, chunks, and similar generators, you'll find them enormously helpful. See e.g. the itertools module.

Jonathan Eunice
  • 21,653
  • 6
  • 75
  • 77