Adarsha Verma
Adarsha Verma

Reputation: 73

python split array into equal length values

I am new to python. I have a list of arbitrary length long int values, and I need to split it up into equal size value. I tried so many ways to do this but it is not printing in correct format.

I defined a array called

arr = [10816446963217280179219841728017921984108164469632
928047221121299210511552129921051155292804722112
50562677440755267009927552670099250562677440
23552216364842368604561924236860456192235522163648
179217208962112891264211289126417921720896
30081517568473642615684736426156830081517568
2624284224185650156818565015682624284224
5765318451245216051245216057653184
288020416028163121792281631217922880204160
];

I have an API that will return an array value. I will use that output and i do this operation:

arr = [1081........
.................];
for i in range(0, len(arr), 8):
print " ".join(map(str,arr[i:i + 8]))

10816 4469632 17280 17921984 17280 17921984 10816 4469632
9280 4722112 12992 10511552 12992 10511552 9280 4722112
5056 2677440 7552 6700992 7552 6700992 5056 2677440
23552 2163648 42368 60456192 42368 60456192 23552 2163648
1792 1720896 2112 891264 2112 891264 1792 1720896
3008 1517568 4736 4261568 4736 4261568 3008 1517568
2624 284224 1856 501568 1856 501568 2624 284224
576 53184 512 452160 512 452160 576 53184
2880 204160 2816 3121792 2816 3121792 2880 204160

I need to print values like this:

  a      b      c        d        e      f        g       h
10816  4469632 17280  17921984  17280  17921984 10816  4469632
9280   4722112 12992  10511552  12992  10511552 9280   4722112
5056   2677440 7552   700992    7552   6700992  5056   2677440
23552  2163648 42368  60456192  42368  60456192 23552  2163648
1792   1720896 2112   891264    2112   891264   1792   1720896
3008   1517568 4736   4261568   4736   4261568  3008   1517568
2624   284224  1856   501568    1856   501568   2624   284224
576    53184   512    452160    512    452160   576    53184
2880   204160  2816   3121792   2816   3121792  2880   204160

I tried with '\t' like

 for i in range(0, len(arr), 8):
 print "\t".join(map(str,arr[i:i + 8]))

but it is not printing properly please help me to do this. Thanks in advance.

Upvotes: 0

Views: 161

Answers (1)

Steven Summers
Steven Summers

Reputation: 5394

Using str.format you can align your values. <N will left align and fill to N characters, >N will right align and ^N will format between and fill left and right side until N characters.

for i in range(0, len(arr), 8):
    for item in arr[i:i+8]:
        print('{:<8}'.format(item), end = ' ')
    print()

Or more compact

for i in range(0, len(arr), 8):
    print(' '.join('{:<8}'.format(item) for item in arr[i:i+8]))

# Output
10816    4469632  17280    17921984 17280    17921984 10816    4469632 
9280     4722112  12992    10511552 12992    10511552 9280     4722112 
5056     2677440  7552     700992   7552     6700992  5056     2677440 
23552    2163648  42368    60456192 42368    60456192 23552    2163648 
1792     1720896  2112     891264   2112     891264   1792     1720896 
3008     1517568  4736     4261568  4736     4261568  3008     1517568 
2624     284224   1856     501568   1856     501568   2624     284224  
576      53184    512      452160   512      452160   576      53184   
2880     204160   2816     3121792  2816     3121792  2880     204160 

Upvotes: 1

Related Questions