'Python - losing last char in String while splitting/mapping
I found an issue with converting a String in multiple substrings with a given length.
I have a String of binaries, which I want to split into groups of five. However with my method right now it seems that I lose some last bits if the length of the String is not dividable by 5. How could I improve my code?
I start splitting from the front, and If there is a remainder, I want to add as many '0' to the string, to make the last 'packet' also contain 5 values
list_5bit_words = list(map(''.join, zip(*[iter(binary_i)]*5)))
print(list_5bit_words)
With '0100011001000110' I get this: ['01000', '11001', '00011'] But I want this : ['01000', '11001', '00011', '00000']
Solution 1:[1]
Try to add as many 0's as you need to de string binary_i in order to make it divisible by 5.
binary_i += '0'*(5-len(binary_i)%5)
Solution 2:[2]
The most straight forward solution is to simply implement the logic you described: check the remainder, and pad it with extra '0's at the end:
binary_i = '0100011001000110'
group_size = 5
list_5bit_words = list(map(''.join, zip(*[iter(binary_i)] * group_size)))
remainder = binary_i[len(list_5bit_words)*group_size:]
padding_len = group_size-len(remainder)
list_5bit_words.append(remainder+'0'*padding_len)
print(list_5bit_words) # Output ['01000', '11001', '00011', '00000']
However, you could simply append '0000' (4 zeroes) to the end of your string before starting to chunk it, so the last group will always be filled with 0 up to 5 chars, but no "new group" will be created:
list_5bit_words = list(map(''.join, zip(*[iter(binary_i+'0000')] * 5)))
Solution 3:[3]
This Code should work:
def split_string(string):
splitted = []
string += "0" * (4 - ((len(string) - 1) % 5))
for i in range(0, len(string), 5):
splitted.append(string[i : i + 5])
return splitted
Solution 4:[4]
You may do that with textwrap module for splitting and ljust method for filling with zeros when needed:
from textwrap import wrap
list_5bit_words = [chunk.ljust(5, '0') for chunk in wrap(binary_i, 5)]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sergio GarcÃa |
| Solution 2 | Adam.Er8 |
| Solution 3 | alanturing |
| Solution 4 | Beanonde |
