Python:在不使用 .zfill 的情况下在单个数字中添加零
Python: add zeroes in single digit numbers without using .zfill
我目前使用的是 micropython,它没有 .zfill 方法。
我想要得到的是获得 UTC 的 YYMMDDhhmmss。
例如,它给我的时间是
t = (2019, 10, 11, 3, 40, 8, 686538, None)
我可以使用 t[:6] 访问我需要的那些。现在问题出在个位数,3 和 8。我能够让它显示 1910113408,但我需要得到 19101034008 我需要在那些 2 之前得到零。我使用
t = "".join(map(str,t))
t = t[2:]
所以我的想法是遍历 t 然后检查数字是否小于 10。如果是。我将在它前面添加零,替换数字。这就是我想出的。
t = (2019, 1, 1, 2, 40, 0)
t = list(t)
for i in t:
if t[i] < 10:
t[i] = 0+t[i]
t[i] = t[i]
print(t)
然而,这给了我 IndexError: list index out of range
请帮忙,我是 coding/python.
的新手
我建议您使用 Python 的字符串格式化语法。
>> t = (2019, 10, 11, 3, 40, 8, 686538, None)
>> r = ("%d%02d%02d%02d%02d%02d" % t[:-2])[2:]
>> print(r)
191011034008
让我们看看这里发生了什么:
- %d 表示 "display a number"
- %2d 表示 "display a number, at least 2 digits"
- %02d 表示 "display a number, at least 2 digits, pad with zeroes"
所以我们提供所有相关数字,根据需要填充它们,并从“2019”中删除“20”。
当您使用
for i in t:
i
不是索引,每一项。
>>> for i in t:
... print(i)
...
2019
10
11
3
40
8
686538
None
如果你想使用索引,按如下操作:
>>> for i, v in enumerate(t):
... print("{} is {}".format(i,v))
...
0 is 2019
1 is 10
2 is 11
3 is 3
4 is 40
5 is 8
6 is 686538
7 is None
创建“191011034008”的另一种方法
>>> t = (2019, 10, 11, 3, 40, 8, 686538, None)
>>> "".join(map(lambda x: "%02d" % x, t[:6]))
'20191011034008'
>>> "".join(map(lambda x: "%02d" % x, t[:6]))[2:]
'191011034008'
注意:
%02d
当参数小于 10 时添加前导零,否则(大于或等于 10)使用自身。所以年份仍然是 4 位字符串。
此 lambda 不期望参数为 None。
我在 https://micropython.org/unicorn/
测试了这段代码
已编辑:
str.format方法版本:
"".join(map(lambda x: "{:02d}".format(x), t[:6]))[2:]
或
"".join(map(lambda x: "{0:02d}".format(x), t[:6]))[2:]
第二个例子的0
是参数索引。
如果你想指定它,你可以使用参数索引(例如:格式字符串和参数之间的位置不匹配,想要多次写入相同的参数......等等)。
>>> print("arg 0: {0}, arg 2: {2}, arg 1: {1}, arg 0 again: {0}".format(1, 11, 111))
arg 0: 1, arg 2: 111, arg 1: 11, arg 0 again: 1
我目前使用的是 micropython,它没有 .zfill 方法。 我想要得到的是获得 UTC 的 YYMMDDhhmmss。 例如,它给我的时间是
t = (2019, 10, 11, 3, 40, 8, 686538, None)
我可以使用 t[:6] 访问我需要的那些。现在问题出在个位数,3 和 8。我能够让它显示 1910113408,但我需要得到 19101034008 我需要在那些 2 之前得到零。我使用
t = "".join(map(str,t))
t = t[2:]
所以我的想法是遍历 t 然后检查数字是否小于 10。如果是。我将在它前面添加零,替换数字。这就是我想出的。
t = (2019, 1, 1, 2, 40, 0)
t = list(t)
for i in t:
if t[i] < 10:
t[i] = 0+t[i]
t[i] = t[i]
print(t)
然而,这给了我 IndexError: list index out of range 请帮忙,我是 coding/python.
的新手我建议您使用 Python 的字符串格式化语法。
>> t = (2019, 10, 11, 3, 40, 8, 686538, None)
>> r = ("%d%02d%02d%02d%02d%02d" % t[:-2])[2:]
>> print(r)
191011034008
让我们看看这里发生了什么:
- %d 表示 "display a number"
- %2d 表示 "display a number, at least 2 digits"
- %02d 表示 "display a number, at least 2 digits, pad with zeroes"
所以我们提供所有相关数字,根据需要填充它们,并从“2019”中删除“20”。
当您使用
for i in t:
i
不是索引,每一项。
>>> for i in t:
... print(i)
...
2019
10
11
3
40
8
686538
None
如果你想使用索引,按如下操作:
>>> for i, v in enumerate(t):
... print("{} is {}".format(i,v))
...
0 is 2019
1 is 10
2 is 11
3 is 3
4 is 40
5 is 8
6 is 686538
7 is None
创建“191011034008”的另一种方法
>>> t = (2019, 10, 11, 3, 40, 8, 686538, None)
>>> "".join(map(lambda x: "%02d" % x, t[:6]))
'20191011034008'
>>> "".join(map(lambda x: "%02d" % x, t[:6]))[2:]
'191011034008'
注意:
%02d
当参数小于 10 时添加前导零,否则(大于或等于 10)使用自身。所以年份仍然是 4 位字符串。此 lambda 不期望参数为 None。
我在 https://micropython.org/unicorn/
测试了这段代码已编辑:
str.format方法版本:
"".join(map(lambda x: "{:02d}".format(x), t[:6]))[2:]
或
"".join(map(lambda x: "{0:02d}".format(x), t[:6]))[2:]
第二个例子的0
是参数索引。
如果你想指定它,你可以使用参数索引(例如:格式字符串和参数之间的位置不匹配,想要多次写入相同的参数......等等)。
>>> print("arg 0: {0}, arg 2: {2}, arg 1: {1}, arg 0 again: {0}".format(1, 11, 111))
arg 0: 1, arg 2: 111, arg 1: 11, arg 0 again: 1