开发者

remove the spaces

#!/usr/bin/python
import random
lower_a = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
upper_a = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
num = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

all = []
all = "".join("".join(lower_a) + "".join(upper_a) + "".join(num))
all = all.split()
x = 0
while x < 10:
    for i in range(7):
        a = random.choice(all)
        print a,
    print
    x += 1

开发者_运维百科What I want to do is remove the spaces from the output. What it gives now is:

Z 3 a A I K R
G B i N 9 c E
v g E r A N 8
e B 6 d v H O
c a V 8 c x y
b g 2 W a T T
f 8 H T r 6 E
p D K l 5 p u
x q 8 P Z 9 T
n I W X n B Q


Putting it all together with the string module.

import random
import string

chars = string.ascii_uppercase + string.ascii_lowercase + string.digits

for row in range(10):
    print ''.join(random.choice(chars) for col in range(7))


instead of:

 for i in range(7):
  a = random.choice(all)
  print a,
 print

you could do this:

 print "".join(random.choice(all) for x in range(7))


nevermind - i fixed it

while x < 10:
    y = []
    for i in range(7):
        a = random.choice(all)
        y.append(a)
    print "".join(y)
    x += 1


The space is placed by print a,. A comma following print statement print a space.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜