<i id='ZHh5K'><tr id='ZHh5K'><dt id='ZHh5K'><q id='ZHh5K'><span id='ZHh5K'><b id='ZHh5K'><form id='ZHh5K'><ins id='ZHh5K'></ins><ul id='ZHh5K'></ul><sub id='ZHh5K'></sub></form><legend id='ZHh5K'></legend><bdo id='ZHh5K'><pre id='ZHh5K'><center id='ZHh5K'></center></pre></bdo></b><th id='ZHh5K'></th></span></q></dt></tr></i><div id='ZHh5K'><tfoot id='ZHh5K'></tfoot><dl id='ZHh5K'><fieldset id='ZHh5K'></fieldset></dl></div>

    <legend id='ZHh5K'><style id='ZHh5K'><dir id='ZHh5K'><q id='ZHh5K'></q></dir></style></legend>

      <small id='ZHh5K'></small><noframes id='ZHh5K'>

    1. <tfoot id='ZHh5K'></tfoot>
      • <bdo id='ZHh5K'></bdo><ul id='ZHh5K'></ul>

      在 Python 中的数字列表中添加前导零

      时间:2023-06-06
        • <tfoot id='1snda'></tfoot>

          <small id='1snda'></small><noframes id='1snda'>

            <tbody id='1snda'></tbody>
            • <bdo id='1snda'></bdo><ul id='1snda'></ul>

              <legend id='1snda'><style id='1snda'><dir id='1snda'><q id='1snda'></q></dir></style></legend>

              1. <i id='1snda'><tr id='1snda'><dt id='1snda'><q id='1snda'><span id='1snda'><b id='1snda'><form id='1snda'><ins id='1snda'></ins><ul id='1snda'></ul><sub id='1snda'></sub></form><legend id='1snda'></legend><bdo id='1snda'><pre id='1snda'><center id='1snda'></center></pre></bdo></b><th id='1snda'></th></span></q></dt></tr></i><div id='1snda'><tfoot id='1snda'></tfoot><dl id='1snda'><fieldset id='1snda'></fieldset></dl></div>
                本文介绍了在 Python 中的数字列表中添加前导零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我是 Python 新手.我正在尝试调整列表的格式,如下所示:

                I am new to Python. I am trying to adjust the format of a list which looks like below:

                data=[1,10,313,4000,51234,123456]
                

                我想将它们转换为带有前导零的字符串列表:

                and I would like to convert them to a list of strings with leading zeros:

                result=['000001','000010','000313','004000','051234','123456']
                

                每个元素都有 6 个数字.

                each of the element has 6 digits.

                我知道一个数字 X,我可以做到:

                I know for a single number X, I can do:

                str(X).zfill(6)
                

                但我不确定如何将其应用于列表.我想在不使用 for 循环的情况下解决这个问题.

                but I am not sure how to apply this to a list. I would like to solve this problem without using a for loop.

                有人可以帮忙吗?谢谢.

                Anyone could help? Thanks.

                推荐答案

                应用相同zfill函数,像这样

                Apply the same zfill function in a list comprehension, like this

                >>> [str(item).zfill(6) for item in data]
                ['000001', '000010', '000313', '004000', '051234', '123456']
                

                或者,您可以使用字符串的 format方法,使用格式说明符,像这样

                Alternatively, you can use the string's format method, with format specifiers, like this

                >>> ["{:06d}".format(item) for item in data]
                ['000001', '000010', '000313', '004000', '051234', '123456']
                

                如果您要更频繁地进行格式化,则可以将其存储在变量中,如下所示

                If you are going to do the formatting more often, then you can store that in a variable, like this

                >>> formatter = "{:06d}".format
                >>> [formatter(item) for item in data]
                ['000001', '000010', '000313', '004000', '051234', '123456']
                

                如果您使用的是 Python 2.x,那么您可以使用 mapformatter 函数,像这样

                If you are using Python 2.x, then you can use map and the formatter function, like this

                >>> map(formatter, data)
                ['000001', '000010', '000313', '004000', '051234', '123456']
                

                如果您使用的是 Python 3.x,map返回一个可迭代的 map 对象.所以,你需要显式地创建一个列表,像这样

                If you are using Python 3.x, map returns an iterable map object. So, you need to explicitly create a list, like this

                >>> list(map(formatter, data))
                ['000001', '000010', '000313', '004000', '051234', '123456']
                

                这篇关于在 Python 中的数字列表中添加前导零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:Python dateutil.parser.parse 首先解析月份,而不是日期 下一篇:在 Python 中打开一个波形文件:未知格式:49. 出了什么问题?

                相关文章

                1. <i id='SUwa9'><tr id='SUwa9'><dt id='SUwa9'><q id='SUwa9'><span id='SUwa9'><b id='SUwa9'><form id='SUwa9'><ins id='SUwa9'></ins><ul id='SUwa9'></ul><sub id='SUwa9'></sub></form><legend id='SUwa9'></legend><bdo id='SUwa9'><pre id='SUwa9'><center id='SUwa9'></center></pre></bdo></b><th id='SUwa9'></th></span></q></dt></tr></i><div id='SUwa9'><tfoot id='SUwa9'></tfoot><dl id='SUwa9'><fieldset id='SUwa9'></fieldset></dl></div>

                  <small id='SUwa9'></small><noframes id='SUwa9'>

                  <tfoot id='SUwa9'></tfoot>

                    <legend id='SUwa9'><style id='SUwa9'><dir id='SUwa9'><q id='SUwa9'></q></dir></style></legend>

                      <bdo id='SUwa9'></bdo><ul id='SUwa9'></ul>