Use string formatting and list comprehension:
>>> lst = range(11)
>>> ["{:02d}".format(x) for x in lst]
['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10']
or format:
>>> [format(x, '02d') for x in lst]
['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10']
Answer from Ashwini Chaudhary on Stack OverflowUse string formatting and list comprehension:
>>> lst = range(11)
>>> ["{:02d}".format(x) for x in lst]
['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10']
or format:
>>> [format(x, '02d') for x in lst]
['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10']
zfill does exactly what you want and doesn't require you to understand an arcane mini-language as with the various types of string formatting. There's a place for that, but this is a simple job with a ready-made built-in tool.
ranger = [str(x).zfill(2) for x in range(r)]
loops - Generating a range of numbers as strings Python - Stack Overflow
How to convert numeric string ranges to a list in Python - Stack Overflow
python - Create a list with string + range - Stack Overflow
python - How to transfer range() output into string - Stack Overflow
I had the same issue and there is a perfect solution to it -zfill method
An example of usage:
>>> str('1').zfill(7)
'0000001'
What you need to do is to create a generator for N numbers and fill its string representation with zeros.
>>> for i in range(1, 18):
... str(i).zfill(2)
...
'01'
'02'
'03'
...
'16'
'17'
>>> [str(i).zfill(2) for i in range(1,18)]
['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17']
def f(x):
result = []
for part in x.split(','):
if '-' in part:
a, b = part.split('-')
a, b = int(a), int(b)
result.extend(range(a, b + 1))
else:
a = int(part)
result.append(a)
return result
>>> f('1,2,5-7,10')
[1, 2, 5, 6, 7, 10]
I was able to make a true comprehension on that question:
>>> def f(s):
return sum(((list(range(*[int(j) + k for k,j in enumerate(i.split('-'))]))
if '-' in i else [int(i)]) for i in s.split(',')), [])
>>> f('1,2,5-7,10')
[1, 2, 5, 6, 7, 10]
>>> f('1,3-7,10,11-15')
[1, 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 15]
the other answer that pretended to have a comprehension was just a for loop because the final list was discarded. :)
For python 2 you can even remove the call to list!
You can use f-strings to do so :
my_list = [f"Unnamed: {i}" for i in range(16, 60)]
# Output
['Unnamed: 16', 'Unnamed: 17', 'Unnamed: 18', 'Unnamed: 19', ...]
I would do it following way
prefix = "Unnamed: "
lst = [prefix + str(i) for i in range(16,25)]
print(lst)
output
['Unnamed: 16', 'Unnamed: 17', 'Unnamed: 18', 'Unnamed: 19', 'Unnamed: 20', 'Unnamed: 21', 'Unnamed: 22', 'Unnamed: 23', 'Unnamed: 24']
Note: I used othre range for brevity sake. You might elect to use one of string formatting method instead.
Just looking for clarification for this very simple thing. Please correct me if Iβm wrong but from what I understand, in for i in string, it takes each element in the string over the entire length. For i in range(len(string)) it indexes the elements and looks at the elements at each index so at position 0, string =βxβ and at 1 string= βdβ ect.
If this is correct, Iβm afraid I still donβt see the difference by way of when to use each or what purpose they serve.