2013-03-30 40 views
0

有什麼辦法可以讓我的輸出放在16行中?這些行還需要有16列垂直排列。我想在不輸入任何內容的情況下執行此操作。Python - 輸出行數爲16

def main(): 

    # asks the user to input key 
    key = input("Enter a key [6-15 characters]: ") 

    s = [] 

    # append all items in list 
    for i in range(256): 

     s.append(i) 

    #Runs the key through a loop to give RC4 encryption and assigns j to 0 
    j = 0 
    for i in range(256): 
     j = ((j+s[i]+ord(key[i%len(key)]))%256) 
     #Swap values of s[i] and s[j] 
     s[i],s[j] = s[j],s[i] 
    print(s) 

main() 
+0

你不回或'main'打印任何東西,所以沒有輸出在所有... – nneonneo

+0

對不起,我編輯的它。 –

+0

順便說一句。而不是's = [](...)s.append(...)''你可以做's = list(range(256))''。 – Tadeck

回答

1

你是否想要16行,每行16個字符?以下是我會做它(十六進制打印):

s = range(256) 

for i in xrange(256): 
    j = ((j+s[i]+ord(key[i%len(key)]))%256) 
    s[i],s[j] = s[j],s[i] 

for i in xrange(0, 256, 16): 
    print ''.join('%02x' % c for c in s[i:i+16]) 

爲256個字節的隨機生成的序列(整數範圍爲[0,255]),我得到

f7a98bd77f4accd553f1b86f582d678e 
265e38af7c6c4e09af0a6132bec36fb5 
47527d8acc69c4f3f099d0fa34fdb527 
7a170e102c794692356ca0e441c787e9 
98dfb550b316a3952e67983489f4f1f4 
8f8db56a58e94b6beae95fc5f394295a 
83ed9c72b6c24dbf15ad28506d505eed 
e2359cdeed6c1526b2c3d11b14074b08 
332e37fcd686b50d6de15df4feb8c404 
de5c0ae40eaf755f209300ad5143397f 
77b626dab0c99e9658a4eb1d5f028a40 
bb5ed8d40b6dc925954990aa84bed906 
5ed527fab36762e8c65069a2f4a68888 
0a114e0019e3b2174265d77a9e959a06 
52429fc7106db54098a6c8a1219625aa 
e59b98fc6c38a115fca2e0d7faf3376f 

你可以調整最後一行以滿足您的需求。例如,要打印十進制整數,使用

for i in xrange(0, 256, 16): 
    print ''.join('%4d' % c for c in s[i:i+16]) 

得到

92 142 194 158 114 83 120 69 155 118 191 72 33 53 216 219 
    29 97 30 213 236 177 170 96 101 45 126 6 48 43 185 2 
139 123 47 19 147 191 222 127 254 26 248 183 192 232 190 226 
110 125 222 252 212 235 246 120 107 195 52 251 169 220 157 182 
169 47 151 122 131 73 174 97 175 202 201 225 66 179 34 160 
105 62 117 227 80 217 25 17 120 51 56 209 238 249 121 21 
152 100 113 190 209 243 165 44 118 85 195 236 176 105 149 225 
146 225 69 225 89 12 176 194 88 223 141 185 107 216 157 198 
    16 57 132 10 3 155 130 227 30 10 11 31 7 212 244 61 
    50 212 21 98 202 244 62 180 23 5 148 3 91 53 226 147 
189 189 222 124 179 194 147 43 46 94 216 62 10 44 149 16 
173 120 78 75 123 113 61 4 106 100 96 117 243 181 118 148 
223 110 83 117 212 86 83 161 141 48 193 48 255 196 20 10 
169 144 64 160 239 136 34 145 104 101 53 58 5 178 20 141 
    46 40 254 153 166 108 172 215 184 222 55 127 116 210 49 216 
202 120 34 208 22 57 98 179 238 153 173 195 101 229 113 220 
+0

對不起,我想要16行產生的數字 –

+0

請注意,在密碼學中,用十六進制表示數據更爲典型。如果你想要十進制數字,把''%02x''改成''%04d''。 – nneonneo