[Tutor] Problems padding a string with 0's while deconcatonating
Josh Yagy
JYAGY1 at pride.hofstra.edu
Tue Dec 6 00:49:34 CET 2005
Wow, that code is much more compact, thanks for the help! But as far as the original question goes, I think I worded my problem wrong. The output you got with your binary string is almost what I Want, but not quite. I need each subset of binary strings to be n bits long (in the dummy problem you ran, 4 bits). The last subset is two bits short, but for the sake of the cryptosystem, I can't pad it with zeros at the right, I need to pad it to the left. for instance:
Your output was:
print binaryString(b, 4) -> ['0101', '0011', '0001', '1100', '0011', '11']
What I'm looking for is for the output to be:
print binaryString(b, 4) -> ['0001', '0100', '1100', '0111', '0000', '1111']
notice that my desired output is the same as yours, only all the bits are moved back two places to fill up the last subset, and the extra space is filled with 0's in the first subset.
Thanks again for the help
-----Original Message-----
From: Kent Johnson <kent37 at tds.net>
Date: Mon, 05 Dec 2005 13:05:46 -0500
Subject: Re: [Tutor] Problems padding a string with 0's while deconcatonating
Josh Yagy wrote:
>I have the following code:
>
>def binaryString(b, n):
> s=[]
> j=0
> while j < len(b):
> temp = b[j:j+n]
> s = s + [ temp ]
> j = j + n
> return s
>
>which works for all intents and purposes, but I can't figure out a way to get it to pad the left of the string with zeros so that each of the subsets is n bits long. As is, it will deconcatonate a given string into n-substrings, but not all of the substrings are n bits long. Any help would be greatly appreciated and sorry again if I didn't include anything I should.
>
Josh,
I don't think I understand the problem. When I try your function I get
b= '0101001100011100001111'
print binaryString(b, 4) -> ['0101', '0011', '0001', '1100', '0011', '11']
How does this differ from what you want? I am not losing any leading
zeros, just the last substring is short.
BTW your function can be written much more compactly. I'll show you how
in a few steps.
First, notice that the sequence of values in j can be generated with the
range() function, e.g.
>>> range(0, len(b), 4)
[0, 4, 8, 12, 16, 20]
This lets you replace the calculation of j with a simple for statement:
def binaryString2(b, n):
s=[]
for j in range(0, len(b), n):
temp = b[j:j+n]
s = s + [ temp ]
return s
Next, use list.append() to add to s and get rid of temp:
def binaryString3(b, n):
s=[]
for j in range(0, len(b), n):
s.append(b[j:j+n])
return s
Now this can be easily replaced with a single list comprehension:
def binaryString4(b, n):
return [b[j:j+n] for j in range(0, len(b), n)]
(This version appears in the Python Cookbook at
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/425044)
Kent
_______________________________________________
Tutor maillist - Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list