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