Change language

Split a string into equal parts (grouper in Python)

| | |

Existing Itertools used:
izip_longest (* iterables [, fillvalue]) : create an iterator that concatenates elements from each from repeatable elements. If the iterations are of unequal length, the missing values ​​are filled with the padding value. The iteration continues until the longest iteration is exhausted.

Performance:

  • Advanced tools offer the same high performance as the basic toolset .
  • Superior memory performance is supported by processing elements one at a time, rather than carrying everything iterated into memory at once.
  • Keeping the code small by tying tools together in a functional style that helps eliminate temporary variables .
  • High speed is achieved by preferring "vectorized" building blocks over using for loops and generators, which incur interpreter overhead.

Examples:

  Input:  str = ABCDEFG, l = 3  Output:  ABC DEF Gxx  Explaination:  Grouping characters of string in set of 3: ABC DEF Gxx. ’x’ is added to the set which doesn’t have 3 elements in it.  Input:  str = GEEKSFORGEEKS, l = 5  Output:  GEEKS FORGE EKSxx 

Below is the Python3 code:

# Python3 code for group recipe

 
#import existing itertool izip_longest

from itertools import izip_longest

 
# function for perch recipe

def grouper (iterable, n, fillvalue = ’x’ ):

 

# create & # 39; n & # 39; -blocks for the collection

  args = [ iter (iterable)] * n

  

# collect data into fixed-length blocks

# length & # 39; n & # 39; using izip_longest and store

# result as a list

ans = list (izip_longest (fillvalue = fillvalue, * args))

 

# (optional) loop to convert ANS to string

  t = len (ans)

for i in range < code class = "plain"> (t):

ans [i] = "". join (ans [i])

 

  # return ans as a string

return "" . join (ans) 

 

 
# Driver code

s = "ABCDEFG"

k = 3

 

result = grouper (s, k)

print (result) 

Exit :

 ABC DEF Gxx 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically