0

I would like to generate 3 digits numbers randomly. But I can't find how to display leading zero after the range function.

I tried on Jupyter notebook with python3 version.

    import random
    raw_list = list(range(000,999))
    print ("Original list : ",  raw_list)
    random.shuffle(raw_list) #shuffle method
    print ("List after first shuffle  : ",  raw_list)

I only get numbers like that -

List after first shuffle  :  [474, 421, 46, 183,.....................

The final output to I would like to generate is [000,111,002,...098............,999]

Swe Zin Phyoe
  • 149
  • 1
  • 2
  • 11

2 Answers2

0

I think in int you lose always the leading 0. You could switch to str and add the leading zeros for example like that:

def add_zeros(z):
    while len(z) < 3:
        z = '0' + z
    return z

new_list = [add_zeros(str(z)) for z in raw_list]
print ("List after first shuffle  : ",  new_list)
Kvothe
  • 110
  • 4
0
import random

raw_list = list(range(000, 999))
n_raw_list=[str(num).zfill(3) for num in raw_list]
print("Original list : ", n_raw_list)
random.shuffle(n_raw_list)  # shuffle method
print("List after first shuffle  : ", n_raw_list)

Try this hopefully this will work

Vashdev Heerani
  • 660
  • 7
  • 21