5

I see a few great posts here on how to split Python lists into chunks like how to split an iterable in constant-size chunks. Most posts deal with dividing up the chunks or join all strings in the list together and then limit based on normal slice routines.

However, I was in need of performing something similar based on a character-limit. If you have a list of sentences but cannot truncate any slices in the list.

I was able to churn out some code here:

def _splicegen(maxchars, stringlist):
    """
    Return a list of slices to print based on maxchars string-length boundary.
    """
    count = 0  # start at 0
    slices = []  # master list to append slices to.
    tmpslices = []  # tmp list where we append slice numbers.

    for i, each in enumerate(stringlist):
        itemlength = len(each)
        runningcount = count + itemlength
        if runningcount < int(maxchars):
            count = runningcount
            tmpslices.append(i)
        elif runningcount > int(maxchars):
            slices.append(tmpslices)
            tmpslices = []
            count = 0 + itemlength
            tmpslices.append(i)
        if i==len(stringlist)-1:
            slices.append(tmpslices)
    return slices

The output should return something like: Slices is: [[0, 1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12, 13], [14, 15, 16, 17, 18, 19, 20]] (Each number references an item in stringlist)

So, as I iterate over this list of lists, I can use something like "".join([item for item in each]) to print 0,1,2,3,4,5,6 on one line, 7,8,9,10,11,12,13 on another. Sometimes, a list might only be 2 items because each of those two items are very long (would add up to under the limit of 380 characters or whatever).

I know that the code is pretty bad and that I should use a generator. I'm just not sure how to do this.

Thanks.

4
  • 3
    Are you doing this to format strings into paragraphs? If so, perhaps you could just use textwrap.wrap instead?
    – unutbu
    Feb 4, 2013 at 3:10
  • wait, whats your input for stringlist?
    – Greg
    Feb 4, 2013 at 3:21
  • "".join([item for item in each]) is just a over complicated version of "".join(each) Feb 4, 2013 at 3:25
  • To make it a generator, you just need to yield tmpslices everywhere instead of slices.append(tmpslices) and lose the return slices Feb 4, 2013 at 3:27

2 Answers 2

4

Something like this should work

def _splicegen(maxchars, stringlist):
    """
    Return a list of slices to print based on maxchars string-length boundary.
    """
    runningcount = 0  # start at 0
    tmpslice = []  # tmp list where we append slice numbers.
    for i, item in enumerate(stringlist):
        runningcount += len(item)
        if runningcount <= int(maxchars):
            tmpslice.append(i)
        else:
            yield tmpslice
            tmpslice = [i]
            runningcount = len(item)
    yield(tmpslice)

Also see the textwrap module

2
  • I put up a paste of the code and here: sprunge.us/CeSe This is how it should output: ./splicelist.py Slices is: [[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11], [12, 13, 14]] [0, 1, 2, 3, 4, 5] [6, 7, 8, 9, 10, 11] [12, 13, 14] Yes, it's similar to textwrap but doesn't textwrap truncate at a character boundary? I need to "terminate" at the list slices before it hits the maxchars number. A suggestion above said that it is like there is a character limit for a paragraph but you can't truncate items (it is).
    – a q
    Feb 4, 2013 at 3:40
  • Hi, I just tried your _slicegen and this did the trick. Thanks.
    – a q
    Feb 4, 2013 at 3:44
1

This is just a one liner. Hope its useful

>>>list=[[1,2], [1]]
>>>sorted(list, key=lambda sublist: len(sublist))
[[1], [1,2]]

Also:

>>>list=[[1,2,3],[1],[1,2]]
>>>sorted(list, key=lambda sublist: -len(sublist))
[[1,2,3], [1,2], [1]]
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.