出于某种原因,我花了很长时间才弄清楚如何在 Python 中做到这一点。
我试图在字符串变量中表示一个二进制字符串,而我想要它拥有的只是
0010111010
但是,无论我如何尝试将其格式化为字符串,Python总是会砍掉前导零,这让我很头疼试图解析它。
我希望这个问题会有有帮助,但实际上并没有...
有没有办法强制Python停止自动将我的字符串转换为整数?
我已经尝试过以下操作:
val = ""
if (random.random() > 0.50):
val = val + "1"
else
val = val + "0"
并且
val = ""
if (random.random() > 0.50):
val = val + "%d" % (1)
else:
val = val + "%d" % (0)
我之前已将其插入一个数组中,但在将该数组插入另一个数组时遇到了问题,所以我认为将其解析为字符串会更容易。
关于如何恢复我的前导零有什么想法吗?如果有帮助的话,该字符串应该是 10 位的固定长度。
编辑:
代码:
def create_string(x):
for i in xrange(10): # 10 random populations
for j in xrange(int(x)): # population size
v = ''.join(choice(('0','1')) for _ in range(10))
arr[i][j] = v
return arr
a = create_string(5)
print a
希望我看到的输出能够告诉您为什么我遇到问题:
[[ 10000100 1100000001 101010110 111011 11010111]
[1001111000 1011011100 1110110111 111011001 10101000]
[ 110010001 1011010111 1100111000 1011100011 1000100001]
[ 10011010 1000011001 1111111010 11100110 110010101]
[1101010000 1010110101 110011000 1100001001 1010100011]
[ 10001010 1100000001 1110010000 10110000 11011010]
[ 111011 1000111010 1100101 1101110001 110110000]
[ 110100100 1100000000 1010101001 11010000 1000011011]
[1110101110 1100010101 1110001110 10011111 101101100]
[ 11100010 1111001010 100011101 1101010 1110001011]]
这里的问题不仅与打印有关,我还需要能够在每个元素的基础上操作它们。因此,如果我去处理第一个元素,那么它会返回 1,而不是 0(在第一个元素上)。
For some reason I'm having a heck of a time figuring out how to do this in Python.
I am trying to represent a binary string in a string variable, and all I want it to have is
0010111010
However, no matter how I try to format it as a string, Python always chops off the leading zeroes, which is giving me a headache in trying to parse it out.
I'd hoped this question would have helped, but it doesn't really...
Is there a way to force Python to stop auto-converting my string to an integer?
I have tried the following:
val = ""
if (random.random() > 0.50):
val = val + "1"
else
val = val + "0"
and
val = ""
if (random.random() > 0.50):
val = val + "%d" % (1)
else:
val = val + "%d" % (0)
I had stuck it into an array previously, but ran into issues inserting that array into another array, so I figured it would just be easier to parse it as a string.
Any thoughts on how to get my leading zeroes back? The string is supposed to be a fixed length of 10 bits if that helps.
Edit:
The code:
def create_string(x):
for i in xrange(10): # 10 random populations
for j in xrange(int(x)): # population size
v = ''.join(choice(('0','1')) for _ in range(10))
arr[i][j] = v
return arr
a = create_string(5)
print a
Hopefully the output I'm seeing will show you why I'm having issues:
[[ 10000100 1100000001 101010110 111011 11010111]
[1001111000 1011011100 1110110111 111011001 10101000]
[ 110010001 1011010111 1100111000 1011100011 1000100001]
[ 10011010 1000011001 1111111010 11100110 110010101]
[1101010000 1010110101 110011000 1100001001 1010100011]
[ 10001010 1100000001 1110010000 10110000 11011010]
[ 111011 1000111010 1100101 1101110001 110110000]
[ 110100100 1100000000 1010101001 11010000 1000011011]
[1110101110 1100010101 1110001110 10011111 101101100]
[ 11100010 1111001010 100011101 1101010 1110001011]]
The issue here isn't only with printing, I also need to be able to manipulate them on a per-element basis. So if I go to play with the first element, then it returns a 1, not a 0 (on the first element).
发布评论
评论(2)
如果我理解正确,你可以这样做:
Python 2.7
它使用字符串
格式
方法。如果您想表示带有前导零的二进制字符串,这就是答案。
如果您只是尝试用二进制文件生成随机字符串,您可以这样做:
更新
不更新您的更新。
我编写了一个与您的代码相比具有不同输出的代码:
输出是:
这是您正在寻找的吗?
If I understood you right, you could do it this way:
Python 2.7
It uses string
format
method.This is the answer if you want to represent the binary string with leading zeros.
If you are just trying to generate a random string with a binary you could do it this way:
Update
Unswering your update.
I made a code which has a different output if compared to yours:
The output is:
Is this what you are looking for?
下面怎么样:
这给出了一个十个字符的二进制字符串;没有删除前导零。
如果您不需要改变数字概率(上面的
0.50
),一个稍微简洁的版本是:How about the following:
This gives a ten-character binary string; there's no chopping off of leading zeroes.
If you don't need to vary digit probability (the
0.50
above), a slightly more concise version is: