Python问题中的二进制字符串

the*_*e_e 3 python string binary

出于某种原因,我很想知道如何在Python中做到这一点.我试图在字符串变量中表示二进制字符串,而我想要的只是

0010111010
Run Code Online (Sandbox Code Playgroud)

但是,无论我如何尝试将其格式化为字符串,Python总是会删除前导零,这让我在尝试解析它时感到头痛.

我希望这个问题会有所帮助,但事实并非如此......

有没有办法强制Python停止自动将我的字符串转换为整数?

我尝试过以下方法:

val = ""
if (random.random() > 0.50):
  val = val + "1"
else
  val = val + "0"
Run Code Online (Sandbox Code Playgroud)

val = ""
if (random.random() > 0.50):
  val = val + "%d" % (1)
else:
  val = val + "%d" % (0)
Run Code Online (Sandbox Code Playgroud)

我以前把它插入到一个数组中,但遇到了将该数组插入另一个数组的问题,所以我认为将它解析为字符串会更容易.

关于如何让我的领先零回归的任何想法?如果有帮助,该字符串应该是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
Run Code Online (Sandbox Code Playgroud)

希望我看到的输出会告诉你为什么我遇到问题:

[[  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]]
Run Code Online (Sandbox Code Playgroud)

这里的问题不仅仅是打印,我还需要能够在每个元素的基础上操作它们.所以,如果我去玩第一个元素,那么它返回1,而不是0(在第一个元素上).

ovg*_*vin 7

如果我理解你,你可以这样做:

a = 0b0010111010
'{:010b}'.format(a)

#The output is: '0010111010'
Run Code Online (Sandbox Code Playgroud)

Python 2.7

它使用字符串format方法.

如果要使用前导零表示二进制字符串,这就是答案.

如果您只是尝试使用二进制文件生成随机字符串,则可以这样做:

from random import choice
''.join(choice(('0','1')) for _ in range(10))
Run Code Online (Sandbox Code Playgroud)

更新

不要更新您的更新.我制作了一个与你的相比具有不同输出的代码:

from random import choice
from pprint import pprint

arr = []

def create_string(x):
    for i in xrange(10):       # 10 random populations
        arr.append([])
        for j in xrange(x):  # population size
            v = ''.join(choice(('0','1')) for _ in range(10))
            arr[-1].append(v)
    return arr

a = create_string(5)
pprint(a)
Run Code Online (Sandbox Code Playgroud)

输出是:

[['1011010000', '1001000010', '0110101100', '0101110111', '1101001001'],
 ['0010000011', '1010011101', '1000110001', '0111101011', '1100001111'],
 ['0011110011', '0010101101', '0000000100', '1000010010', '1101001000'],
 ['1110101111', '1011111001', '0101100110', '0100100111', '1010010011'],
 ['0100010100', '0001110110', '1110111110', '0111110000', '0000001010'],
 ['1011001011', '0011101111', '1100110011', '1100011001', '1010100011'],
 ['0110011011', '0001001001', '1111010101', '1110010010', '0100011000'],
 ['1010011000', '0010111110', '0011101100', '1111011010', '1011101110'],
 ['1110110011', '1110111100', '0011000101', '1100000000', '0100010001'],
 ['0100001110', '1011000111', '0101110100', '0011100111', '1110110010']]
Run Code Online (Sandbox Code Playgroud)

这是你想要的?