Slice a binary number into groups of five digits

后端 未结 7 1335
野性不改
野性不改 2021-01-21 07:34

Is there any neat trick to slice a binary number into groups of five digits in python?

\'00010100011011101101110100010111\' => [\'00010\', \'00110\', \'10111\', ... ]

7条回答
  •  渐次进展
    2021-01-21 07:55

    How about using a regular expression?

    >>> import re
    >>> re.findall('.{1,5}', '00010100011011101101110100010111')
    ['00010', '10001', '10111', '01101', '11010', '00101', '11']
    

    This will break though if your input string contains newlines, that you want in the grouping.

提交回复
热议问题