3

I have this function to split iterables into sublists by length, the fill values, and the direction of fill :

def split(v,size,fill=0,direction='right'):
    if size == 0: return []
    lenv = len(v)
    count = lenv/size
    remainder = lenv%size
    result = []
    for i in range(0,lenv-remainder,size):
            result.append(v[i:i+size])
    if remainder > 0:
            if direction == 'right':
                    result.append(v[count*size:] + [fill] * (size-remainder))
            else:
                    result.append([fill] * (size-remainder) + v[count*size:])
    return result

Because I like one liners I want to rewrite it with map, but I don't understand how to. I have this so far :

def j1(a,b): 
        return a + b 

def j2(a,b): 
        return b 

def split2(v,size,fill=0): 
        map(j1,(x for x in map(j2,v))) 

I have no idea. Any hints?

Cris Stringfellow
  • 3,714
  • 26
  • 48

1 Answers1

6

I believe you are thinking too much. This problem can be aptly handled using the grouper recipe without using map

def split1(v,size,fill=0,direction='right'):
    result = list(izip_longest(*[iter(l)]*size, fillvalue=fill))
    if direction == 'left':
        result[-1] = result[-1][::-1]
    return result

Explanation:

  • iter: This function converts a sequence to an iterable. Iterables are self consumable and has only one method, next which returns the next element from the iterable, moving from left to right.
  • [iter(l)]*size: Creates a list of size iterables
  • * (Kleene star): This operator is used to unpack a list or tuple
  • izip_longest: Transposed the elements. For shorter sequences, it is filled by filled value
  • result[-1] = result[-1][::-1]: If the direction is left reverse the last sequence

Another possible popular solution without grouper is

def split2(v,size,fill=0,direction='right'):
    result = [v[i:i+size] for i in range(0,len(v),size)]
    result[-1] = result[-1] + [fill] * (size - len(result[-1]))
    if direction == 'left':
        result[-1] = result[-1][::-1]
    return result

Explanation:

  • Used Python's Extended Slice. Sequence Slicing has the following syntax [start: end: stride]
  • Python Range returns a list (in Py2.x) and a range object (in Py 3.x) as a sequence/iterable , starting from start, ending at end and stepping stride elements. Similar to (for int i = start; i < stop; i+= stride)
  • [fill] * (size - len(result[-1])): Generates (size - len(result[-1])) fill elements as a list. If (size - len(result[-1])) is <=0 it generates an empty list
  • result[-1] = result[-1] + [fill] * (size - len(result[-1])) - Update the last sequence with the fill value
  • result[-1] = result[-1][::-1]: If the direction is left reverse the last sequence
Community
  • 1
  • 1
Abhijit
  • 62,056
  • 18
  • 131
  • 204
  • 1
    I like that it is concise. Could you explain the * unpacking a little bit? – Cris Stringfellow Jan 29 '13 at 07:42
  • one thing you overlooked was the reversal actually reverses the original sequence of the list, as well as the side which the fills are applied on. So I am going to edit your answer to include the intended function! – Cris Stringfellow Jan 29 '13 at 09:10