开发者

how to parse a list or string into chunks of fixed length

开发者 https://www.devze.com 2023-03-13 21:20 出处:网络
I\'m really stuck on a basic question. I am trying to take a list of one item and divide it into a list of many items each with a charater length of 10. For example give a list with one item, [\'11111

I'm really stuck on a basic question. I am trying to take a list of one item and divide it into a list of many items each with a charater length of 10. For example give a list with one item, ['111111111122222222223333333333'], the output would produce:

1111111111
2222222222
3333333333

I feel like this is super simple, but I'm stumped. I tried to create a function like this:

def parser(nub):    
    while len(nub) > 10:  
        for subnub in nub:  
            subnub = nub[::10]
            return(subnub)  
    else:开发者_JAVA百科  
        print('Done')

Obviously, this doesn't work. Any advice? Would using a string be easier than a list?


A related question has been asked: Slicing a list into a list of sub-lists

For example, if your source list is:

the_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, ... ]

you can split it like:

split_list = [the_list[i:i+n] for i in range(0, len(the_list), n)]

assuming n is your sub-list length and the result would be:

[[1, 2, 3, ..., n], [n+1, n+2, n+3, ..., 2n], ...]

Then you can iterate through it like:

for sub_list in split_list:
    # Do something to the sub_list

The same thing goes for strings.

Here's a practical example:

>>> n = 2
>>> listo = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> split_list = [listo[i:i+n] for i in range(0, len(listo), n)]
>>> split_list
[[1, 2], [3, 4], [5, 6], [7, 8], [9]]

>>> listo = '123456789'
>>> split_list = [listo[i:i+n] for i in range(0, len(listo), n)]
>>> split_list
['12', '34', '56', '78', '9']


Use:

value = '111111111122222222223333333333'
n = 10
(value[i:i+n] for i in xrange(0, len(value), n))


Although this question has been posted after 4 years, but here is a another way to do this use textwrap module. From the document:

textwrap.wrap(text[, width[, ...]])

Wraps the single paragraph in text (a string) so every line is at most width characters long. Returns a list of output lines, without final newlines.

Optional keyword arguments correspond to the instance attributes of TextWrapper, documented below. width defaults to 70.

So we can do it like this:

>>> import textwrap
>>> myList = ['111111111122222222223333333333']

>>> [i for text in myList for i in textwrap.wrap(text, 10)]
['1111111111', '2222222222', '3333333333']

>>> for i in [i for text in myList for i in textwrap.wrap(text, 10)]:
...     print i
1111111111
2222222222
3333333333
>>> 


Other ways to do it recursively:

Option 1: Recursive function

>>> def chunks(x, n=10):
...      if len(x) <= n:
...          return [x]
...      else:
...          return [x[:n]] + chunks(x.replace(x[:n], ''))
...
>>> seq = ['111111111122222222223333333333']
>>> print chunks(seq[0])
['1111111111', '2222222222', '3333333333']

Option 2: Recursive lambda

>>> n = 10
>>> chunks = lambda x: [x] if len(x) <= n else [x[:n]] + chunks(x.replace(x[:n], ''))
>>> print chunks(seq[0])
['1111111111', '2222222222', '3333333333']
0

精彩评论

暂无评论...
验证码 换一张
取 消