How to parse a list or string into pieces of a fixed length

I really got stuck on the main question. I am trying to take a list of one element and divide it into a list of many elements, each of which has a character length of 10. For example, a list with one element, ['111111111122222222223333333333'] , the result will produce:

 1111111111 2222222222 3333333333 

I feel it's super easy, but I'm at a dead end. 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: print('Done') 

Obviously this does not work. Any advice? Is it easier to use a string than a list?

+6
source share
4 answers

The corresponding question was asked: Cutting the list into a list of subscriptions

For example, if your source list is:

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

you can break it like this:

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

Assuming n is the length of your sub-list, and the result will be:

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

Then you can iterate through it, for example:

 for sub_list in split_list: # Do something to the sub_list 

The same goes for strings.

Here is 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'] 
+9
source

Using:

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

Although this question was published after 4 years, but here is another way to do this, use the textwrap module . From the doc:

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

Wraps around one paragraph in the text (line), so each line has a length of no more than the width characters. Returns a list of output lines without a final new line.

The optional keyword arguments correspond to the attributes of the TextWrapper instance described below. default width is 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 >>> 
+1
source

Other ways to do this 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
source

Source: https://habr.com/ru/post/890678/


All Articles