[GO] [Python] Calculate the number of digits required when filling in 0s [Note]

When I learned log in high school, I always use log! I thought it was a good idea, so I took notes.

import math

list = [1,2,3,4,5,6,7,8,9,10,11]

number_of_val = len(list)
number_of_padding_zero = math.ceil(math.log10(number_of_val))
format_pattern = '{:0'+ str(number_of_padding_zero) +'d}'

for i,val in enumerate(list):
  serial_number = format_pattern.format(i)
  print(serial_number)

Execution result:

00
01
02
03
04
05
06
07
08
09
10

It is assumed that when outputting a file with a serial number, the holes are filled with 0 (zero padding). Since it is for testing, the list in the code contains numbers, but please think that it contains the file path and so on. Count the number of output files, take log10 and round up. It feels good to output from 0 instead of from 1.

format_pattern = '{:0'+ str(number_of_padding_zero) +'d}' ↑ I feel that this code is absolutely useless, but it works for the time being, so it's a good thing. Please let me know in the comments if you have any good writing style. Some people may think that you can use the join method, Personally, I just like the notation that connects with + because it can be written in common in most languages.

Speaking of which, log10 is the common logarithm. You really use it. When I looked it up, it was a normal thing to take the common logarithm and count the number of digits. No, if I don't have a chance to actually use it, I'll be asked "What should I do if I know the number of digits?" Also, in the first place, if I used the common logarithm, I was completely out of my mind. I somehow realized that it would be nice to use logarithms! Rather compliment ()

Recommended Posts

[Python] Calculate the number of digits required when filling in 0s [Note]
How to get the number of digits in Python
Calculate the square root of 2 in millions of digits with python
Calculate the total number of combinations with python
[Note] About the role of underscore "_" in Python
Note when putting lxml of python package in ubuntu 14.04
Get the size (number of elements) of UnionFind in Python
Get the number of digits
Calculate the number of changes
Python --Find out number of groups in the regex expression
[Note] Import of a file in the parent directory in Python
[Homology] Count the number of holes in data with Python
Calculate the previous month in Python
Count the number of Thai and Arabic characters well in Python
[Python] Let's reduce the number of elements in the result in set operations
Get the number of readers of a treatise on Mendeley in Python
Check the behavior of destructor in Python
The result of installing python in Anaconda
The basics of running NoxPlayer in Python
In search of the fastest FizzBuzz in Python
Python Note: The secret role of commas
Project Euler # 17 "Number of Characters" in Python
[Python] Yuriko Koike Calculate the number of votes you need exactly [matplotlib]
Python Note: When you want to know the attributes of an object
Check the in-memory bytes of a floating point number float in Python
A useful note when using Python for the first time in a while
plot the coordinates of the processing (python) list and specify the number of times in draw ()
[Python] Sort the list of pathlib.Path in natural sort
[Note] Export the html of the site with python.
Get the caller of a function in Python
Match the distribution of each group in Python
View the result of geometry processing in Python
[Python] Precautions when finding the maximum and minimum values in a numpy array with a small number of elements
Make a copy of the list in Python
Find the number of days in a month
Note on encoding when LANG = C in Python
A note about the python version of python virtualenv
Find the divisor of the value entered in python
Set an upper limit on the number of recursive function iterations in Python
Visualize the timeline of the number of issues on GitHub assigned to you in Python
Find the solution of the nth-order equation in python
The story of reading HSPICE data in Python
About the behavior of Model.get_or_create () of peewee in Python
Solving the equation of motion in Python (odeint)
Output in the form of a python array
How to identify the element with the smallest number of characters in a Python list?
[Python] A program that finds the shortest number of steps in a game that crosses clouds
An easy way to pad the number with zeros depending on the number of digits [Python]
Note: The meaning of specifying only * (asterisk) as an argument in the Python function definition.
[Python] Representing the number of complaints from life insurance companies in a bar graph
How to count the number of occurrences of each element in the list in Python with weight
Check the processing time and the number of calls for each process in python (cProfile)
Display n digits after the decimal point in python
Check if the string is a number in python
python beginners tried to predict the number of criminals
Python note: When the pip command cannot be used
Experience the good calculation efficiency of vectorization in Python
[Python] A program that counts the number of valleys
Note the frequently used options in Python + Selenium + Chrome
[Python] Calculate the average value of the pixel value RGB of the object
Initial settings when using the foursquare API in python