Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate list of numbers in specific format

Tags:

I need to generate a list of numbers in a specific format. The format is

mylist = [00,01,02,03,04,05,06,07,08,09,10,11,12,13,14,15] #Numbers between 0-9 are preceded by a zero. 

I know how to generate a normal list of numbers using range

>>> for i in range(0,16): ...     print i 

So, is there any built-in way in python to generate a list of numbers in the specified format.

like image 815
RanRag Avatar asked Aug 19 '12 21:08

RanRag


People also ask

How do I make a list of numbers?

To start a numbered list, type 1, a period (.), a space, and some text. Word will automatically start a numbered list for you. Type* and a space before your text, and Word will make a bulleted list. To complete your list, press Enter until the bullets or numbering switch off.

How do you make a list of numbers from 1 to N?

To create a list of numbers from 1 to N: Use the range() class to create a range object from 1 to N. Use the list() class to convert the range object to a list. The new list will contain the numbers in the specified range.


1 Answers

Python string formatting allows you to specify a precision:

Precision (optional), given as a '.' (dot) followed by the precision.

In this case, you can use it with a value of 2 to get what you want:

>>> ["%.2d" % i for i in range(16)] ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12',  '13', '14', '15'] 

You could also use the zfill function:

>>> str(3).zfill(2) '03' 

or the string format function:

>>> "{0:02d}".format(3) '03' 
like image 71
jterrace Avatar answered Sep 30 '22 00:09

jterrace