从给定字符生成固定长度随机字符串的内置方法 [英] Built-in method to Generate Random Strings of Fixed Length From Given Characters
问题描述
这就是我的问题:我需要创建一个 50 个字符长的随机字符串,由 1
s 和 0
s 组成.
This is what my problem is: I need to make a random string 50 characters long, made up of 1
s and 0
s.
我知道如何解决这个问题,甚至有一个单线.我也在 SO 上寻找解决这个问题的各种解决方案,只是为了找回我已经知道的(1、2 等).但我真正想要的是最 Pythonic 的方式来做到这一点.
I know how to solve this problem, and even have a one-liner for it. I have also looked for various solutions to this problem on SO, only to get back what I already know(1, 2, etc). But what I really want is the most Pythonic way of doing this.
目前,我倾向于 ''.join(( random.choice([0,1]) for i in xrange(50) ))
有没有更pythonic的方法来做到这一点?是否有一个内置的可以做这样的事情,也许在 itertools
中?
Is there a more pythonic way of doing this? Is there a built-in that does something like this, perhaps in itertools
?
推荐答案
对于 Python2.7 或更高版本:
For Python2.7 or better:
In [83]: import random
In [84]: '{:050b}'.format(random.randrange(1<<50))
Out[84]: '10011110110110000011111000011100101111101001001011'
(在 Python2.6 中,使用 '{0:050b}'
而不是 '{:050b}'
.)
(In Python2.6, use '{0:050b}'
instead of '{:050b}'
.)
说明:
string.format
方法可以将整数转换为其二进制字符串表示形式.执行此操作的基本格式代码是 '{:b}'
:
The string.format
method can convert integers into their binary string representations. The basic format code to do this is '{:b}'
:
In [91]: '{:b}'.format(10)
Out[91]: '1010'
要制作宽度为 50 的字符串,请使用格式代码 '{:50b}'
:
To make a string of width 50, use the format code '{:50b}'
:
In [92]: '{:50b}'.format(10)
Out[92]: ' 1010'
并用零填充空格,使用 {:050b}
:
and to fill in the whitespace with zeros, use {:050b}
:
In [93]: '{:050b}'.format(10)
Out[93]: '00000000000000000000000000000000000000000000001010'
str.format 的语法 有点令人生畏首先.这是我的备忘单:
The syntax for str.format is a bit daunting at first. Here is my cheat sheet:
http://docs.python.org/library/string.html#format-string-syntax
replacement_field ::= "{" field_name ["!" conversion] [":" format_spec] "}"
field_name ::= (identifier|integer)("."attribute_name|"["element_index"]")*
attribute_name ::= identifier
element_index ::= integer
conversion ::= "r" | "s"
format_spec ::= [[fill]align][sign][#][0][width][,][.precision][type]
fill ::= <a character other than '}'>
align ::= "<" | ">" | "=" | "^"
"=" forces the padding to be placed after the sign (if any)
but before the digits. (for numeric types)
"<" left justification
">" right justification
"^" center justification
sign ::= "+" | "-" | " "
"+" places a plus/minus sign for all numbers
"-" places a sign only for negative numbers
" " places a leading space for positive numbers
# for integers with type b,o,x, tells format to prefix
output with 0b, 0o, or 0x.
0 enables zero-padding. equivalent to 0= fill align.
width ::= integer
, tells format to use a comma for a thousands separator
precision ::= integer
type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" |
"o" | "x" | "X" | "%"
c convert integer to corresponding unicode character
n uses a locale-aware separator
% multiplies number by 100, display in 'f' format, with percent sign
这篇关于从给定字符生成固定长度随机字符串的内置方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!